Install R and RStudio [optional]

If you’re working with your own data, install the software on your computer

Remember you can also complete assignments by running R code in your browser instead

Create an RStudio project

If you’re using R and RStudio on your own computer, first create a new RStudio project, to help us keep our data, code and outputs organised:

  1. Open RStudio
  2. Go to Project > Create Project
  3. Click on New Project
  4. Type in a directory name, e.g. ExploreData
  5. Click the Browse button to choose where to save your ExploreData folder
  6. Click Create Project
  7. Go to File > New R Script to create a blank text file in which to type and save your code

This is what your RStudio window should look like, with the different panels labelled:

Screenshot of RStudio with labelled panes

Run code in RStudio

Once you have typed some code into your new script, you need to run it to see the output in the Console panel

The easiest way to run the functions from RStudio is by using the keyboard, because using the mouse slows you down (and fatigues your hands)

  1. Move your cursor to the first line
  2. To run the entire line of code where your cursor is, press Ctrl+R. RStudio will send the function(s) to the R window below, and R will process it
  3. R gives you the answer immediately you run the code
  4. The cursor will jump to the next line of code automatically, so you can keep pressing Ctrl+R to run multiple lines
  5. You can also
    1. Run multiple lines by highlighting a block of text and pressing Ctrl+R
    2. Run just part of a line by selecting only the code you want to run

Your code is shown in blue text, the R output (answers) in black text, and error messages in red

Back to top

Footnotes

  1. OS: Operating Systems↩︎