R for Biologists course

R takes time to learn, like a spoken language. No one can expect to be an R expert after learning R for a few hours. This course has been designed to introduce biologists to R, showing some basics, and also some powerful things R can do (things that would be more difficult to do with Excel). The aim is to give beginners the confidence to continue learning R, so the focus here is on tidyverse and visualisation of biological data, as we believe this is a productive and engaging way to start learning R. After this short introduction you could use this book to dive a bit deeper.

Intro to R and RStudio

RStudio is an interface that makes it easier to use R. There are four windows in RStudio. The screenshot below shows an analogy linking the different RStudio windows to cooking.


R script vs console

There are two ways to work in RStudio in the console or in a script. We can type a command in the console and press Enter to run it. Try running the command below in the console.

1 + 1
## [1] 2

Or we can use an R script. To create a script, from the top menu in RStudio: File > New File > R Script. Now type the command below in the script. This time, to run the command, you use Ctrl + Enter for Windows/Linux or Cmd + Enter for MacOS. This sends the command where the cursor is from the script to the console. You can highlight multiple commands and then press Cmd/Ctrl + Enter to run them one after the other.

2 + 2
## [1] 4

As the RStudio screenshot above explains, if we work in the console we don’t have a good record (recipe) of what we’ve done. We can see commands we’ve run in the History panel (top right window), and we can go backwards and forwards through our history in the console using the up arrow and down arrow. But the history includes everything we’ve tried to run, including our mistakes so it is good practice to use an R script.

We can also add comments to a script. These are notes to ourself or others about the commands in the script. Comments start with a # which tells R not to run them as commands.

# testing R
2 + 2
## [1] 4

Keeping an accurate record of how you’ve manipulated your data is important for reproducible research. Writing detailed comments and documenting your work are useful reminders to your future self (and anyone else reading your scripts) on what your code does.

Working directory

Opening an RStudio session launches it from a specific location. This is the ‘working directory’. R looks in the working directory by default to read in data and save files. You can find out what the working directory is by using the command getwd(). This shows you the path to your working directory in the console. In Mac this is