Installing R and RStudio (adopted from datacarpentry)


For this course, you will need both R and RStudio. R is a programming language that is particularly powerful for statistical data analysis and broadly used across almost all scientific disciplines. RStudio is an Integrated Development Environment (IDE) that makes it much easier to access R and develop reproducible code. Please make sure to install R first.


To install R and RStudio on Windows:

  • Download R for Windows from CRAN
  • Run the downloaded .exe file to install R
  • Download RStudio for Windows
  • Install by executing the downloaded .exe file


To install R and RStudio on MacOS:

  • Download R for Mac from CRAN
  • Double click on the downloaded .pkg file to install R
  • Also download and install XQuartz (needed by some packages)
  • Download RStudio for MacOS
  • Install by executing the downloaded .pkg file


To install R and RStudio on Linux:

  • Download and install the correct version of R on CRAN (versions differ between Linux distributions)
  • Download and install the correct version of RStudio (versions differ between Linux distributions)


In all cases, open RStudio and type 2+2 into the console (the command line window at the bottom left) and hit enter. If a 4 appears, you are all set up.

1 What is R?

  • a very flexible, open-source programming environment for (statistical) data analyses and visualisation (www.r-project.org)
  • an object-oriented and interpreted programming language based on the S language (see here)
  • runs on all common operating systems

How to operate R:

  • R is operated from the command line
  • work with script files for easier tractability, recycling and modification of commands
  • scripts also allow you to send big chunks of code or the entire script to the R console
  • scripts are the first step for automatizing your data manipulation and analyses
  • R has a built-in editor, but many people nowadays use R Studio (http://rstudio.org/) or other text editors

Additional R resources:

2 Create a new project in RStudio

We recommend using RStudio throughout the course.

We also recommend setting up an RStudio project for the entire course (UP students can watch the video tutorial in Moodle) and within the RStudio project separate R scripts for each session. To do so:

  • Open RStudio
  • Go to the tab “File” and select “New Project”
  • Select either “New Directory” if you haven’t set up a course folder yet, or select “Existing Directory” to point towards an existing folder. This directory will be the working folder for the course. Navigate to the path of the project folder on your machine (e.g. ’~Documents/Courses/R-intro)
  • Name your project
  • Use your file explorer on your machine, find your project folder, and create four sub-folders: data, scripts, results, and figures
  • Create a new empty R script by going to the tab “File”, select “New File” and then “R script”
  • In the new R script, type # Session 1: Getting started in R and save the file in your folder “scripts” within your project folder, e.g. as “1_GettingStarted.R”.

3 RStudio interface

Your interface should now be split into four parts:

  • Script: here, you type in the code you want to be able to save. This way you can use it again at a later point in time or share it with others. Use the Ctrl + Enter shortcut or the “Run” button to execute commands.
  • Console: results generated by your code will show up here. You can also type directly into the console and execute commands using Enter, but these commands will then not be saved for the next session.
  • Environment: This shows you all the objects you currently have in your workspace.
  • Files/Plots/Packages/Help: here, you can see the files in your current pathway, plots you created, the package you have installed, and access the help section.

4 Console and prompt

In the R console you will find the symbol >:

>

This is the so-called prompt, which asks you for the next command.

The most basic thing you can do now is to type in some mathematical calculations. If you see a + instead, then the former command is not completed yet, and R waits for further arguments (maybe you have forgotten the closing bracket?).

20 * (5 - 3
)

A line break or a semicolon (;) separates commands.

2 + 3; 2 + 4

Anything following a hashmark #, will be interpreted as a comment and will, thus, not be executed.

1 + 3
# 1 + 3

You could type all code examples that are listed here directly into your R console. You can also view the previous comments you executed in the console by using the cursor/arrow keys. However, we highly recommend storing everything into a script file!

5 Working directory

R is always pointed at a specific directory on your computer, the working directory. You can check the current working directory by typing:

getwd()

If you set up the RStudio project correctly, then getwd() should show your course folder as your current working directory. Else, you change the working directory using setwd(). Check out the help page:

?setwd

RStudio also offers an option for setting the working directory in the tab bar. To do so, go to the tab “Session > Set Working Directory”.

See the files contained in your folder:

dir()

Exercises:

  • Use the dir() command in R to check whether the script you are working on has been saved in the correct folder (scripts).

References

Crawley, Michael J. 2007. The r Book. UK: John Wiley & Sons.