【问题标题】:knitr fails on Stan chunk with cache=TRUEknitr 在缓存 = TRUE 的 Stan 块上失败
【发布时间】:2023-04-06 21:46:01
【问题描述】:

根据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

【问题讨论】:

    标签: r knitr rstan


    【解决方案1】:

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

    remotes::install_github('yihui/knitr')
    

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2020-06-20
      相关资源
      最近更新 更多