Code tools

html
code
Author

Mine Çetinkaya-Rundel

Published

July 13, 2022

code-tools

You can include a Code menu in the header of your document that provides various tools for readers to interact with your source code.

If you want to include the full source code (including executable code chunks and your prose, in other words, your full .qmd file), set code-tools: true in the YAML of your document.

---
format:
  html: 
    code-tools: true
---

The mlr3book uses this approach.

Alternatively, if your document lives within a project that has a specified repo-url in the project-level _quarto.yml file, you can directly link to the document in your repo by specifying the source.

---
format:
  html: 
    code-tools:
      source: repo
---

On this page I’ve done something a little different. I have some “analysis code” on my page, in the following code chunks.

First, I load some packages.

library(tidyverse)
library(palmerpenguins)

Then, I calculate some summary statistics.

penguins %>%
  group_by(species) %>%
  summarise(mean_bill_length = mean(bill_length_mm, na.rm = TRUE))
# A tibble: 3 × 2
  species   mean_bill_length
  <fct>                <dbl>
1 Adelie                38.8
2 Chinstrap             48.8
3 Gentoo                47.5

When you have a bunch of code as well as bunch of prose in your document (like here), your readers might want access to just the content in your executable code chunks so that they can easily replicate your analysis. You can do this by maintaining a separate R script, and then linking to it from your YAML, like I’ve done on this page.

---
format:
  html: 
    code-tools:
      source: https://github.com/mine-cetinkaya-rundel/quarto-tip-a-day/blob/main/posts/16-code-tools/penguins.R
---

And you have two options for generating that R script:

  • You can create and maintain it manually, making sure to keep it updated as you update the analysis in your narrative / .qmd file.
  • You can automatically generate it from your .qmd file with knitr::purl(), which “extracts the R code in it according to a list of patterns, evaluates the code and writes the output in another file”.

Learn more

Code tools