---
title: "Quarto document formatting"
author: "_Mason Stahl_ (ENS-215)"
date: "2026-03-05"
date-format: "MMMM D, YYYY"
format:
  html:
    embed-resources: true
    code-fold: show
    code-tools: 
      source: false
    df-print: paged
    theme:
      light: journal
      dark: darkly
    page-layout: full
    toc: true
    toc-float: true
---

<br/>

You have already learned how to do basic formatting of R Quarto documents and you can also take a look at the **.qmd** files that I post with each lecture as this allows you to see my code and exactly how I format my R Quarto documents.  

Below are just a few basics that you will likely use when formatting your final project.  

<br/>


### Suppress messages
When loading in libraries in your final project you can specify `message = FALSE` so that the messages that print when you typically load in a library are not displayed in your knit html document.  This makes you document look nicer.

Chunk options are specified right at the top of your code chunk -- directly within the `{r}` portion.  Thus to when specifying `message = FALSE` the top of your code chunk would look like:

> `{r message = FALSE}`


Below, I've loaded in `tidyverse` and suppressed the messages.
```{r message = FALSE}
library(tidyverse)
```

<br/>

### Suppress warnings
Sometimes when you run code you will get warnings that inform you of something to be aware of (e.g. points not shown in plots or values dropped).  It is good to be aware of these warnings and make sure that they are not actually a problem with respect to your analysis.  However, oftentimes the warning, while useful to be aware of, is not actually a problem and thus you do not want to see it in your final knit document.  To suppress warning when your code is knit yo use the `warning = FALSE` chunk option.  

> `{r warning = FALSE}`

```{r warning = FALSE}

```


<br/>


### Hide code though run and display results
`echo = FALSE` hides the code in your knit document, however the code is still run.  

> `{r echo = FALSE}`

```{r echo = FALSE}

```

<br/>


### Multiple chunk options

You can combine multiple **chunk options**  

> `{r message = FALSE, echo = FALSE}`

```{r message = FALSE, echo = FALSE}

```



<br/>


### Additional chunk options
There are a number of other chunk options available.  The table below shows these options (_adapted from R4DS_)

| Option 	| Run code 	| Show code 	| Output 	| Plots 	| Messages 	| Warnings 	|
|-------------------	|----------	|-----------	|--------	|-------	|----------	|----------	|
| `eval = FALSE` 	| N 	| Y 	| N 	| N 	| N 	| N 	|
| `include = FALSE` 	| Y 	| N 	| N 	| N 	| N 	| N 	|
| `echo = FALSE` 	| Y 	| N 	| Y 	| Y 	| Y 	| Y 	|
| `results = "hide"` 	| Y 	| Y 	| N 	| Y 	| Y 	| Y 	|
| `fig.show = "hide"` 	| Y 	| Y 	| Y 	| N 	| Y 	| Y 	|
| `message = FALSE` 	| Y 	| Y 	| Y 	| Y 	| N 	| Y 	|
| `warning = FALSE` 	| Y 	| Y 	| Y 	| Y 	| Y 	| N 	|



<br/>

### Display tables and figures later in a document
If you want to generate a table or figure -- but want to have it displayed later in the document, you can save the figure or table to an R object

> `fig_1 <- ggplot(...) + ...`

> `table_1 <- code that generates your table`


Then later on in the document you can display the figure or table by simply typing `fig_1` or `table_1` in your code chunk  


<br/>  

### Additional features
To explore the many formatting and output options for R Quarto documents you should look at the [**Quarto guide**](https://quarto.org/docs/guide/).  This guide is an excellent resource that provides a range of easy to follow examples that will allow you to create professionally formatted documents from your qmd files.    

In particular take a look at the [Markdown Basics](https://quarto.org/docs/authoring/markdown-basics.html), which covers formatting Quarto document output (which is what we have been doing in this class) when rendering your qmd file.  This chapter shows you how to implement many useful formatting features and I strongly recommend you look through this chapter and apply features that you think will be helpful.  The section on [HTML basics](https://quarto.org/docs/output-formats/html-basics.html) and [HTML Code Blocks](https://quarto.org/docs/output-formats/html-code.html) will also be useful.  

Below I mention a few notable document features that you might be interested in (though consult the abovementioned resources for a wide range of options).  

You should also consult your [R Quarto cheatsheet](https://stahlm.github.io/ENS_215/Resources/cheatsheets_2026/R_cheatsheets_all_2026.pdf), which has a bunch of helpful examples for formatting your documents (e.g., including equations, adding images, text formatting, lists, footnotes,...).  

<br/>  

#### Themes
Themes set the style of the document (e.g., font type, font colors, section header styles, headers/footers,...).  There are a number of built-in themes, which you can [see here](https://bootswatch.com/3/) and you can see how to set these themes in your qmd file by following the examples [here](https://quarto.org/docs/output-formats/html-themes.html).  

<br/>  

#### Table of contents
You can add a table of contents (which I add to all of our lecture documents) by modifying the YAML header at the top of your qmd file.  Take a look [here](https://quarto.org/docs/output-formats/html-basics.html#table-of-contents) for details.  


<br/>  

#### Nicely formatted tables in R Quarto
If you are including tables of output/results in your knit document, you should consider using the `kable` functionality and `kableExtra` package.  These functions/packages allow you to control table formatting in your HTML file (e.g., fonts, colors, table dimensions, interactive/responsive tables,...).  You can find a [tutorial on these packages here](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html).   

