Exercise 0: R script and Quarto
File Management & Data Download
Make a new directory for this course.
Go to course website and to the Data tab. Press the Download Data button.
Move the Data folder to your course directory.
Under your course directory, make a new folder for your scripts, one for our output, and another for the presentations you download.
Your file tree should look something like this:
Working directories
- Open R studio.
- Access your current working directory by writing
getwd()
in the console.
The working directory in R (or other programming environments) is the folder on your computer where R looks for files to read or write by default. When you load or save data, R will use the working directory unless you specify another path. A path can either be absolute or relative: - Absolute path: The path from the root of your file system to the input file. - Relative path: The path from the working directory to the input file.
R Script
- Create an R script and save it in your work folder.
An R script is a plain text file containing a series of R commands and code used for data analysis. R scripts have a .R
extension and can be executed line-by-line in an interactive R session or as a whole script. They are ideal for automating workflows and keeping your analyses reproducible and organized. Code can be run
Type
getwd()
in your R script and run the line. Compare the working directory with the one from the console.Change the working directory using
setwd()
.Run
getwd()
again.Type in a few lines of code and some comments and re-save the file.
Quarto
- Create an Quarto document and save it in your work folder.
Quarto is an open-source publishing system designed to help you create dynamic, reproducible documents, presentations, and websites. It extends the ideas of tools like R Markdown, combining simplicity with powerful customization options for modern scientific and technical communication.
Type
getwd()
in a code chunk in your Quarto document and run the line. Compare the working directory with the one from the console.Change the working directory in one chuck using
setwd()
.Run
getwd()
in the same chunk assetwd()
AND another. What do you observe?Create some code chunks, write text and headers. Re-save the file.
Render the Quarto document and have a look at the html file.
Resources for Quarto:
Quarto website
“Get started with Quarto” tutorial for RStudio
“Get started with Quarto” video for RStudio
Comprehensive guides to Quarto basics
R project
An R project in RStudio creates a self-contained working environment tied to a specific folder, which becomes the default working directory for all scripts, data, and outputs. This structure helps organize files, ensures reproducibility, and simplifies path management. By default, a Quarto document’s working directory is its file location. While this can be changed chunk-wise, the working directory for R scripts can be set globally for all scripts in a folder by creating an R project. The R project is a small configuration file, usually placed in the root of the project folder, and requires no manual interaction—it quietly ensures your workflows remain well-organized.
Create an R project by clicking the Project (None) in the top right → New Project → Existing Directory and choose an appropriate location.
Check the working directory of the R script and Quarto document. Is it as you expected?
If you like the flow of the R project, keep it. If not, delete it. It is not necessary to have an R project.