knitr在Stan chunk上失败,缓存= TRUE

问题描述 投票:2回答:1

根据this Github issue(针对knitr v1.12标记为固定),应该可以在编织器中缓存Stan块,以便每次编织文件时都不需要重新编译Stan模型对象。

但是,使用knitr v1.20(Rt3.5.1在RStudio v1.1.463中),当第二次编织文件时(以便已经构建了缓存),我收到一个错误:

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 chunk选项中尝试使用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
r knitr rstan
1个回答
1
投票

这是knitr的一个错误,I just fixed on Github。请尝试当前的开发版本:

remotes::install_github('yihui/knitr')
© www.soinside.com 2019 - 2024. All rights reserved.