FJUEL workshop
Dynamic documents in R: Introduction to Quarto

Session 1: R project setup and Quarto documents

Session 1: Learning goals

Learning goals

  • Understand the fundamentals of working with R
  • Learn how to organize your R project and workflow
  • Create and render your first quarto notebook
  • Take on board some R basics

Fundamentals

Fundamentals

  • Use R with RStudio
  • Create an RStudio project for each new research project/study
  • Use notebooks (Quarto documents), which include R code + output
  • Project folder: Organization into subfolders (data, figures, etc.)
  • Everything should always be reproducible from code + input data

R projects: Setup

Create an RStudio project

  • File : New project…
  • New Directory
  • New Project
  • Use an informative directory name
  • Store it where you will find it

RStudio project organization

  • A project should be self-contained: All in one place
  • Keep data files and .qmd-notebooks in your project folder
  • Use subfolders: data, figures

Reproducibility

Your data analysis project should be self-contained, which means that everything that is needed to reproduce the results is stored in the project folder.

RStudio project setup

  • Go to your project folder
  • Create two subfolders: data, figures

RStudio project setup

This is what a minimal R project setup looks like:

Quarto documents

Use notebooks (Quarto documents)

  • Quarto documents combine text, code, and output
  • Can be exported as html/Word/PDF file
  • In the analysis notebook:
    • Talk to your (future) self: Say what you do (and why you do it)
    • Run analyses
    • Look at output (figures, tables,…)

Create a Quarto document

  • File : New file : Quarto document…
  • Use default options, click “Create”
  • Save it in your project folder
  • Render it: Click the Render button

Play around with the Quarto file

  • Compare contents of file with rendered version
  • Change/add text and render again
  • Add headers / sub-headers
  • Run code inside of RMarkdown file
  • Add bold print **bold**
  • Add italics *italics*
  • Have a look at the RMarkdown Cheat Sheet

R basics

R: General advice

  • Use the tidyverse set of packages
  • Use ggplot2 for data visualization (Session 2)
  • Workflow: Document every step using code
  • Aim: Full reproducibility based on code + data

R packages

  • For some things, you need packages (sets of functions)
  • You need to install a package only once: install.packages()
install.packages("tidyverse")
  • Packages must be loaded at beginning of R session: library()
library(tidyverse)
  • Notebooks should start with section ## R setup, where you load the packages you need

In R, everything is an object

  • Functions, data tables, etc.
  • Create an object by assigning something to a new name
  • Assignment operator: <- (shortcut: Alt + -)
  • New objects then exist in R’s workspace (top right panel)
  • Important object types:
    • Vector: Sequence of elements (e.g. names, numbers)
    • Data frame/tibble: Data tables

In R, you do things using functions

  • Functions do the work in R (calculation, graphing, etc.)
  • Functions have parentheses at the end of their names
  • We give instructions to a function using its arguments
  • function(argument = "...")

Some useful functions

  • Inspect contents of data frame: str()
  • Inspect first few rows of data frame: head()
  • mean(), median()
  • Access columns (= variables) in a data frame: $

Session 1: Practice

Time for practice! The tasks are available here. (there is also a link on my website)