01 - Performing magic

with Quarto

Tom Mock, RStudio PBC

2022-07-25


WiFi

  • Username: conf22
  • Password: together! (at last!)

Sli.do

Gender Neutral Bathrooms

  • There are gender neutral bathrooms by the National Harbor rooms.

Specialty Rooms

  • The meditation room is located at National Harbor 9. Open 8am - 5pm, Monday - Thursday. The hotel also has a dedicated room behind the reception.
  • The lactation room is located at Potomac Dressing Room. Open 8am - 5pm, Monday - Thursday.

Lanyards

  • Participants who do not wish to be photographed have red lanyards, please note everyone’s lanyard colors befor taking a photo and respect their choices.

Code of Conduct

Please read and review the entire Code of Conduct carefully: https://www.rstudio.com/conference/2022/2022-conf-code-of-conduct/

COVID-19

RStudio requires that in-person attendees be fully vaccinated against COVID-19 prior to the start of the event.

RStudio requires that you wear a mask that fully covers your mouth and nose at all times in all public spaces. We strongly recommend that you use a correctly fitted N95, KN95, or similar particulate filtering mask; we will have a limited supply available upon request.

  • You can remove your mask to briefly drink/eat during breaks in the hall but please refrain from removing your mask in the workshop room itself

Code of Conduct

Please read and review the entire Code of Conduct carefully: https://www.rstudio.com/conference/2022/2022-conf-code-of-conduct/

Anti-Harassment

We expect everyone at rstudio::conf, whether attending in-person or virtually, to contribute to a welcoming, safe, and tolerant environment.

Reporting

You can make a personal report:

In person: Contact any rstudio::conf staff member, identifiable by their orange staff t-shirt shirt, or visit the conference registration desk.

By email: Send a message to conf@rstudio.com; event organizers will respond promptly.

By phone: Call 844-448-1212; this phone number will be monitored for the duration of the event.

General Info

Need help?

Look for orange shirts!

Hello Quarto

Hello Learners

We assume

You know R

You know a little bit of Markdown

You want to learn about Quarto, the next-generation of RMarkdown

We’ll teach you

Quarto syntax and formats

More Markdown

Even more R along the way!

Find me at:

@thomas_mock

@jthomasmock

themockup.blog

thomas@rstudio.com

Your TAs

Carlos Scheidegger - RStudio + Quarto

Isabel Zimmerman - RStudio + Python/Vetiver

Katie Masiello - RStudio + Solutions Engineering

Tan Ho - ML Engineer at Zelus Analytics

Workshop Prep

pkg_list <- c(
  "tidyverse", "gt", "gtExtras", "reactable", "ggiraph", "here", "quarto",
  "rmarkdown", "gtsummary", "palmerpenguins", "fs", "skimr"
  )
install.packages(pkg_list)

Great, let’s get started!

Structure of the workshop

  • Lecture - 🤠
  • “Your turn” or “Our Turn” - 💻
  • Breaks - ☕

In-session:

  • = Tom isn’t explaining it well enough or I need time

  • = I have completed the “Our Turn”

  • Raised hand = I have a question for Tom/TA - please try and make eye-contact with me or the TA, based on I have a “general question” or I’m running into a code error.

  • Use the https://slido.com with code for questions: #start-quarto

Ease of Use VS Full Details

Pouring milk

A straight-forward task - pour the boxed milk into the glass.

Easy to use

Pouring milk

A straight-forward task - pour the boxed milk into the glass.

Easy to use

Understanding the full details

Our Turn

  • Open Sesame RStudio
  • If you haven’t already, clone the workshop: https://github.com/jthomasmock/quarto-workshop.git
  • Open the quarto-workshop RProject



01:00

Hello Quarto

Quarto® is an open-source scientific and technical publishing system built on Pandoc

You can weave together narrative text and code to produce elegantly formatted output as documents, web pages, blog posts, books and more.

Why Quarto?1

Shakespeare’s Hamlet as a “Quarto”

What about RMarkdown?

A brief history of knitr

  • 2011-10-16: initial development

  • 2012-01-17: initial CRAN release

  • 50 CRAN releases in 10 years https://cran.r-project.org/src/contrib/Archive/knitr/

A brief history of rmarkdown

  • Initial experiment: knitr::pandoc() (2014)

  • Matured as the rmarkdown package (2015)

A brief history of R Markdown - Yihui Xie, 2021-09-09

What about RMarkdown?

So what is Quarto?



Quarto is the next-generation of RMarkdown.

For everyone.

So what is Quarto?

Quarto is a command line interface (CLI) that renders plain text formats (.qmd, .rmd, .md) OR mixed formats (.ipynb/Jupyter notebook) into static PDF/Word/HTML reports, books, websites, presentations and more

