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:
To install R and RStudio on MacOS:
To install R and RStudio on Linux:
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.
How to operate R:
Additional R resources:
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:
# Session 1: Getting started in R
and save the file in your
folder “scripts” within your project folder, e.g. as
“1_GettingStarted.R”.Your interface should now be split into four parts:
Ctrl + Enter
shortcut or the “Run” button
to execute commands.Enter
, but these commands will then not be saved for the
next session.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!
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:
dir()
command in R to check whether the script
you are working on has been saved in the correct folder (scripts).