根据this Github issue(对于knitr v1.12标记为固定),应该可以在Snitr中缓存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
答案 0 :(得分:1)
这是 knitr 的错误,I just fixed on Github。请尝试当前的开发版本:
remotes::install_github('yihui/knitr')