Chapter 10 Reproducible Reports in R

10.0.1 Set - Up

Quarto lets you create reproducible code documents to help demonstrate workflow, and create templates or demos (Like the ones you have seen so far).

When you first create a Quarto document, you will see a section at the top that includes your title, format, and editor. It looks like this:

---
title: "Quarto Demo"
format: html
editor: visual
---

You can add more things to this when setting up your document! All you do is add a new line below the last line in the document header!

10.0.2 Styling

---
theme: "quartz" # {html, revealjs} ["default", "cerulean", "cosmo", ...] Name of a
                # built-in HTML Bootswatch theme, or link to a Sassy Cascading
                # Style Sheets (SCSS) file
pdf-engine: "tinytex" # {pdf, beamer} Toolchain for generating the PDF
reference-doc: "template.docx" # {docx, odt, pptx} Path to a file to use as a style reference
quarto-required: ">= 1.2.0"
---

10.0.3 Table of Contents

---
toc: true # [false, true] Include a table of contents in the document
toc-depth: 2 # Least significant section header to include in table of contents
---

10.0.4 Fonts

---
mainfont: "Baskerville" # {html, pdf, beamer} Font family for regular text
monofont: "JetBrains Mono" # {html, pdf, beamer} Font family for code
# Specify font families with CSS or LaTeX, depending upon the output format
fontsize: 14 # {html, pdf, beamer} Set the base size of the font.
---

10.0.5 Colours

---
fontcolor: "#B06500" # {html} Color of text.
linkcolor: "#007FFF" # {html, pdf, beamer} Color of link text.
---

10.0.6 Code

---
code-line-numbers: true # {html, pdf, docx, revealjs, beamer, epub} [false, true]
                        # Should line numbers be included for code?
highlight-style: "breeze" # {html, pdf, docx, revealjs, beamer, epub} ["default",
                          # "a11y", "arrow", ...] Theme for code highlighting
code-fold: "show" # {html, revealjs, epub} [false, true, show] Should code be
                  # contained in a collapsible HTML <details> block?
                  # false: Don't put code in a collapsible block
                  # true: Put code in a collapsible block; start block collapsed
                  # "show": Put code in a collapsible block; start block expanded
code-overflow: "wrap" # {html, revealjs, epub} ["scroll", "wrap"] How should long
                      # lines of code behave?
---

10.0.7 Execution

---
eval: false # [true, false] Should code cells be evaluated?
echo: false # [true, false] Include cell source code in the rendered document?
output: "asis" # [true, false, "asis"] Include the results of executing the code in
              # the rendered document?
              # "asis": Include the results, treating them as raw Markdown
warning: false # [true, false] Display warnings in the rendered document?
error: false # [true, false] Display errors in the rendered document?
include: false # [true, false] Set echo, output, warning, and error together
cache: false # [true, false, "refresh"] Cache the results of computations so
            # repeated generation of the rendered document is faster
            # "refresh": Force a refresh, even when cache has not been invalidated
---

10.0.8 Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
## [1] 2

You can add options to executable code like this

## [1] 4

The echo: false option disables the printing of code (only output is displayed).

10.0.9 Inserting Additional Elements

In Quarto, you can insert and add different formatting and elements for your text, like headings, links, tables, pictures, equations, etc.

The best thing about Quarto, is that you can do all of this by clicking the options in the tool bar above your document - no inline formatting to memorize here!

🙊 didn’t know you could do this!!!

\[ y = (a + c)/ 2 \]

\[ y = mx + b \]

what about inline??? well… \(y = mx + b\) cool right!

Here are some more equations:

\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]

\[ \sum_{i = 1}^{n}{(\bar{x} - x_i)^2} \]

\[ Y \sim X\beta_0 + X\beta_1 + \epsilon \]

\[ \frac{d}{dx}\left( \int_{a}^{x} f(u)\,du\right)=f(x) \]

Quarto is similar to rMarkdown except everything in the “code” document is formatted how you would see it rendered.

Check out the other options in the drop down menu above… “format, insert, etc!!

Insert a table!

# library(tidyverse)
# library(readr)
# library(kableExtra)
# mydata <- read.csv("class_demo_dataset.csv")
# 
# # select a few columns/rows for table demo
# tabledata <- mydata %>% 
#   select(river, date, common_name, count, total_length_mm) %>% 
#   na.omit() %>% 
#   head(10)
# 
# # if you want to have certain formatting on columns you can add that aswell
# kbl(tabledata, caption = "table with formatted column for species name") %>% 
#   kable_paper(full_width = F) %>% # add a theme to the table
#   column_spec(4, background = "blue", bold = T)

If i wanted to remove the messages when loading in packages, how would I do that?

Hint: Check out the document settings, or code chunk settings!

10.0.10 INLINE CODE

Here is an added section for how to include inline code, you’re welcome Marco :)

You can save values in your environment after you wrangle your data and refer to them within your document again later on.

# # lets create a stat value 
# total_length <- mean(mydata$total_length_mm, nr.rm = TRUE)
# total_length

Let’s find the mean total length for this class demo dataset, which is:

to specify Quarto to execute code inline, you need to include it like this (you need those ticks)

# `r total_length`

10.1 Chapter Wrap-Up

10.1.1 Chapter Terms & Definitions

Here is a summary of some of the bolded terms used throughout this chapter, refer back to this list whenever you need a refresher!