1.1 Opening RStudio

First, open RStudio from the Novell Applications window. Go to File in the top left corner and select Open File, then navigate to and open prac08_intro.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 LSHTM computers. You may also wish to go into Tools, then Global Options, then Appearance to find an R theme that works for you (i.e. you can make it dark like Stata if you wish!).

What RStudio should look like on opening a new Notebook

Figure 1.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. Go ahead and retitle your notebook now, overwriting the default text. The notebook itself is written in R Markdown. R Markdown provides a convenient way of mixing text and code, and also allows you to publish (‘knit’) your work into an html, Word, or pdf file (as has been done with what you are reading). Areas in your selected background colour in R Markdown are text. Interspersed amongst this text are code ‘chunks’, denotated by three backticks (`) to close and open them. To run all the code a chunk, click anywhere in the chunk and press Ctrl-Shift-Enter, or press the green triangle in the top right corner of the chunk. To run a single line within a chunk, place your cursor anywhere in the line, and press Ctrl-Enter. Try these ways of running the chunk on the below.


A plot (showing data from one of R’s built in datasets) should have appeared directly below the chunk. You should also notice a change in the Console, the pane directly below the Notebook. There should be an additional line “> plot(cars)”. This is R’s way of letting you know which code has been run. When the code is finished running, at the bottom of the pane, R will show only “>” followed by your cursor. Coding can also be done directly in the console - this is useful for testing out code that you are not sure will work. In general, you want to keep your R Notebook quite clean. Type or copy ‘plot(cars)’ into the console (or click into the console and hit the up arrow key to scroll through your command history) and press Enter to run it. This time, the plot should have appeared in the bottom right pane, under the “Plots” tab. From here, you can easily export (save) this image using the Export button.