Lecturer in Health Data Science within Centre for Health Informatics, Computing, and Statistics.
Academic background in statistics, with experience in data science consultancy and training.
Using R for over 10 years, and author of multiple R packages.
Combines slides, live coding examples, and exercises for you to participate in.
Ask questions throughout!
I hope you end up with more questions than answers after this workshop!
Course website: nrennie.rbind.io/training-intro-to-quarto
How do you write your journal articles?
How do you currently perform analysis?
Quarto is an open-source scientific and technical publishing system that allows you to combine text, images, code, plots, and tables in a fully-reproducible document.
Quarto has support for multiple languages including R, Python, Julia, and Observable.
It also works for a range of output formats such as PDFs, HTML documents, websites, presentations,…
More journals require code to be submitted (for transparency and reproducibility). Keeping the code with the paper makes this easier.
Copying and pasting is tedious (and a great source of accidental errors).
If you fix an error in code or data, the results and figures in the paper update automatically.
Easy to share publicly.
Open source so anyone can use it.
R Markdown isn’t going anywhere but…
Quarto has better multi-language support
More user-friendly
Better control of the output layouts
Source editor
Visual editor
Within RStudio IDE: click Render (or Ctrl+Shift+K)
YAML header
Content
Text, links, images
Code, tables, plots
Equations, references
Documents: HTML, PDF, MS Word, Markdown
Presentations: Revealjs, PowerPoint, Beamer
Websites
Books
…
docx
.05:00
In Quarto, you can include many things that aren’t code:
These are primarily written using Markdown syntax.
You don’t need special software to read it.
Separates out the content and structure from the document styling.
Focus on the content not the way it looks.
Sections are added to a document using different numbers of #
:
#
creates a Level 1 heading (section)
##
creates a Level 2 heading (subsection)
###
creates a Level 3 heading (sub-subsection)
…
Lists can be created using either *
or -
:
or
gives:
Works but isn’t great:
Add a link using square brackets and round brackets:
A link to the [Quarto documentation](https://quarto.org/)
.
which gives:
A link to the Quarto documentation.
If the text to appear and the URL should be the same, you can put it inside <>
instead:
… can be found at <https://quarto.org>
.
which gives:
… can be found at https://quarto.org.
Add an image using ![](path/image.png)
.
Tip
Use relative file paths rather than absolute file paths - other people won’t share the same absolute file path as you!
File paths are relative to where the Quarto document is!
Use the tab key to show files.
Add a figure caption in the square brackets:
![Figure caption goes here](path/image.png)
.
Image options can be added in curly brackets afterwards:
![Figure caption goes here](path/image.png){fig-align="center" width="50%"}
.
You can create simple tables with Markdown syntax:
Surname | Forename |
---|---|
Rennie | Nicola |
Smith | Jane |
Barclay | Rebekah |
I almost never make tables by writing out -
and |
…
You can create nice-looking tables from data files e.g. .csv, .xlsx. We’ll look at this in the next section.
The Visual Editor is much easier for writing tables.
Table –> Insert Tables:
---
title: "A very cool title"
format: html
---
\begin{equation}
Y = \beta_0 + \beta_1 \text{age} + \beta_2 \text{education}
\end{equation}
\[\begin{equation} Y = \beta_0 + \beta_1 \text{age} + \beta_2 \text{education} \end{equation}\]
sample-text.qmd
.eggs.jpg
.05:00
Code blocks are the main way of including executable R code in a document.
Code blocks always start with three backticks, followed by the an r
in curly brackets.
Tip
Add a new code block using the Ctrl + Shift + I keyboard shortcut.
Code block options can control:
Code visibility options:
label
label
to each code block.unnumbered code block 39
).label
Let’s say we want to load some data. We can add a load-data
label:
Tip
Separate label words using a -
rather than a _
.
We’ll be using the sat.act
data from {psych} for some examples. You can also load the data directly from a CSV file:
See ?psych::sat.act
.
fig-
Figure options:
fig-
tbl-
There are lots of options for creating tables in R:
kable()
in {knitr}tbl-
Prepare the data:
tbl-
Make a table:
tbl-
education | SATV | SATQ |
---|---|---|
0 | 618.0536 | 620.4286 |
1 | 600.3488 | 607.2558 |
2 | 575.4651 | 577.2093 |
3 | 611.6245 | 606.0743 |
4 | 615.9781 | 611.8540 |
5 | 622.9281 | 623.6331 |
bfi
.
education
. You can use either base R or {ggplot2} (or another package!).head(bfi[, 1:5], 10)
. Use any table package you prefer.05:00
We can also include code inline, rather than as a separate chunk.
This is especially useful for using results values in a paragraph of text.
The total number of participants is `r nrow(bfi)`
.
The total number of participants is 2800.
For more complex numbers, you can calculate or extract results from a code block.
The median age of females in the study is `r age_f`
.
The median age of females in the study is 26.
Earlier we saw how to add images to Quarto documents, where image options can be added in curly brackets afterwards:
![Figure caption goes here](path/image.png){fig-align="center" width="50%"}
.
Add a figure label with a #
:
![Figure caption goes here](path/image.png){#fig-my-image fig-align="center" width="50%"}
.
This label must start with fig-
to be a Figure label.
To reference the figure in the text, use an @
with the figure label:
@fig-my-image
.
Tip
This is basically the pattern of cross-referencing throughout Quarto:
#
give a reference label to an object.
@
refer the object’s reference in the text.
We can add a cross-reference to a different section of our manuscript by adding a reference label that has a sec-
prefix:
## Exploratory data analysis {#sec-eda}
Then we can make a reference to this section using:
… as discussed in @sec-eda
.
This automatically adds the word Section
before the relevant number.
To make sure that the cross reference numbers make sense, you should also switch on the numbering for the document sections:
Earlier we made a histogram of age:
To make this a Figure reference, we need to prefix the label with fig-
:
Then:
A histogram of participant ages is shown in @fig-age-histogram
.
gives:
A histogram of participant ages is shown in Figure 1.
Table references are similar, except we prefix with tbl-
:
education | SATV | SATQ |
---|---|---|
0 | 618.0536 | 620.4286 |
1 | 600.3488 | 607.2558 |
2 | 575.4651 | 577.2093 |
3 | 611.6245 | 606.0743 |
4 | 615.9781 | 611.8540 |
5 | 622.9281 | 623.6331 |
Then:
Summary statistics are shown in @tbl-sat-summ
.
gives:
Summary statistics are shown in Table 1.
To add citations in Quarto, we need two things:
A source bibliography with citation IDs
A way to refer citations IDs in Markdown syntax
Quarto supports bibliographies in a wide variety of formats including BibTeX and CSL
.bib
files are probably most common.
The first entry inside the curly brackets is the citation ID.
@Manual{psych,
title = {psych: Procedures for Psychological, Psychometric, and Personality Research},
author = {{William Revelle}},
organization = {Northwestern University},
address = {Evanston, Illinois},
year = {2024},
note = {R package version 2.4.1},
url = {https://CRAN.R-project.org/package=psych},
}
Tip
Find a citation for the R packages you using by running citation("pkgname")
e.g. citation("psych")
Add a bibliography to your document using bibliography
in the document YAML.
You might also choose to link the citations to the reference list.
To refer to a citation, use @
and the citation ID:
… as described in @psych.
which gives:
… as described in @psych.
Or inside brackets:
…as seen in the bfi data [@psych].
which gives:
…as seen in the bfi data [@psych].
Adding citations is one task where the Visual Editor in RStudio can be really helpful. It provides:
Integration with Zotero (no set up if using desktop versions).
Search with DOI, CrossRef, PubMed.
It updates the .bib
file for you!
Click Insert –> Citation:
05:00
Let’s assume that we’ve written a wonderful, fully-reproducible manuscript and we’re ready to submit it to our favourite journal.
Now comes the journal formatting…
Most (but not all)journals aren’t quite ready for Quarto (.qmd
) files. They accept Word documents, LaTeX files, and sometimes PDFs to start with.
How can you alter the styling of a Quarto document?
LaTeX
The template.tex
file might be provided by a journal.
But there’s an easier way…
Quarto extensions are a powerful way to modify and extend the behavior of Quarto. Extensions can be used to add styling to your documents.
Extensions aren’t just used to change document styling. There are also extensions to:
Quarto extensions can be used to install templates for journal articles.
Many templates already exist (and the list is getting longer…)
From the command line
To create a new document from a template:
To convert an existing document
Using the {quarto} R package
Version 1.4 onwards
To create a new document from a template:
To convert an existing document
APA Quarto extension: wjschne.github.io/apaquarto
Install: quarto::quarto_use_template("wjschne/apaquarto")
05:00
We often work with other people when we write papers.
How do we do that effectively using Quarto?
Caution
I don’t have perfect answers to this…
Your .qmd files will get excessively long when you start writing (especially when you include code).
This quickly gets annoying (especially with multiple people adding to a single document).
There are several options for breaking them down:
You can use {{< include doc.qmd >}}
to include one Quarto document in another.
Quarto projects are a collection of Quarto files structured in a specific way that provide:
Manuscripts
are a special type of Quarto project.
Has configuration file _quarto.yml
that identifies the project as a Quarto manuscript and controls how your manuscript is put together.
A place to store and manage versions of code.
Public or private repositories
Alternatives like GitLab and Bitbucket exist.
Why use GitHub?
Easily roll back to previous versions.
History of who added what.
Add comments and suggestions via pull requests. Ask for a review.
Can render your documents for you.
GitHub discussion: github.com/quarto-dev/quarto-cli/discussions/405
When collaborating with other people, it’s useful (sometimes crucial) that you all have:
Problem: you have R code that takes a long time to run, and you don’t want to re-run it every time you re-render the document.
Solutions:
cache: true
: store the results of cell executions so they aren’t re-executed with every document render.
freeze: true
: in project, state that computational documents should never be re-rendered during a global project render, or alternatively only be re-rendered when their source file changes.
Problem: you have R code that takes a long time to run, and you don’t want to re-run it every time you re-render the document.
Solutions:
We’ve already seen the include
shortcode.
There are other shortcodes to make your life easier.
Adding {{< pagebreak >}} adds a page break in your document (as the name suggests).
This works across multiple output formats.
```{dot}
graph G {
qmd -- Knitr;
qmd -- Jupyter;
Knitr -- md;
Jupyter -- md;
md -- pandoc;
pandoc -- HTML;
pandoc -- PDF;
pandoc -- Word;
pandoc -- more;
}
```
Would you use Quarto to write a manuscript?
What things seem difficult or less nice than what you currently use?
Is there anything that didn’t work the way you expected?
Questions?
Course website: nrennie.rbind.io/training-intro-to-quarto
Documentation: quarto.org
GitHub Discussions: github.com/quarto-dev/quarto-cli/discussions
Intro Webinar: jthomasmock.github.io/quarto-2hr-webinar
Awesome Quarto List: github.com/mcanouil/awesome-quarto
Zotero Connector (for source editor): github.com/paleolimbot/rbbt