thomasmock$ quarto --help

  Usage:   quarto
  Version: 1.0.36

  Description:
    Quarto CLI

  Options:
    -h, --help     - Show this help.                            
    -V, --version  - Show the version number for this program.  

  Commands:
    render          [input] [args...]   - Render input file(s) to various document types.            
    preview         [file] [args...]    - Render and preview a document or website project.          
    serve           [input]             - Serve a Shiny interactive document.                        
    create-project  [dir]               - Create a project for rendering multiple documents          
    convert         <input>             - Convert documents to alternate representations.            
    pandoc          [args...]           - Run the version of Pandoc embedded within Quarto.          
    run             [script] [args...]  - Run a TypeScript, R, Python, or Lua script.                
    install         <type> [target]     - Installs an extension or global dependency.                
    publish         [provider] [path]   - Publish a document or project. Available providers include:
    check           [target]            - Verify correct functioning of Quarto installation.         
    help            [command]           - Show this help or the help of a sub-command.    

Quarto, more than just knitr

We learned from 10 years of literate programming with knitr + rmarkdown

Quarto, more than just knitr

Quarto, more than just knitr

Comfort of your own workspace

A screenshot of a Quarto document rendered inside RStudio

A screenshot of a Quarto document rendered inside JupyterLab

A screenshot of a Quarto document rendered inside VSCode

Quarto, more internals

  • knitr or jupyter evaluates R/Python/Julia code and returns a .md file along with the evaluated code
  • Quarto applies Lua filters + CSS/LaTeX which is then evaluated alongside the .md file by Pandoc and converted to a final ouput format

Here is an example of a Lua filter that converts strong emphasis to small caps, from https://pandoc.org/lua-filters.html

return {
  {
    Strong = function (elem)
      return pandoc.SmallCaps(elem.c)
    end,
  }
}

Lua filters written by R/Python/Julia developers should be interchangeable between formats - not language specific!

So what is Quarto?

Quarto® is an open-source scientific and technical publishing system built on Pandoc

So what is Quarto?

A Quarto document i.e. a .qmd is a plain text file, like a .rmd, that can be rendered to many different formats

A screenshot of a Visual and Source Quarto document

One install, “Batteries included”

  • RMarkdown grew into a large ecosystem, with varying syntax.

Quarto comes “batteries included” straight out of the box

  • HTML reports and websites
  • PDF reports
  • MS Office (Word, Powerpoint)
  • Presentations (Powerpoint, Beamer, revealjs)
  • Books
  • Any language, exact same approach and syntax

Many Quarto formats

Feature R Markdown Quarto
Basic Formats
Beamer
PowerPoint
HTML Slides
Advanced Layout

Many Quarto formats

Feature R Markdown Quarto
Cross References
Websites & Blogs
Books
Interactivity Shiny Documents Quarto Interactive Documents
Paged HTML pagedown Summer 2022
Journal Articles rticles Summer 2022
Dashboards flexdashboard Fall 2022

What to do with my existing .Rmd or .ipynb?

For some of you - nothing changes! Keep using RMarkdown and Jupyter.


However, most existing .rmd or .ipynb can be rendered as-is via Quarto

quarto render my-favorite.rmd --to html


Since Jupyter notebooks can either be treated as a linear document to be re-executed or an already evaluated document there are additional options like: --execute

quarto render my-favorite.ipynb --to html --execute

Quarto comes out of the box with much better accessibility, better defaults, more options, and a much more consistent syntax.

Summary of Quarto > RMarkdown

  • Easier to organize/structure document and document layout
  • Features largely cross-format
  • Better ability to integrate multiple languages in a PROJECT
  • Evaluate native language (R in knitr, Python/Julia in Jupyter)
  • HTML slides with revealjs are pandoc-compatible, so RStudio Visual Editor works with them

Rendering

  1. Render in RStudio
  1. System shell via quarto render
quarto render document.qmd # defaults to html
quarto render document.qmd --to pdf
quarto render document.qmd --to docx
  1. R console via quarto R package
library(quarto)
quarto_render("document.qmd") # defaults to html
quarto_render("document.qmd", output_format = "pdf")

Our Turn

  • Use RStudio to create a new default Quarto document
  • Add a title and your name as the author
  • Change format to html theme to sketchy
  • Add toc
  • Create two sections, one with fact you want to learn and your favorite thing about R
  • Render it with RStudio button, then try with the R package and terminal
02:00

What about for Data Science at Work?

  • Quarto can be used in RStudio Team today!
  • Quarto v1.0 comes pre-installed in RStudio Workbench 2022.07.1 and later. You can use Quarto in RStudio Workbench’s RStudio IDE, JupyterLab/Jupyter notebooks, or VSCode.
  • Quarto can be published and re-executed by RStudio Connect!

Why Quarto, instead of RMarkdown

  • Batteries included, shared syntax
  • Choose your own editor and your preferred data science language
  • Better accessibility and richer features out of the box
  • More enhancements overtime - RMarkdown still maintained, but majority of new features built into Quarto

Collaboration with other colleagues in other languages - shared format, choose your editor and your native language

