Error in knit( tangle = TRUE) when using eval chunk options

I'm using targets to render a Quarto report, which contains some eval chunk options that are based on variables created during the pipeline execution. During the execution, I started getting some errors that the variable was not found. Initially I though it was a problem with targets and started a discussion (inconsistency when using target object as input to quarto chunk options · Discussion #137 · ropensci/tarchetypes · GitHub). But on further exploration, it looks like it's a problem when issuing knitr::knit( tangle = TRUE).

Bellow is a small reprex:
report_eval_test.qmd

---
format: html
---

```{r}
choice <- TRUE
```


```{r}
#| eval: !expr choice
1 + 1 == 2
```

```{r}
#| eval: !expr (!choice)
1 + 1 == 3
```

If I issue knitr::knit('report_eval_test.qmd', tangle = TRUE) I get:

processing file: report_eval_test.qmd
  |......................................................                           |  67%
Error in eval(x, envir = envir) : object 'choice' not found
  |.................................................................................| 100%
Error in eval(x, envir = envir) : object 'choice' not found

output file: report_eval_test.R

Am I doing something wrong? It's important to notice that rendering this document in RStudio (render button) works just fine.
I just updated knitr to v1.42 and the issue remains

> sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C               LC_TIME=pt_BR.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.42

loaded via a namespace (and not attached):
[1] compiler_4.2.2 tools_4.2.2    yaml_2.3.7     xfun_0.36   

Thanks

I've noticed similar issues with quarto chunks not recognizing what should, to my way of thinking, should be in .Global. My workaround was to put everything in the setup chunk to create objects that could be called in later chunks. Been a while so I'm sure neither that this still works or is necessary.

Please do read our FAQ for next time FAQ: How to Format R Markdown Source

tangle for qmd file is only supported since knitr 1.42 - Are you using this one ? please do update otherwise, and check it is working ok.

Thank you

Thanks for the FAQ link. I fixed it now. Also, updated knitr to 1.42 but the issue remains. I edited the question to reflect the changes.

Thanks I understand the issue now. I thought it was qmd but it is just not support by knitr even without qmd.

See knitr::purl fail with parametrized Rmd when params is used in chunk option · Issue #1938 · yihui/knitr · GitHub

Variable can't be used in chunk header with purl (tangle = TRUE) because it does not evaluate code

Workaround when execution is needed is to use hook_purl() -See help page

---
format: html
---

```{r, purl = FALSE}
knitr::knit_hooks$set(purl = knitr::hook_purl)
```


```{r, purl = FALSE}
choice <- TRUE
```

```{r}
#| eval: !expr choice
1 + 1 == 2
```

```{r}
#| eval: !expr (!choice)
1 + 1 == 3
```

Doing knitr::knit("test.qmd") will get you a .md and a .R file with the desired extracted code.

Thanks. I see that the target dev has participated on that github issue you mentioned (knitr::purl). So it looks like I've stumbled on some know issues.

I can get around this by including a call to the variable in the chunk tar_read(variable) instead.

But what caught my attention is that it's inconsistent with the #| echo: chunk options, that works with variables.

---
format: html
---

```{r}
choice <- TRUE
```

```{r test1}
#| echo: !expr choice
# show this
```

```{r test2}
#| echo: !expr (!choice)
# don't show this
```

```{r test3}
#| eval: !expr choice
1 + 1 == 2
```

```{r test4}
#| eval: !expr (!choice)
1 + 1 == 3
```

Only chunks test3 and tes4 will give error on tangle = TRUE.

This topic was automatically closed 21 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.

Sorry for the delay.

I am not following. What is the inconsistency ?

It seems the example you shared is similar with what we discussed above and limitation of purl function