library(tidyverse)
<- read_csv("./data/song2020/shallow.csv") shallow
R scripts
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.
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
.
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.
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.
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