Why Quarto

  1. Reproducibility
  2. Re-usability
  3. Extensibility
  4. “Lazy” ability

Change your mental model

Source

Output

Change your mental model

Source

Output

Change your mental model

---
title: "ggplot2 demo"
author: "Norah Jones"
date: "5/22/2021"
format: 
  html:
    fig-width: 8
    fig-height: 4
    code-fold: true
---

## Air Quality

@fig-airquality further explores the impact of temperature 
  on ozone level.

```{r}
#| label: fig-airquality
#| fig-cap: Temperature and ozone level.
#| warning: false

library(ggplot2)
ggplot(airquality, aes(Temp, Ozone)) + 

  geom_point() + 
  geom_smooth(method = "loess"
)
```

Quarto

Quarto® is an open-source scientific and technical publishing system built on Pandoc. You can weave together narrative text and code to produce elegantly formatted output as documents, web pages, blog posts, books and more.

No more copy-paste, no more manually rebuilding analyses from disparate components, no more dread when the data is updated and you need to run an analysis.

Anatomy of a Quarto document

  • Metadata (YAML)
---
format: html
---
  • Code
```{r}
#| eval: true
library(dplyr)
mtcars %>% 
  group_by(cyl) %>%
  summarize(mean = mean(mpg), .groups = "drop")
```
# A tibble: 3 × 2
    cyl  mean
  <dbl> <dbl>
1     4  26.7
2     6  19.7
3     8  15.1
  • Text
# Heading 1
This is a sentence with some **bold text**, some *italic text* and an [image](image.png).

Metadata: YAML

The YAML metadata or header is:

processed in many stages of the rendering process and can influence the final document in many different ways. It is placed at the very beginning of the document and is read by each of Pandoc, Quarto and knitr. Along the way, the information that it contains can affect the code, content, and the rendering process.

YAML

---
title: "My Document"
format: 
  html:
    toc: true
    code-fold: true
---

Markdown

Quarto is based on Pandoc and uses its variation of markdown as its underlying document syntax. Pandoc markdown is an extended and slightly revised version of John Gruber’s Markdown syntax.

Markdown is a plain text format that is designed to be easy to write, and, even more importantly, easy to read

Text Formatting

Markdown Syntax Output
*italics* and **bold**
italics and bold
superscript^2^ / subscript~2~
superscript2 / subscript2
~~strikethrough~~
strikethrough
`verbatim code`
verbatim code

Headings

Markdown Syntax Output
# Header 1

Header 1

## Header 2

Header 2

### Header 3

Header 3

#### Header 4

Header 4

##### Header 5
Header 5
###### Header 6
Header 6

Code

```{r}
#| output-location: column
#| label: fig-airquality
#| fig-cap: Temperature and ozone level.
#| warning: false

library(ggplot2)

ggplot(airquality, aes(Temp, Ozone)) + 
  geom_point() + 
  geom_smooth(method = "loess"
)
```

Figure 1: Temperature and ozone level.

Code, more than just R

```{python}
#| label: fig-polar
#| fig-cap: "A line plot on a polar axis"

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(
  subplot_kw = {'projection': 'polar'} 
)
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()
```

What about the CLI?

Quarto is a command line interface (CLI) that renders plain text formats (.qmd, .rmd, .md) OR mixed formats (.ipynb/Jupyter notebook) into static PDF/Word/HTML reports, books, websites, presentations and more

A screenshot of a Visual and Source Quarto document

Our Turn

  • In RStudio, open materials/workshop/01-intro/history.qmd
  • Render with the RStudio > Render button
  • Look at it and compare to Wikipedia source


  • Move to the integrated terminal and execute:
quarto render --help
  • Render via terminal with quarto render history.qmd --to pdf

  • Compare to the HTML version

03:00

Resources

Quarto questions?

RStudio Refresher

https://r4ds.had.co.nz/introduction.html#prerequisites

RStudio Tips

  • Search for and open file with Ctrl + I
  • RStudio pane and Pane Layout

CLI Overview

thomasmock$ quarto --help

  Usage:   quarto
  Version: 1.0.36

  Description:
    Quarto CLI

  Options:
    -h, --help     - Show this help.                            
    -V, --version  - Show the version number for this program.  

  Commands:
    render          [input] [args...]   - Render input file(s) to various document types.            
    preview         [file] [args...]    - Render and preview a document or website project.          
    serve           [input]             - Serve a Shiny interactive document.                        
    create-project  [dir]               - Create a project for rendering multiple documents          
    convert         <input>             - Convert documents to alternate representations.            
    pandoc          [args...]           - Run the version of Pandoc embedded within Quarto.          
    run             [script] [args...]  - Run a TypeScript, R, Python, or Lua script.                
    install         <type> [target]     - Installs an extension or global dependency.                
    publish         [provider] [path]   - Publish a document or project. Available providers include:
    check           [target]            - Verify correct functioning of Quarto installation.         
    help            [command]           - Show this help or the help of a sub-command.