Banner image: Photo © Cingular | Shutterstock
This document is based on teaching material originally developed for the Bren School of Environmental Science & Management’s Master of Environmental Data Science (MEDS) program. Check out Creating your personal website using Quarto, Customizing Quarto websites, and Adding a blog to your existing Quarto website for additional information and examples.
What is Quarto?
Quarto is a publishing system built on Pandoc that allows users to create dynamic content using R, Python, Julia, and ObservableJS (with plans to add more languages too!).
R users have long loved RMarkdown for combining prose, code, and outputs into single “knitted” documents. Quarto extends all of RMarkdown’s best features (plus many more!) to additional languages.
.rmd
vs. .qmd
files
If you’re already an avid RMarkdown user, great news! RMarkdown (.rmd
) and Quarto Markdown (.qmd
) files look super similar:
- document-level metadata and configurations are included in the document’s YAML (denoted by the
---
gates at the top of the document) - code is written inside executable code chunks
- prose is written in the body of the document
There are some slight differences to be aware of:
- some YAML option names differ between the two document types (e.g.
output
in.rmd
vs.format
in.qmd
) - chunk-level execution options are are written within with code block braces (e.g. ```
{r echo=FALSE}
) in.rmd
files, and written below code block braces following hash pipes,|#
(e.g.|# echo: false
) in.qmd
files - booleans are capitalized in YAML and chunk-level metadata in
.rmd
files (e.g.FALSE
) and lowercase in.qmd
files (e.g.false
) - you Knit
.rmd
files and Render.qmd
files to convert your work to your desired output type (e.g..html
)
They also look pretty similar when knitted/rendered. Below is a side-by-side comparison of a knitted .rmd
file and a rendered .qmd
file (both as .html
files):
Art by Allison Horst. Be sure to check out the rest of Allison’s seriously cute Quarto penguin art in the #rstudioconf2022 keynote talk, Hello Quarto, by Julie Lowndes & Mine Çetinkaya-Rundel!
You can explore Quarto’s documentation to learn more about creating documents, websites, blogs, books, slides, dashboards, etc.
Create the scaffolding for your website
To follow along, you’ll need:
- R & RStudio installed
- Quarto installed – Quarto is now included with RStudio v2022.07.1+ i.e. no need for a separate download / install if you have the latest version of RStudio
- A GitHub account
- Git installed / configured
Please refer to the MEDS Installation Guide for detailed setup instructions (follow steps 1-7).
This section reviews the “create remote (i.e. GitHub) repo first, then clone repo to your local machine to add Quarto website scaffolding files second” workflow. If you need / want to instead use the create a local repo with Quarto website scaffolding files first, then create upstream remote (GitHub) repo second workflow, check out these instructions.
Steps:
Navigate to the GitHub profile or organization where you want your website repository (repo) to live (this will likely be the OHI-Science GitHub organization, but certainly doesn’t have to be).
Create and clone a GitHub repository following the instructions, below. Importantly, choose your repo name carefully, as it’ll become a part of the website’s URL, once deployed (e.g. a repo named
my-awesome-repo
that lives in theOHI-Science
organization will have a base URL, https://OHI-Science/my-awesome-repo).
Create a GitHub (aka remote) repo:
- Click on Repositories tab in the top menu bar
- Click on the green New repository button in the upper right-hand corner
- Give it a name, and optionally, a short description. Select the radio button for Public repo (it should be selected by default). DO NOT initialize the repo with a README or
.gitignore
(we can add these later). Click Create repository. - Copy your repo’s URL to your clipboard (currently, it’s an empty repository)
Clone your repo:
- Open up RStudio
- Click on the drop down menu next to the R project icon (top right corner)
- Click New Project… > Version Control > Git > paste your repo’s URL in the Repository URL: box and press Tab to auto fill the Project directory name field (it should automatically be named the same as your GitHub repo). Browse to select where on your computer you’d like your repo to be saved.
- Click Create Project
- Add Quarto website scaffolding files by running the following in your RStudio Terminal. Doing so will create the essential default website files (
_quarto.yml
,.gitignore
,index.qmd
,about.qmd
,styles.css
):
quarto create-project --type website
- Preview your site by typing
quarto preview
in your RStudio Terminal. This will launch a preview of your website in a browser window. So long as you leave the preview running, it will update each time you make and save changes to website files (which makes iterating on your work really easy!). By default, your repo name (here, my repo name isOHI-methods-EXAMPLE
) will appear as both your navbar and landing page titles, alongside some other placeholder text. A second navbar page, titled “About”, is created for you as well.
Previewing our website in the browser by running quarto preview
in the RStudio Terminal
A quick tour of our website files
When you create a new Quarto website, a handful of files are generated by default:
index.qmd
renders your website’s landing page. Whatever you add to this file will be the first content visitors see when they visit your site. Update the content ofindex.qmd
(or any other website page) using markdown and / or HTML (you can mix and match both on the same page), add and execute code chunks and embed outputs, etc.
index.qmd
This is the default / expected name given to website landing / home pages. If you change the name of this file, you risk breaking your (eventual) deployment.
about.qmd
is a regular ’ole website page. You’re able to change both the name of this file (e.g. changeabout.qmd
tomy-new-name.qmd
) and / or the title of the file by updating its YAML – by default, the YAML only includes a title:
about.qmd
---
: "About"
title---
YAML is a human-readable data serialization language, which is commonly used for creating configuration files. Quarto recognizes lots of different YAML options for controlling the appearance and behavior of your individual website pages. YAML is always written at the top of a .qmd
file and is denoted by a pair of “gates”, ---
.
_quarto.yml
is your website configuration file. Any document rendered within your project directory will automatically inherit the metadata defined in this project-level configuration file (though you can control metadata on a page-by-page basis by making edits to an individual page’s YAML, which will override any options specified in_quarto.yml
). Importantly, this is where you define your website’s structure (e.g. your navbar, sidebar, footer, etc.). By default, your file should look similar to this:
_quarto.yml
:
project: website
type
:
website: "OHI-methods-EXAMPLE"
title:
navbar:
left- href: index.qmd
: Home
text- about.qmd
:
format:
html: cosmo
theme: styles.css
css: true toc
styles.css
is a stylesheet, where you can write CSS rules to alter the appearance of your website. We’ll actually create and use a different type of stylesheet (called a “sassy css file”,.scss
) in a bit..gitignore
is a place where we can specify any files that we don’t want Git to track (i.e. that we want Git to ignore). This is not a “Quarto thing,” but rather a valuable file that lives insidegit
directories. One common use is to add any large data files that you don’t want to accidentally push to GitHub (GitHub isn’t designed to handle LARGE files).The
_site/
directory is where all of your rendered HTML (and other important) files live. When you render your site, Quarto takes all of your.qmd
files and converts them to.html
files, and saves them to this folder (which is important because web browsers don’t know how to read.qmd
files, but do know how to read.html
files). We’re actually going to change the name of this folder once we configure our website for deployment, but_site
is the default name that Quarto uses (we can leave as-is, for now). You don’t want to physically edit or move any files inside this directory (if you want to make a change to your website, update the.qmd
or_quarto.yml
file, then re-render).
Additional _quarto.yml
configurations
The _quarto.yml
file is a place to make lots of additional updates to website appearance / configurations. Here, we’ll review just a few places to make easy and valuable updates, but be sure to check out the Quarto documentation for additional available tooling options.
Updating website title, table of contents, and page layout
index.qmd
) before continuing on!
Before we make updates to _quarto.yml
, let’s add some background information to our landing page, index.qmd
(the below text was copied directly from Ocean Health Index 2022: Methods). We can also update our page title
to something more informative than our repo name (which is set by default):
---
title: "Ocean Health Index 2024: Methods"
---
## Introduction to the global Ocean Health Index (OHI) assessment
The global Ocean Health Index assesses ocean health for 220 coastal countries and territories and has been conducted every year starting in 2012. The Index describes how well we are sustainably managing 10 goals for ocean ecosystems which represent the full suite of benefits that people want and need from the ocean. These goals include: artisanal fishing opportunity, biodiversity, carbon storage, clean waters, coastal livelihoods and economies, coastal protection, food provision, natural products, sense of place, and tourism and recreation. Each goal is given a score ranging from 0 to 100, and the full suite of goal scores are then averaged to obtain an overall index score for each region.
<http://ohi-science.org/ohi-global/>, which includes information about downloading global ocean health data. For more information about the philosophy of the Ocean Health Index and model development see Halpern et al. (2012, 2015) and
The website should look similar to this, when previewed:
Our new landing page, with an updated title and text.
A few adjustments to our _quarto.yml
can improve our website’s appearance:
_quarto.yml
:
project: website
type
:
website:
sidebar1: "OHI Methods (2024)"
title: "docked"
style: true
search:
contents- href: index.qmd
: Home
text- regions.qmd
- section: Models
:
contents- index-scores.qmd
- goal-scores.qmd
- section: Likely future status dimensions
:
contents- trend.qmd
- pressure.qmd
- resilience.qmd
- references.qmd
:
format:
html: cosmo
theme: styles.css
css2: false
toc3-layout: full page
- 1
-
Update the website
title
- 2
-
Remove Table of Contents from all pages (though you can enable TOC on a page-by-page basis by setting
toc: true
in the YAML of individual.qmd
files) - 3
- Allow text / content to fill the entire page – read more on page layout options
After these three small updates, our website now looks like this:
Adding a logo
Quarto supports images in the navbar / sidebar (though note that getting the sizing correct in a navbar can be a bit tricky and may require writing some CSS). Provide an image to the logo
option in _quarto.yml
.
You can download the OHI logo, but also feel free to practice using any image you have easily accessible.
_quarto.yml
:
project: website
type
:
website:
sidebar1# title: "OHI Methods (2024)"
2: media/OHI-logo.png
logo: "docked"
style: true
search:
contents- href: index.qmd
: Home
text- regions.qmd
- section: Models
:
contents- index-scores.qmd
- goal-scores.qmd
- section: Likely future status dimensions
:
contents- trend.qmd
- pressure.qmd
- resilience.qmd
- references.qmd
:
format:
html: cosmo
theme: styles.css
css: false
toc-layout: full page
- 1
- I decided to remove the website title, but you can include both a title and logo (the title will print beneath the logo)
- 2
-
I saved my logo to a folder called
media/
Build & publish your website using GitHub Pages
There are a lots of options to publish your website. We’ll use the GitHub Pages option, which allows you to publish a website from any GitHub repository. To do so, there are a few configuration steps:
Create a file named
.nojekyll
in your repository’s root directory (e.g.OHI-methods-EXAMPLE/
), which is required to disable some processing of HTML files that GitHub does by default. There are two ways you can do this:- In your RStudio Terminal, type the following command:
touch .nojekyll
- From RStudio’s File pane, click New Blank File > Text File, then type in
.nojekyll
.
.nojekyll
file)
The touch
command can be used to create a new, empty file from the command line. Similarly, RStudio’s Text File button allows you to define and create any file type. Since there’s no default button in RStudio for creating a .nojekyll
file, you’ll want to use one of these two approaches.
.nojekyll
is a hidden file which won’t visibly appear in your directory. You should see it show up as a file to track with git (either under the Git tab in RStudio, or when you run git status
in the command line). You can also view hidden files in Finder (Mac) using the keyboard shortcut Command
+ Shift
+ .
, or follow these instructions for Windows 10, 8.1, and 7.
- Set the
output-dir
in your_quarto.yml
file todocs
(it’s easiest to open and edit this from RStudio):
_quarto.yml
:
project: website
type-dir: docs
output
# ~ additional metadata excluded for brevity ~
output-dir
(here, that’s the docs/
folder)
The output-dir
is the directory (i.e. folder) where your rendered .html
(and other important) files will automatically be saved to when you “Build” your website (see the next step!) – that is, when you convert all your .qmd
files to the .html
files that your web browser can interpret / display.
_site/
if it exists and if you’re publishing with GitHub Pages
If you previewed or built your site before setting output-dir
to docs
in _quarto.yml
, you’ll notice a _site/
directory inside your repository – this is the default output directory name. Because GitHub Pages will expect a docs/
folder to deploy from, you can delete _site/
altogether (and push your deletion, if you’ve already committed / pushed _site/
to GitHub).
- Build / render your website. Click on the Build tab (top left pane in RStudio if you have the default layout), then Render Website (alternatively, you can run
quarto render
in the Terminal) – this command creates the final version of your website documents for deployment. Once rendered, you should see your soon-to-be website appear in the Viewer tab.
If you don’t see the Build tab, close then reopen your project. It should appear!
Clicking Render Website in the Build tab (or running quarto render
in the Terminal) is a necessary pre-(re)deployment step – it converts all .qmd
files to .html
and ensures that all website components are stitched together correctly. If you do not render your website before pushing your files, your changes will not deploy.
Send all of your website files from your local git repository to your remote GitHub repository:
- Stage your files by checking all the boxes in the Git tab (this is analogous to the
git add .
command used in the Terminal for staging all files) - Commit your files by clicking the Commit button, adding a commit message, and clicking “Commit” (analogous to
git commit -m "my commit message"
) - Push your files to the remote repository (on GitHub) by clicking the “Push” button with the green upward facing arrow (analogous to
git push
).
- Stage your files by checking all the boxes in the Git tab (this is analogous to the
Configure GitHub pages to serve content from the “docs” directory by clicking on the Settings tab in the top menu bar, then the Pages tab from the left-hand menu bar. Make sure that Branch is set to
main
and that the selected folder is set to/docs
. Click Save. Once deployed (this may take a few minutes), your website’s URL will appear inside a box at the top of the page (you may have to try refreshing a few times).
See deployment status, time of each deployment, and how long it took to deploy each run. You can also find failed deployments here (yes, it does happen on occasion) and take action on fixing them.
You’re now ready to continue iterating on your website and redeploy with ease! Your workflow will look something like this:
- Make updates to your website in RStudio (or IDE / text editor of choice)
- Re-render your website by either (a) clicking Render Website in RStudio’s Build tab, or (b) running
quarto render
in the Terminal - Push all of your files to GitHub where GitHub Pages will re-deploy your site with your latest changes
Updating colors & fonts using Sass
There are a number of ways to update a Quarto website’s theme (e.g. colors, fonts), but the (arguably) fastest and easiest way to get started is by defining some Sass variables in a .scss
file (stylesheet), then linking to that stylesheet in your _quarto.yml
metadata. The steps below offer a “quick start guide approach,” though I recommend checking out these workshop materials on customizing Quarto websites using Sass & CSS for a much more thorough / deeper dive.
Create and apply a .scss
file / stylesheet
Create a
styles.scss
file in your repo’s root directory (you can name it however you’d like), using the commandtouch styles.scss
in the Terminal, or by Blank File > Text File > typingstyles.scss
from the RStudio Files pane.Add the
/*-- scss:defaults --*/
region decorator to the top ofstyles.scss
(required by Quarto) – you’ll write all your Sass variables underneath this:
styles.scss
/*-- scss:defaults --*/
- Apply your
styles.scss
stylesheet to your website using thetheme
option in_quarto.yml
:
_quarto.yml
:
project# ~ additional YAML omitted for brevity ~
:
website# ~ additional YAML omitted for brevity ~
:
format:
html1: styles.scss
theme: false
toc-layout: full page
- 1
-
Apply your
styles.scss
stylesheet using thetheme
option
css: styles.css
from _quarto.yml
This tutorial doesn’t cover writing CSS, though you can (and should) write any CSS in your .scss
file (rather than writing it in a separate .css
file). I recommend removing styles.css
from _quarto.yml
. You can also delete styles.css
from your repo (you can always create a new one if you decide you want / need a .css
file in the future).
Update website colors
- Define some color variables using the syntax,
$variable-name: value;
(you get to decide what these variable names are!). Here, we define three colors:
styles.scss
// Colors
$baby-blue: #E3F1F2;
$dark-blue: #252D5C;
$orange: #F26C25;
//
for single line comments in .scss
files
You may also create multi-line comments, which start with /*
and end at the next */
.
- Use your color variables as values for Quarto’s Sass variables. Below, we specify only four, however there are a number of Sass variables which can be defined within SCSS files (find a list of them here):
styles.scss
/*-- scss:defaults --*/
// Colors
$baby-blue: #E3F1F2;
$dark-blue: #252D5C;
$orange: #F26C25;
// Sass vars
$body-bg: $baby-blue;
$body-color: $dark-blue;
$link-color: $orange;
$footer-bg: $dark-blue;
Your newly styled site should look similar to this (be sure to run quarto preview
in the Terminal, if it’s not already running).
Update website fonts
- Find a Google font family by browsing options at https://fonts.google.com/. There are so many options – click on the Filter button in the top left corner of the page to help narrow your choices. For this exercise, we’ll go with Nunito (my personal favorite).
- Select a Google font family(ies) by clicking the blue Get Font button in the top right corner of the page, which adds your font family to your “bag.” You can add as many font families to your bag as you’d like to import, though we’ll stick with just one (Nunito) for this exercise. Click Get embed code, then choose the @import radio button (beneath the Web menu option), which will provide your import code chunk. Copy everything between the
<style> </style>
tags (starting with@import
and ending with;
) to your clipboard.
- Import your Google font by pasting the import code into
styles.scss
(I always place this at the top of my stylesheet, beneath/*-- scss:defaults --*/
):
styles.scss
/*-- scss:defaults --*/
// Import Google fonts
@import url('https://fonts.googleapis.com/css2?family=Nunito:ital,wght@0,200..1000;1,200..1000&display=swap');
// Colors
$baby-blue: #E3F1F2;
$dark-blue: #252D5C;
$orange: #F26C25;
// Sass vars
$body-bg: $baby-blue;
$body-color: $dark-blue;
$link-color: $orange;
$footer-bg: $dark-blue;
- Apply your font using the
mainfont
option in_quarto.yml
– this is the easiest way to use a single font across your entire website:
:
project# ~ additional YAML omitted for brevity ~
:
website# ~ additional YAML omitted for brevity ~
:
format:
html: styles.scss
theme: false
toc-layout: full
page1: Nunito mainfont
- 1
- Be sure to spell the font family name exactly as it appears on Google Fonts
Our updated website, with Nunito applied.
But this requires a bit more Sass and CSS. While we won’t get into that here, check out these workshop materials and some “in the wild” examples (e.g. like the stylesheet for this very instructional document ).
GitHub Pages will then rebuild and redeploy your site at your URL.
Additional resources & tips
- New to Markdown or need a refresher? Review the most commonly used markdown syntax.
- Quarto will automatically generate citations and a bibliography in a number of styles(!!) – check out the official documentation. I also provide some examples, screenshots, and explanations in this blog post.
- Take inspiration from other Quarto websites (and explore their source code)! See something you want to recreate but don’t know how? Check out the GitHub repo (which is often linked somewhere on the website). Here are a couple other Quarto websites I’ve created, which take advantage of custom layouts and other fun Quarto features: