knitr 在缓存 = TRUE 的 Stan 块上失败
knitr fails on Stan chunk with cache=TRUE
根据this Github issue(标记为 knitr v1.12 已修复),应该可以在 knitr 中缓存 Stan 块,这样 Stan 模型对象就不需要每次都重新编译我们编织文件。
但是,使用 knitr v1.20(RStudio v1.1.463 中的 R v3.5.1),第二次编织文件时(因此缓存已经建立),我得到一个错误:
Quitting from lines 9-18 (Testing_Stan_cache.Rmd)
Error in fun(environment()) : invalid first argument
Calls: <Anonymous> ... call_block -> <Anonymous> -> lazyLoad -> lazyLoadDBexec -> fun
Execution halted
示例 Rmarkdown 文件(错误中提到的第 9-18 行是 Stan 块):
---
title: "Testing Stan cache"
output: html_document
---
## Stan model
```{stan output.var="ex1", cache=TRUE}
data {
int<lower=0, upper=1> X[100];
}
parameters {
real<lower=0, upper=1> p;
}
model {
X ~ bernoulli(p);
}
```
## Run the model
```{r}
library(rstan)
fit <- sampling(ex1, data=list(X = rbinom(100, 1, 0.3)))
print(fit)
```
我也在 Stan 块选项中使用 cache.lazy=FALSE
进行了尝试,但得到了不同的错误:
Quitting from lines 23-26 (Testing_Stan_cache.Rmd)
Error in sampling(ex1, data = list(X = rbinom(100, 1, 0.3))) :
object 'ex1' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> sampling
Execution halted
这是 knitr 的错误,其中 I just fixed on Github。请试用当前开发版本:
remotes::install_github('yihui/knitr')
根据this Github issue(标记为 knitr v1.12 已修复),应该可以在 knitr 中缓存 Stan 块,这样 Stan 模型对象就不需要每次都重新编译我们编织文件。
但是,使用 knitr v1.20(RStudio v1.1.463 中的 R v3.5.1),第二次编织文件时(因此缓存已经建立),我得到一个错误:
Quitting from lines 9-18 (Testing_Stan_cache.Rmd)
Error in fun(environment()) : invalid first argument
Calls: <Anonymous> ... call_block -> <Anonymous> -> lazyLoad -> lazyLoadDBexec -> fun
Execution halted
示例 Rmarkdown 文件(错误中提到的第 9-18 行是 Stan 块):
---
title: "Testing Stan cache"
output: html_document
---
## Stan model
```{stan output.var="ex1", cache=TRUE}
data {
int<lower=0, upper=1> X[100];
}
parameters {
real<lower=0, upper=1> p;
}
model {
X ~ bernoulli(p);
}
```
## Run the model
```{r}
library(rstan)
fit <- sampling(ex1, data=list(X = rbinom(100, 1, 0.3)))
print(fit)
```
我也在 Stan 块选项中使用 cache.lazy=FALSE
进行了尝试,但得到了不同的错误:
Quitting from lines 23-26 (Testing_Stan_cache.Rmd)
Error in sampling(ex1, data = list(X = rbinom(100, 1, 0.3))) :
object 'ex1' not found
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> sampling
Execution halted
这是 knitr 的错误,其中 I just fixed on Github。请试用当前开发版本:
remotes::install_github('yihui/knitr')