Failure compiling Stan model chunk in .Rmd

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

[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 (950 Bytes)

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


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

1 Like

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

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?

Assuming @nerutenbeck is talking about R Notebooks (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.


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, 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.


@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?


That would be cool!

PR submitted for review:


Awesome, thank you!

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.


Very nice!