setwd('PATH')
getwd()
Exercise 0: Getting started
File Management & Data Download
- To get this material you went to the github repository of this course: https://github.com/Center-for-Health-Data-Science/R4DataScience, pressed Code → Download ZIP and downloaded the directory with the course material called R4DataScience or R4DataScience-main. This folder contains several sub directory with more and less relevant material. The files you need for this course is:
slides/R4datascience_slides.pdf
: contains the lecture slides in pdf format.presentations/*
: all the scripts that the lectures will go through in plenum during the course.exercises/*
: all the exercises that you will go through yourselves. You are currently looking atexercises/exercise0.qmd
.data/*
: all the data you need for the exercises.
- Under the course directory, make a sub directory for the outputs that you will generate doing the exercises.
Your file tree should look something like this:
Working directories
- As you have Rstudio open, access your current working directory by typing
getwd()
in the console.
The working directory in R (and 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 exercise 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. R scripts can be submitted to a job on a supercomputer unlike Quarto documents.
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.Read in the file from
data/diabetes.csv
using theread_csv()
function and check the structure of the data with thestr()
function. Re-save the file.
Quarto
- Create an Quarto document and save it in your exercise 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 chunk using
setwd()
.Run
getwd()
in the same chunk assetwd()
AND in another chunk. What do you observe?
getwd()
Create a code chunk and write the same code as you did in 8. Write a description of what you did above the code chunk. Re-save the file.
Render the Quarto document and have a look at the html file.
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.
- Before doing this exercise, we need to brief you about what will happen when you do the instructions below:
Rstudio will ask you if you want to save your current workspace and to that you will answer Don’t Save.
Rstudio will close down your current session and open a new one which means that this Quarto will not be open in Rstudio anymore. After you have done the instructions below, simply navigate to this document and reopen it.
Great - now to the exercise:
Create an R project by clicking the Project (None) in the top right → New Project → Existing Directory and choose an appropriate location. Look at the top-right corner to check that you are in your R project.
- Reopen the the R script and Quarto document you created in Exercise 4 and 9 respectively. Check each of their working directories. Are they as you expect? Explain.
The working directory of the R script is the same as the location of the
.Rproj
file.The working directory of the Quarto document is always the same as the location of the document.