# Failure compiling Stan model chunk in .Rmd

**URL:** <https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280>\
**Category:** Interfaces\
**Tags:** cmdstanr\
**Created:** [August 6, 2020, 2:48pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280 "2020-08-06T14:48:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nerutenbeck](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nerutenbeck/32/4975_2.png) [@nerutenbeck](https://discourse.mc-stan.org/u/nerutenbeck)\
**Post date:** [August 6, 2020, 2:48pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/1 "2020-08-06T14:48:59Z")

</div>

Hi there!

I’m trying to use the exciting new feature of compiling a Stan model from an Rmarkdown chunk with cmdstanr using `cmdstanr::register_knitr_engine()`.

When I try to compile interactively in RStudio, however, running the chunk results in the error:

```
R symbol not found

```

Strangely, it works fine when I render the file (e.g. with the “Knit” button in RStudio or via `rmarkdown::render()`) rather than execute the chunk in an interactive session.

I’ve attached a .txt version of the .Rmd, and here is the output of `sessionInfo()`:

```
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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] cmdstanr_0.1.1

loaded via a namespace (and not attached):
 [1] digest_0.6.25 R6_2.4.1 jsonlite_1.7.0 backports_1.1.8 posterior_0.1.2 magrittr_1.5   
 [7] evaluate_0.14 rlang_0.4.7 stringi_1.4.6 checkmate_2.0.0 rmarkdown_2.3 tools_4.0.2    
[13] stringr_1.4.0 glue_1.4.1 abind_1.4-5 parallel_4.0.2 xfun_0.16 yaml_2.2.1     
[19] compiler_4.0.2 base64enc_0.1-3 htmltools_0.5.0 knitr_1.29    

```

Thanks for the new feature, and thanks for your help!

[tst.txt](https://discourse.mc-stan.org/uploads/short-url/uSo44QxeGeg437LwSXtJfsrCSdw.txt) (950 Bytes)

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [August 6, 2020, 2:58pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/2 "2020-08-06T14:58:12Z")

</div>

> [@nerutenbeck](#):
>
> Strangely, it works fine when I render the file (e.g. with the “Knit” button in RStudio or via `rmarkdown::render()` ) rather than execute the chunk in an interactive session.

My hunch is that overriding the built-in Stan knitr engine isn’t affecting R-markdown when rendering the chunks interactively for some reason. That is, it’s recognizing that it should use CmdStan when you render the whole thing but it might think it should use RStan when you evaluate the chunks interactively. If you set `override=FALSE`

```no-highlight
cmdstanr::register_knitr_engine(override=FALSE)

```

and then use `{cmdstan, output.var = "tst_chunk"}` instead of `{stan, output.var = "tst_chunk"}` does it also work interactively?

---

<div class="post-metadata">

**Author:** ![nerutenbeck](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nerutenbeck/32/4975_2.png) [@nerutenbeck](https://discourse.mc-stan.org/u/nerutenbeck)\
**Post date:** [August 6, 2020, 3:01pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/3 "2020-08-06T15:01:19Z")

</div>

Got it! I had tried `override = FALSE` but not setting the chunk interpreter to `cmdstan`. Thanks @jonah!

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [August 6, 2020, 3:18pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/4 "2020-08-06T15:18:15Z")

</div>

Glad that works. @bearloga Any idea if we can also get the chunks to work interactively without having to use `override` and changing the chunk to `cmdstan`? Alternatively should we just add more to the doc about this?

---

<div class="post-metadata">

**Author:** ![bearloga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bearloga/32/8136_2.png) [@bearloga](https://discourse.mc-stan.org/u/bearloga)\
**Post date:** [August 6, 2020, 5:54pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/5 "2020-08-06T17:54:48Z")

</div>

Assuming @nerutenbeck is talking about [R Notebooks](https://bookdown.org/yihui/rmarkdown/notebook.html) (the only way I know of running non-R chunks in R Markdown documents interactively in RStudio), it appears those don’t respect knitr options/overrides the way non-notebook R Markdown docs do.

To avoid changing the `stan` chunks to be `cmdstan` chunks, you can specify `engine = "cmdstan"` in the chunk’s options (after using `override = FALSE` when registering the engine), for example:

````
```{stan, output.var = "tst_chunk", engine = "cmdstan"}

````

But at that point you might as well just switch to `cmdstan` chunks.

I looked into using hooks[1][2][3] for automating this step for `stan` chunks to avoid specifying it manually in every chunk, but it seems R Notebooks don’t respect custom hooks either. In my tests the example hook from [2] was called when the document was knit/rendered, but not when the chunk was run interactively. Which is…a bummer.

@jonah: I can update the docs with a caveat about usage with R Notebooks and suggest these two workarounds.

* * *

1. [https://yihui.org/knitr/hooks/](https://yihui.org/knitr/hooks/)
2. [https://bookdown.org/yihui/rmarkdown-cookbook/option-hooks.html](https://bookdown.org/yihui/rmarkdown-cookbook/option-hooks.html)
3. [https://bookdown.org/yihui/rmarkdown-cookbook/chunk-hooks.html](https://bookdown.org/yihui/rmarkdown-cookbook/chunk-hooks.html)

---

<div class="post-metadata">

**Author:** ![nerutenbeck](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nerutenbeck/32/4975_2.png) [@nerutenbeck](https://discourse.mc-stan.org/u/nerutenbeck)\
**Post date:** [August 6, 2020, 6:39pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/6 "2020-08-06T18:39:35Z")

</div>

> [@bearloga](#):
>
> Assuming @nerutenbeck is talking about R Notebooks (the only way I know of running non-R chunks in R Markdown documents interactively in RStudio)

For what it’s worth, I checked and it does not seem to matter whether I set YAML output to `html_notebook` or `html_document`, (I can run either interactively in RStudio, it’s just that the `html_notebook` prints persistent Notebook style output in the doc instead of in the console / plots pane) - setting the chunk to `cmdstan` is required. NBD!

…and since we’re talking about this - what I’d _really_ love to be able to do is connect to a remote Jupyter kernel running on a server and do the same thing via Atom + Hydrogen. I saw the @mitzimorris post about [Stan notebooks in the cloud](https://mc-stan.org/users/documentation/case-studies/jupyter_colab_notebooks_2020.html), so will try to figure something out (or maybe somebody already has a solution…). Right now if I want to iterate over models on an EC2 instance I have to edit .stan files locally and then either scp them up or push / pull via GitHub. It’s not that big of a pain, but enough (especially during model writing) that I am excited to be able to do a certain amount of light development and editing interactively on my local machine before trying to scale up.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [August 6, 2020, 6:51pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/7 "2020-08-06T18:51:06Z")

</div>

@bearloga Thanks for looking into this. I agree it’s a bummer that neither knitr options/overrides nor hooks are respected when running chunks interactively. I don’t know why anyone would _not_ want them to be respected interactively so maybe it’s a bug in RStudio or just something they haven’t gotten around to implementing yet?

> [@bearloga](#):
>
> @jonah: I can update the docs with a caveat about usage with R Notebooks and suggest these two workarounds.

Thanks!

> [@nerutenbeck](#):
>
> …and since we’re talking about this - what I’d _really_ love to be able to do is connect to a remote Jupyter kernel running on a server and do the same thing via Atom + Hydrogen. I saw the @mitzimorris post about [Stan notebooks in the cloud](https://mc-stan.org/users/documentation/case-studies/jupyter_colab_notebooks_2020.html), so will try to figure something out (or maybe somebody already has a solution…).

That would be cool!

---

<div class="post-metadata">

**Author:** ![bearloga](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bearloga/32/8136_2.png) [@bearloga](https://discourse.mc-stan.org/u/bearloga)\
**Post date:** [August 7, 2020, 2:15am UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/8 "2020-08-07T02:15:50Z")

</div>

PR submitted for review: [https://github.com/stan-dev/cmdstanr/pull/268](https://github.com/stan-dev/cmdstanr/pull/268)

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [August 7, 2020, 4:03am UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/9 "2020-08-07T04:03:37Z")

</div>

Awesome, thank you!

---

<div class="post-metadata">

**Author:** ![nerutenbeck](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nerutenbeck/32/4975_2.png) [@nerutenbeck](https://discourse.mc-stan.org/u/nerutenbeck)\
**Post date:** [August 7, 2020, 6:29pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/10 "2020-08-07T18:29:57Z")

</div>

> [@nerutenbeck](#):
>
> …and since we’re talking about this - what I’d _really_ love to be able to do is connect to a remote Jupyter kernel running on a server and do the same thing via Atom + Hydrogen. I saw the @mitzimorris post about [Stan notebooks in the cloud](https://mc-stan.org/users/documentation/case-studies/jupyter_colab_notebooks_2020.html), so will try to figure something out (or maybe somebody already has a solution…). Right now if I want to iterate over models on an EC2 instance I have to edit .stan files locally and then either scp them up or push / pull via GitHub. It’s not that big of a pain, but enough (especially during model writing) that I am excited to be able to do a certain amount of light development and editing interactively on my local machine before trying to scale up.

Side note for posterity: I was able to figure out a way to do just this by connecting to a Docker container on an EC2 instance using VSCode. There is support for Jupyter notebooks, but it’s also possible to edit the remote directory tree directly, send R code to the remote console, and also do interactive plotting, which allows me to edit / compile / execute Stan files exclusively from a .Rmd or .ipynb on an instance. If your Docker container is equipped with GitHub permissions it’s also possible to fetch and push.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [August 7, 2020, 6:40pm UTC](https://discourse.mc-stan.org/t/failure-compiling-stan-model-chunk-in-rmd/17280/11 "2020-08-07T18:40:39Z")

</div>

Very nice!
