2.2 Opening RStudio

Navigate to where you installed RStudio and open the program. Go to File in the top left corner and select Open File, then navigate to where you saved and open getting_started.Rmd. You should now see four separate panes in RStudio. If this is your initial time setting up RStudio, I strongly suggest navigating to Tools in the upper toolbar then Modify Keyboard Shortcuts. Find ‘Insert new chunk’, click anywhere in the line, and hold Ctrl-Shift-I - we do this overwriting of the command as the standard keyboard shortcut fails on most UK computers (why? I do not know!). You may also wish to go into Tools, then Global Options, then Appearance to find an R theme that works for you.

What RStudio should look like on opening a new Notebook

Figure 2.1: What RStudio should look like on opening a new Notebook

The top left of the four panes is the Notebook. The majority of your coding will be done here. An R notebook is a document that mixes both text and code, with the code written in ‘chunks’. Chunks are a useful way to conceptualise your code - you write one chunk that accomplishes one subtask. You can run an entire chunk by clicking the green triangle in the top right corner. Alternatively, you can run any line within a chunk by placing your cursor anywhere within the line and pressing Ctrl-Enter. Note that you can have multiple notebooks open at once.

Below the Notebook pane is the Console. When you run code, its output will appear in the console. You can also code directly in the Console. A useful R workflow is to test your code directly in the console, and only put code you know works into the R Notebook chunks. It is also useful for doing quick calculations. The > symbol with a blinking cursor indicates the console is ready to receive code. While code runs, the > symbol will disappear, returning when the code is run.

EXERCISE: Run the first chunk of code in the getting_started Notebook. Where does the output appear?

EXERCISE: Calculate 347 divided by 3 in the Console (bottom left) pane. What has R done in the display?

EXERCISE: Insert a chunk that performs the same calculation below this text.

CHALLENGE EXERCISE: Compute the exact area of a circle with radius 9.

The top right pane contains the Environment pane. In R, like other programming languages, we can assign values to objects. For example, we might want to assign the value of 4 to the letter x. In R we do this with the assignment operator <- . This arrow says take the value on the right, and assign it to the object on the left.

EXERCISE: Run the first line of the chunk below. What changes? Now run the second line. Where does the output appear?

#--- Assign the value 4 to the object x
x <- 4 #--- First line
x #--- Second line

EXERCISE: Add a line to the chunk of code above that assigns the value of 1899 (when LSHTM was founded) to a new object ‘year’. Use R and this new object to calculate how many years it has been since LSHTM was founded.

The bottom right pane contains a number of tabs. We can see the structure of the current working directory under Files, we can see any plots we have generated in Plots, any packages we have installed (more on this shortly) in Packages, and any Help files we might want.