quarto documents
Quarto allows you to create dynamic, multi-format documents. It supports the seamless integration of code, text, and visualizations, making it ideal for academic and technical writing. With Quarto, you can produce documents in various formats, including HTML, PDF, Word, and presentations. It is compatible with R, Python, and Julia, offering extensive support for data analysis and visualization. Quarto documents facilitate reproducible research by embedding executable code within the narrative, ensuring your work is transparent and replicable. Ideal for researchers, educators, and data scientists, Quarto enhances the communication of complex information.
Quarto documents can be a little overwhelming in the beginning because they combine three different programming languages in one document—R, Markdown, and YAML. It’s like the 90’s Kinder Egg commercial—three surprises in one! On this page, we will focus on most basic parts of Markdown and YAML code. The rest of the course will focus on the R code.
1 Your first quarto document
Start a new .qmd document
File -> New File -> Quarto Document ->
- Make sure the settings are set to “Document” and “HTML” (both are default settings),
- Uncheck ‘Use visual markdown editor’
- press “Create”
The document contains three types of code YAML, R, and Markdown code. Take a look at the document and see if you can recognize the sections with R code.
- Save your file (Important: file_name_matters).
- Pres “Render” and see what happens.
The YAML code is at the very top of the document. The YAML code begins with ---
and ends with ---
. It gives instructions to your computer on how it should build your document. You will learn about YAML code later. For now - notice that the YAML code is there and forget about it.
- Delete everything in the document except the YAML code
2 Markdown code
Markdown is a simple, text-based language used to style text on the web. You control the display of text by adding non-intrusive symbols: headings are made with hashtags (#), bold text with asterisks (*) or underscores (_), links with brackets and parentheses, among others. It’s quick to learn and write.
Headlines
Headlines are made using one or more # signs followed by a space
# Largest headline
## Second largest headline
### Third largest headline
##### Smallest headline possible.
- Practice making different headlines.
Bold and Italic
*text* becomes text
**text** becomes text
***text*** becomes text
- Practice writing something in italics and bold
New lines
To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return.
If you want a hard line break end your lines with a \
- Practice making line-breaks.
Markdown Code
You can more about Markdown code here
3 R code
R code chunks
Insert a code chunk in your document (Default CTRL+Alt+I
)
If pressing CTRL+Alt+I doesn’t work click Tools -> Command Palette -> and type “Chunk” in the search field
What you write inside the codechunk will be evaluated as R code.
Write 2+2 inside the Code chunk you just created, and Render the document.
Your code block should look something like this:
```{r}
2+2 ```
Run Code chunk
You don’t need to Render your document in order to see the output of your code.
Place the cursor inside the code chunk you just made and Click the green triangle (play-sign) in the right side of the code chunk.
Notice the output in the console.
R comes with many inbuilt datasets, one of them is the iris
dataset.
Plot the iris
dataset.
Insert a codechunk, and write plot(iris)
inside the codechunk.
Click the green triangle.
Your code block should look something like this:
```{r}
plot(iris) ```
pressing ALT+Enter
will run the current line or selection.
pressing CTRL+Enter
will run the current line or selection and jump to next line.
pressing CTRL+ALT+C
will run the current code chunk (instead of just the current line).
Inline R code
Inline R code allows you to incorporate the results of your R code directly into the text of a document. It is useful because it lets you weave the results of your data analysis directly into your written text, making your documents more reproducible and prevents errors if your data changes.
Inline R code is written within the text and is surrounded by backticks and includes an “r” at the start. `r 3 + 4`
will display the result 7 in the rendered document.
- Use the function
nrow()
and inline R code to write a sentence that says how many rows theiris
dataset has
The iris dataset contains `r nrow(iris)`
rows
or
The iris dataset contains `r iris |> nrow()`
rows
4 YAML code
YAML (Yet Another Markup Language) is used for configuration of files.
Basic Rules of YAML:
- Indentation:
- YAML uses indentation to represent the structure. Indentation is done with spaces, not tabs.
- Each level of indentation denotes a nested structure.
- Each level of identation is two spaces
- ‘true’ and ‘false’
- boolean values are written as
true
andfalse
. Different from R code (TRUE
andFALSE
)
- boolean values are written as
---
title: "TITLE"
subtitle: "SUBTITLE"
author: "ME"
date: today
format:
html:
toc: true
toc-depth: 2
embed-resources: true
number-sections: true
number-depth: 2
code-fold: true
code-summary: "Show the code"
code-tools: true
execute:
message: false
warning: false ---