Dynamic documents with code

ANU BDSI
workshop
Reproducible research with Quarto

Emi Tanaka

Biological Data Science Institute

12th April 2024

Current learning objective

  • -Generate HTML, PDF, or Word documents using Markdown syntax
  • -Understand the anatomy of Quarto documents
  • Develop dynamic documents containing code (in R, Python, or Julia) using Quarto
  • Implement various code chunk options to customize chunk behaviour
  • -Recognize the significance of reproducibility and grasp the concept of literate programming
  • -Apply reproducible practices
  • -Establish an organized folder structure for data projects

Code chunk

Chunk options

```{r}
#| label: fig-plot
#| eval: true
#| echo: true
#| fig-width: 4
#| fig-height: 4
#| fig-cap: "A scatter plot of speed and distance."
library(ggplot2)
ggplot(cars, aes(speed, dist)) + geom_point()
```
Figure 1: A scatter plot of speed and distance.

Quarto (and R Markdown) is not just for R

  • To use Python, change the language to python:
```{python}
2 * 2 + 3
```
7
  • To use Julia, change the language to julia:
```{julia}
3 + 3
```
6

🔍 Open and inspect the file example-04-code.qmd

Inline code

Inline R code

`r some_r_code()`


The number of observations in the `ChickWeight` dataset 
is **`r nrow(ChickWeight)`**.

The value of $\pi$ is `r pi`.



The number of observations in the ChickWeight dataset is 578.

The value of \pi is 3.1415927.


  • Note that these inline R command only work if engine: knitr.
  • This doesn’t work for other languages.

Cross references

Bibliography

  • BibTeX citation style format is used to store references in .bib files.
  • You can get most BibTeX citation for R packages citation function.
To cite ggplot2 in publications, please use

  H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
  Springer-Verlag New York, 2016.

A BibTeX entry for LaTeX users is

  @Book{,
    author = {Hadley Wickham},
    title = {ggplot2: Elegant Graphics for Data Analysis},
    publisher = {Springer-Verlag New York},
    year = {2016},
    isbn = {978-3-319-24277-4},
    url = {https://ggplot2.tidyverse.org},
  }

Citing literature

---
bibliography: bibliography.bib
---

[@bibtex-key]

@bibtex-key

(Author et al. 2019)

Author et al. 2019

Figure references

```{r}
#| label: fig-scatter
#| fig-cap: "The scatter plot of speed and distance."
library(ggplot2)
ggplot(cars, aes(dist, speed)) + geom_point()
```
  • Above figure can be referenced in text as @fig-scatter.
  • The chunk label has the prefix fig-.

Table references

```{r}
#| label: tbl-cars
#| tbl-cap: "The first few rows of the `cars` dataset."
knitr::kable(head(cars), booktabs = TRUE)
```
  • Above table can be referenced in text as @tbl-cars.
  • The chunk label has the prefix tbl-.

Section references

If you have enabled numbering of sections:

---
number-sections: true
---

then you can refer to them by their label!


## Introduction

To see the main results, go to @sec-results.

## Results {#sec-results}

My @tbl-results show that the method works. 

🔍 Open and inspect the file example-05-cross-references.qmd

Download the ref.bib file and place in the same location as the above file.

Stack components as you like!