R scripts

Learn about how to organise your code in R scripts
Author

Stefano Coretta

Published

September 18, 2024

Pre-requisites

1 R scripts

So far, you’ve been writing R code in the Console and run it there.

But this is not very efficient. Every time, you need to write the code and execute it in the right order and it quickly becomes very difficult to keep track of everything when things start getting more involved.

A solution is to use R scripts.

R script

An R script is a file with the .R extension that contains R code.

For the rest of this tutorial, you will write all code in an R script.

2 Create an R script

First, create a folder called code in your Quarto project folder. This will be the folder where you will save all of your R scripts and other documents.

Now, to create a new R script, look at the top-left corner of RStudio: the first button to the left looks like a white sheet with a green plus sign. This is the New file button. Click on that and you will see a few options to create a new file.

Click on R Script. A new empty R script will be created and will open in the File Editor window of RStudio.

Note that creating an R script does not automatically saves it on your computer. To do so, either use the keyboard short-cut CMD+S/CTRL+S or click on the floppy disk icon in the menu below the file tab.

Save the file inside the code/ folder with the following name: tutorial-w02.R.

Warning

Remember that all the files of your RStudio project don’t live inside RStudio but on your computer.

So you can always access them from the Finder or File Explorer! However, do not open a file by double clicking on it from the Finder/File Explorer.

Rather, open the Quarto project by double clicking on the .Rproj file and then open files from RStudio to ensure you are working within the RStudio project and the working directory is set correctly.

2.1 Write code

Now, let’s start filling up that script!

Generally, you start the script with calls to library() to load all the packages you need for the script.

Now we only need one package, tidyverse, but in most cases you will need more than one! The best practice is to attach all of packages first, in the top of your script. Please, get in the habit of doing this from now, so that you can keep your scripts tidy and pretty!

Go ahead, write the following code in the top of the my_first.R script.

library(tidyverse)

shallow <- read_csv("./data/song2020/shallow.csv")

Wait, what is that "./data/shallow.csv"? That’s a relative path, remember?

When you are using R scripts in Quarto projects, the ./ folder paths are relative to is the project folder! This is true whichever the name of the folder/project and whichever it’s location on your computer.

Warning

Please, don’t include install.packages() in your R scripts!

Remember, you only have to install a package once, and you can just type it in the Console.

But DO include library() in your scripts.

3 Run the script!

Finally, the time has come to run the script.

There are several ways of doing this. The most straightforward is to click on the Run button. You can find this in the top-right corner of the script window.

An alternative way is to place the text cursor on the line of code you want to run and then press CMD+ENTER/CTRL+ENTER. This will run the line of code and move the text cursor to the next line of code.

You can even select multiple lines of code (as you would select text) and press CMD+ENTER/CTRL+ENTER to run multiple lines of code!

Now that you know how to use R scripts and run code in them, I will assume that you will keep writing new code from this tutorial in your script and run it from there!

Just another tip: sometimes we might want to add a few lines of text in our script, for example to take notes.

You can add so-called comments in R scripts, simply by starting a line with #.

For example:

# This is a comment. Let's add 6 + 3.
6 + 3
[1] 9
Quiz 1

Is the following a valid line of R code?

sum(x + 2) # x = 4

It is a valid line of R code, but if you got an error it is probably because the variable x does not exist (yet). If you add the line x <- 4 before sum(x + 2) # x = 4, the latter will work just fine.

So you see there is a difference between valid code and working code.