10  Code chunks

These are my notes on working with code chunks.

Here are some other helpful resources:

10.1 Code Run Time

We were interested in understanding how much caching code chunks could potentially speed up the rendering process for books. Initially, the code to test out caching code chunks was part of the test_quarto_book project. However, it seemed like the quarto_render() function works differently in the context of a book project and was making testing difficult. Specifically, quarto::quarto_render("slow_code_default.qmd") seemed to render the entire book instead of just the slow_code_default.qmd document. Therefore, we created a separate Quarto project to run these tests – test_quarto_cached_chunks.

The bottom line is that caching actually causes the render to take longer to process on the first run. However, it appears to speed up subsequent renders. This makes sense.

10.2 Naming code chunks

Use the label code chunk option. For example, #| label: slow-code-cached

10.3 Showing entire code chunks

When you want to display and entire code chunk in the rendered output, as opposed to executing the code chunk, surround it with four backticks.

Also, make sure to use double curly braces {{}} around the language name identifier. If you don’t, funky looking code will be output.

````
```{r}
#| A code chunk for display
#| eval:false
1 + 1
```
````