Getting Started

RStudio and Basic Quarto

April 23, 2024

Quarto

Quarto


  • Quarto is an open-source scientific publishing platform
  • Allows you to integrate text with code
  • Kind of like a word processor for data science
  • Can use it to create reports, books, websites, etc.
  • Can make HTML, PDF, Word, and other formats
  • Can use R, Python, Julia, and other languages

Project Oriented Workflow


  • Always start a document in a project folder
    • That way you don’t have to do setwd
    • Also can share easily with other people
  • Go to File>New Project
  • Create a Quarto project folder

Visual Editor

  • There are two ways to edit Quarto docs
    • Source (markdown)
    • Visual editor
  • Visual editor
    • WYSIWYM
    • Approximates appearance
  • Try both and see what you like

Rendering Documents

  • Rendering = converting to another format
    • Default format is HTML
    • Can also render to PDF, Word, etc.
  • To render a Quarto document
    • Click on the Render button
      • Or keyboard shortcut (Cmd/Ctrl + Shift + K)
  • By default, Quarto will preview the document in your browser
  • But you can also preview in Viewer pane
    • Click on the gear icon next to the Render button
    • Select “Preview in Viewer Pane”

Illustration

Let’s Try Quarto!

  • Create a new Quarto document
    • File>New File>Quarto Document
  • Save the document in your project folder
  • Render it
    • Click on the Render button
    • Or keyboard shortcut (Cmd/Ctr + Shift + K)
  • Try out the visual editor
02:00

Quarto Docs

Document Elements


  • YAML Header
  • Markdown content
  • Code chunks

YAML Header


  • Metadata about the document
    • Title, author, date, etc.
  • Output format
  • Execution options

YAML Header

---
title: "My Documnet"
author: "Your Name"
date: today
date-format: long
format: html
execute:
  echo: false
  message: false
---
  • Try changing some of these options in your document
  • Then render it again
  • Look in the Quarto guide for other options to try
02:00

Markdown


  • Markdown is a simple markup language
  • You can use it to format text
  • You can also use it to embed images, tables, etc.
  • And to embed code chunks…

Markdown Syntax - Basic Authoring

  • For basic text you can just start typing
  • For line breaks use two spaces and return (enter)
  • Headings (use #, ##, ###, etc.)
    • # is the largest heading (level 1)
    • ## is the next largest (level 2)
    • ### is the next largest (level 3)
    • Etc.

Markdown Syntax - Styling

  • Emphasis = Italics (use *)
    • Bold (use **)
  • Lists
    • Bullet points (use -)
    • Numbered lists (use 1.)

Markdown Syntax - Content

  • Links (use [text](url))
  • Images (use ![](file path or url))
  • Code chunks
    • R code chunks (```{r}…```)
    • Python code chunks (```{python}…```)
    • Etc.

Try Some Markdown


  • Check out the Markdown Cheatsheet
  • Try editing the markdown in your document
  • Try some of the other things you find in the guide
  • Then render it again
10:00

Code Chunks

  • Incorporate R code (could also be Python, Julia, etc.)
  • Add a code chunk with the ‘+’ button
  • Run the code chunk by clicking the play button
    • Or use keyboard shortcut (Cmd/Ctrl + Shift + Enter)
  • Run all chunks up that point by clicking the down arrow
    • Or use keyboard shortcut (Cmd/Ctrl + Shift + K)
  • Run a single line with shortcut (Cmd/Ctrl + Enter)

Code Chunk Options

  • Use #| (hash-pipe) to add options
  • label is a unique identifier for the chunk
  • Options to control what happens when you render
    • echo controls whether the code is shown
    • eval controls whether the code is run
    • message controls whether messages are shown
    • warning controls whether warnings are shown

Code Chunk Options


  • Code-chunk options override global options set in YAML header
  • See documentation for more options
  • You can also use write chunk options inline with chunk name,
    • e.g., {r, echo = FALSE} ...

Illustration

Try it Yourself!

  • Create a code chunk
  • Copy this code chunk into your document
library(ggplot2)

ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()
  • Try adding some chunk options in your document
  • Then render it again
02:00

A Bit About R

R Packages and Functions


  • A function is a set of instructions
    • read_csv() is a function
    • ggplot() is a function
  • A package is a collection of functions
    • readr is a package that contains the read_csv() function
    • ggplot2 is a package that contains the ggplot() function
  • Use install.packages() to install packages
  • Use library() to load packages
  • You can install packages from CRAN

The Tidyverse

  • The Tidyverse is a collection of data science packages
  • It is also considered a dialect of R
  • In this class, we will be using many Tidyverse packages
    • ggplot2 for data visualization
    • readr for reading data
    • dplyr for data manipulation
    • tidyr for data tidying
    • Etc.
  • At first we will load the packages independently, e.g. library(ggplot2)
  • Later we will load them all at once with library(tidyverse)