Quarto documents

Author

Steen Flammild Harsted

Published

January 10, 2024

   

1 Quarto documents

Start a new .qmd document

From the menu click: File / New File / Quarto Document 1

  • Make sure the settings are set to “Document” and “HTML” (both are default settings),
  • Uncheck ‘Use visual markdown editor’
  • Click “Create”

Create a new quarto document

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. hint.

  • Save your file (Important: file_name_matters).
  • Pres “Render” and see what happens.



2 YAML code

YAML (Yet Another Markup Language).
The YAML code is at the very top of the document. It 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 it is there and forget about it.

  • Delete everything in the document except the YAML code




3 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




4 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)
```
Lifehack

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 the iris dataset has

The iris dataset contains `r nrow(iris)` rows

or

The iris dataset contains `r iris %>% nrow()` rows

Footnotes

  1. Alternatively, click the green-plus icon (top-left icon – under File menu) and choose ‘Quarto document’ from the drop-down list↩︎