Overriding individual code chunk options

Author

Charlie Hadley

Published

June 20, 2022

I’ve just moved this entire blog from {blogdown} to {quarto}. It was mostly simple but for one of the blogposts I really wanted a way to override individual code chunk options. Let’s make a toy .Rmd document to demonstrate this:

---
output: html_document
---
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval=FALSE)
```


This code chunk has `eval=TRUE` which I want to override

```{r, eval=TRUE}
2 + 2
```

I tweeted about this and Garrick Aden-Buie replied linking me to the documentation for option hooks. I was really pleased to then figure out this solution:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_hooks$set(force_not_eval = function(value) {
  value$eval <- FALSE
  value
}
)
knitr::opts_chunk$set(force_not_eval = TRUE)
```

```{r, eval=TRUE}
2 + 2
```

Reuse

Citation

BibTeX citation:
@online{hadley2022,
  author = {Charlie Hadley},
  title = {Overriding Individual Code Chunk Options},
  date = {2022-06-20},
  url = {https://visibledata.co.uk/posts/2022-06-20_overriding-individual-code-chunk-options},
  langid = {en}
}
For attribution, please cite this work as:
Charlie Hadley. 2022. “Overriding Individual Code Chunk Options.” June 20, 2022. https://visibledata.co.uk/posts/2022-06-20_overriding-individual-code-chunk-options.