Exercise 0: R script and Quarto

File Management & Data Download

  1. Make a new directory for this course.

  2. Go to course website and to the Data tab. Press the Download Data button.

  3. Move the Data folder to your course directory.

  4. 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

  1. Open R studio.
  2. 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

  1. 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

  1. Type getwd() in your R script and run the line. Compare the working directory with the one from the console.

  2. Change the working directory using setwd().

  3. Run getwd() again.

  4. Type in a few lines of code and some comments and re-save the file.

Quarto

  1. 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.

  1. Type getwd() in a code chunk in your Quarto document and run the line. Compare the working directory with the one from the console.

  2. Change the working directory in one chuck using setwd().

  3. Run getwd() in the same chunk as setwd() AND another. What do you observe?

  4. Create some code chunks, write text and headers. Re-save the file.

  5. 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.

  1. Create an R project by clicking the Project (None) in the top right → New ProjectExisting Directory and choose an appropriate location.

  2. Check the working directory of the R script and Quarto document. Is it as you expected?

  3. If you like the flow of the R project, keep it. If not, delete it. It is not necessary to have an R project.