Include Stan code

I want just to include a nicely formatted Stan code in an R Markdown document, but I don't want to run the Stan code when knitting because it takes a long time (and I am reloading a previously saved model instead).

The eval=FALSE knitr option is working for the R code chunks, but I am getting an error when adding the same eval=FALSE option to the Stan code chunk. Any idea to solve this? Thank you.

Thank you for your suggestion.
Unfortunately it's not working: the nice, color syntax highlight is lost in case of Stan code.

What is the error ?

If you want only syntax highlighting, using

```stan
# code 
```

should be enough, but you need to use a syntax highligter that supports Stan with the color you want.
Unfortunately, the color syntax highlighting you see in RStudio IDE won't be the one you get in the output document.
If you use default value for html_document, highlight.js will be use, which supports Stan syntax highlighting but it could be different than what you expect. See https://highlightjs.org/static/demo/
If you use one of the Pandoc syntax highlight theme with the highlight argument in html_document, it won't do anything because Pandoc does not support yet Stan. The file stan.xml is not in https://github.com/jgm/skylighting/tree/master/skylighting-core/xml but is upstream https://github.com/KDE/syntax-highlighting/tree/master/data/syntax so I guess it could be added easily.

Hope it helps

@cderv, thank you for your detailed explanation. Now, I understand that it is not trivial (possible) to have nice syntax highlight.

On the other hand, still I consider that the eval=FALSE would make sense for the Stan chunks.

Rationale: the Stan code chunk is translated to a nicely formatted text (without syntax highlight), but the Stan is compiled again and again when knitting, which can be time consuming. For this reason I fitted in advance my Stan models separately and I was planning to re-read them in the Rmarkdown file. I would like to have some nice output when showing/including the Stan code (not cat). The other reason, that the RStudio Desktop is quite frequently crashing (bomb icon) when running Stan code from R.

What is the error when you set eval = FALSE - it should work. Can you share more about your code or the error ? Thanks

@cdev, sorry, I forget to include the error code.
And this morning I updated my whole R environment (usually I am doing it on monthly basis) and it's working now. I am not able to reproduce the error anymore.
So, it's solved. Thank you.

1 Like

That is great !

Glad it works for you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.