I've been running into strange cases where knitting an R markdown document fails when a chunk is unable to find a function that was defined in a previous chunk.
I isolated one of them below (the most I could) and this is how it fails:
- runs with no problem when running each chunk manually
- knitting only fails if I use the custom knit function in the YAML header
- knitr only fails to find the function when I
eval
a quoted expression AND within a list/map function (lapply
orpurrr::
)- i.e. there's no issue if I do
eval(an_eval[[1]])
- i.e. there's no issue if I do
Reproducible Rmd file contents
---
output:
html_document
knit: (function(input, encoding) {
rmarkdown::render(
input = input,
encoding = encoding,
output_file = 'a_file.html'
)
})
---
```{r}
library(knitr)
```
```{r define_function}
a_function <- function() return("a function")
```
```{r runs_fine}
a_function()
```
```{r this_fails}
an_exprs <- list(quote(a_function()))
lapply(an_exprs, eval)
```
Error:
Quitting from lines 26-28 (Debug.Rmd)
Error in a_function() : could not find function "a_function"
Calls: <Anonymous> ... withVisible -> eval -> eval -> lapply -> FUN -> FUN
Execution halted
Session:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.28