If I press the "Run All" button, the first code chunk gets executed because the R code is not evaluated for the eval chunk option. but if I knit the document none of the code chunks get evaluated as I would expect.
The same happens with quarto documents and YAML style chunk options
---
title: "test"
format:
html:
self-contained: true
editor: visual
params:
test: false
---
```{r}
#| eval: !expr params$test
#| echo: false
cat("This should not be executed")
```
```{r}
#| eval: false
#| echo: false
cat("This should not be executed")
```
I don't know if this is a bug or maybe never was implemented and I should file a feature request.
I never noticed this until you pointed it out here. I'm not sure but I think I know why this might happen:
The YAML hearder is not really part of the markdown code, but is used to provide settings to Pandoc when knitting. It can even be in a separate file. When you run the code in RStudio with Run All, you are only executing the R code. The YAML header is ignored and the params variable does not exist at that moment. It likely is interpreted as a NULL which will execute the block (try eval=NULL). When you knit however, Pandoc is called and the YAML header is interpreted and the variable params is created and passed down.
Thanks for looking into this, but params does exist when an Rmd or qmd document is executed interactively, it is even listed in the global environment afterward so the YAML has to be parsed when the code chunks are executed, consider this modified example:
---
title: "Example"
output: html_document
params:
test: FALSE
---
```{r}
# params does exist when executing interactively
(value <- params$test)
```
```{r, eval=value, echo=FALSE}
cat("This shouldn't be executed")
```
```{r, eval=FALSE, echo=FALSE}
cat("This shouldn't be executed")
```
I often use Rmd (and now qmd) documents as scripts with nice execution controls and they are not meant to be rendered (I use them just for the side effects). It would be nice to have conditional execution without the rendering.