使用自定义编织函数并评估列表中引用的函数时,Knitr 未从前一个块中找到函数
Knitr doesn't find function from previous chunk when using custom knit function and evaluating quoted function in a list
我遇到过 运行 奇怪的情况,当一个块无法找到在前一个块中定义的函数时,编写 R markdown 文档失败。
我在下面隔离了其中一个(我能做到的最多),这就是它失败的原因:
- 当运行每个块手动运行时没有问题
- 仅当我在 YAML 中使用自定义编织功能时编织才会失败 header
- knitr 仅当我
eval
引用表达式 AND 在 list/map 函数(lapply
或 purrr::
中时才找不到函数
- 即如果我这样做没有问题
eval(an_eval[[1]])
可重现 Rmd 文件内容
---
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)
```
错误:
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
此时需要指定rmarkdown::render()
的envir
参数,否则默认环境为parent.frame()
,即您提供的(匿名)函数的内部环境到 YAML 中的 knit
。
---
output:
html_document
knit: (function(input, encoding) {
rmarkdown::render(
input = input,
encoding = encoding,
envir = globalenv()
)
})
---
```{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)
```
我遇到过 运行 奇怪的情况,当一个块无法找到在前一个块中定义的函数时,编写 R markdown 文档失败。
我在下面隔离了其中一个(我能做到的最多),这就是它失败的原因:
- 当运行每个块手动运行时没有问题
- 仅当我在 YAML 中使用自定义编织功能时编织才会失败 header
- knitr 仅当我
eval
引用表达式 AND 在 list/map 函数(lapply
或purrr::
中时才找不到函数- 即如果我这样做没有问题
eval(an_eval[[1]])
- 即如果我这样做没有问题
可重现 Rmd 文件内容
---
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)
```
错误:
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
此时需要指定rmarkdown::render()
的envir
参数,否则默认环境为parent.frame()
,即您提供的(匿名)函数的内部环境到 YAML 中的 knit
。
---
output:
html_document
knit: (function(input, encoding) {
rmarkdown::render(
input = input,
encoding = encoding,
envir = globalenv()
)
})
---
```{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)
```