Quarto Integration
RStudio v2022.07.1 and later includes support for editing and preview of Quarto documents.
Quarto® is an open-source scientific and technical publishing system built on Pandoc
Create dynamic content with Python, R, Julia, and Observable.
Author documents as plain text markdown or Jupyter notebooks.
Publish high-quality articles, reports, presentations, websites, blogs, and books in HTML, PDF, MS Word, ePub, and more.
Author with scientific markdown, including equations, citations, crossrefs, figure panels, callouts, advanced layout, and more.
If you are using Quarto within RStudio, it is strongly recommended that you use RStudio v2022.07.1 or a later version (the documentation below assumes you are using this build or later).
Installing Quarto
A stable release of Quarto is bundled with RStudio v2022.07.1, and later. Upgrading to new versions of RStudio in the future will also upgrade the bundled Quarto version.
To upgrade Quarto out of sync with the bundled version in RStudio, follow the directions at https://quarto.org/docs/download/
Creating new documents
Use the File > New File > Quarto Document command to create new Quarto documents, which will launch the New Quarto Document wizard:
Render and Preview
Use the Render button to preview documents as you edit them:
If you prefer to automatically render whenever you save you can check the Render on Save option on the editor toolbar.
The preview appears alongside the editor in the Viewer pane if Preview in Viewer Pane is selected:
The preview will update whenever document is re-rendered. Side-by-side preview works for both HTML and PDF output.
Controlling output and preview
RStudio provides a dropdown menu to control the format options, such as the display location of preview renders, visual content such as tables and graphics, and raw text output from code.
Control location of rendered preview
Select one of the three following options:
- Preview in Window, opening in the default browser
- Preview in Viewer Pane, displaying the entire document preview in the Viewer Pane
- (No Preview), there will not be a preview displayed when rendering
Control preview of markdown output
Select or deselect one of the following options:
- Preview Images and Equations
- Show Previews Inline
Control output of code chunks
Turn the following features on or off:
- Chunk Output Inline, code output including graphics, tables, and printed output will display inline in the source document
- Chunk Output in Console, code output will display in the console, graphics and tables in the Viewer pane
Quarto Background Job
Quarto documents are primarily rendered in RStudio with the Render button or the Render on Save option. Both of these options render the document within a Background Job which maintains a background preview web server. This provides a fresh R session for the rendered document along with a consistent URL to access the preview content.
To access and control the Quarto render, navigate to the Console pane and select the Background Jobs tab:
- The duration of the current preview web server is available in the upper-right corner
- Metadata, arguments, and output from the render process are printed and updated for each render
- The URL to the preview web server is printed at the bottom of the output
- To interrupt the render, press the red Stop button in the upper-right corner of the Background Jobs tab
Projects
To create a new project for a Quarto document or set of documents:
- Use the File : New Project… command
- Specify New Directory
- Select Quarto Project
You can use this UI to create both vanilla projects as well as websites and books. Options are also provided for creating a git repository and initializing an renv environment for the project.
Knitr engine
Quarto is designed to be highly compatible with existing R Markdown documents. You should generally be able to use Quarto to render any existing Rmd document without changes.
One important difference between R Markdown documents and Quarto documents is that in Quarto chunk options are typically included in special comments at the top of code chunks rather than within the line that begins the chunk. For example:
```{r}
#| echo: false
#| fig-cap: "Air Quality"
library(ggplot2)
ggplot(airquality, aes(Temp, Ozone)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE)
```
Quarto uses this approach to both better accommodate longer options like fig-cap
, fig-subcap
, and fig-alt
as well as to make it straightforward to edit chunk options within more structured editors that don’t have an easy way to edit chunk metadata (e.g., most traditional notebook UIs).
Chunk options included this way use YAML syntax rather than R syntax for consistency with options provided in YAML front matter. You can still however use R code for option values by prefacing them with !expr
. For example:
#| fig-cap: !expr paste("Air", "Quality")
Jupyter engine
You can also work with Quarto markdown documents that target the Jupyter engine within RStudio. These files will typically include a jupyter
option in the YAML front matter indicating which kernel to use. For example:
---
title: "Matplotlib Demo"
author: "Norah Smith"
jupyter: python3
---
To work within a virtual environment (venv):
- Use the File : New Project… command
- Specify the Jupyter engine with a venv
- Specify which packages you’d like to seed the venv with:
RStudio will automatically activate this virtual environment whenever you open the project. You can install additional Python packages into the environment using the RStudio Terminal tab. For example:
YAML intelligence
YAML code completion is available for project files, YAML front matter, and executable cell options:
If you have incorrect YAML it will also be highlighted when documents are saved:
R package
If you are not using RStudio and/or you prefer to render from the R console, you can do so using the quarto R package. To install the R package:
install.packages("quarto")
Then, to render a document:
library(quarto)
quarto_render("document.qmd")
To live preview (automatically render & refresh the browser on save) for a document you are working on, use the quarto_preview()
function:
library(quarto)
quarto_preview("document.qmd")
If you are working on a website or book project, you can also use quarto_preview()
on a project directory:
library(quarto)
quarto_preview()