Using R code for chunk option values doesn't work interactively

Does anybody know if using R code for chunk options is (or ever was) supported for interactive use in the IDE? For example:

---
title: "Example"
date: "2022-08-09"
output: html_document
params:
    test: FALSE
---

```{r, eval=params$test, echo=FALSE}
cat("This shouldn't be executed")
```

```{r, eval=FALSE, echo=FALSE}
cat("This shouldn't be executed")
```

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.

Hi,

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.

Hope this helps,
PJ

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")
```
1 Like

Hi,

You're right. It seems this topic is related to your question

Hope this helps,
PJ

Yes, possibly but it is still not clear to me whether this is a bug or simply R code evaluation for chunk options is not implemented in the IDE

I think you encounter this issue

I believe this is a bug in the sense that the knitr feature is not correctly supported by the IDE notebook feature (Run all etc...)

Do you agree this is the same issue reported ?

Yes, pretty much the same thanks!

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.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.