Using {cmdstanr} v0.7.1 and cmdstan v2.34.1 and RStudio 2023.12.1 Build 402.
A code chunk in Quarto like the below will fail to properly color code the \ and **\ comments (it only recognizes the old style “#” comments. It also seems to get confused about the transpose tick to convert a row vector to vector. Finally, the shortcut for commenting multiple lines uses the old “#” comments. a {stan} code chunk seems to work properly but doesn’t support the commenting shortcut.
I’m seeing this problem in the RStudio editor. I added the .xml file you mentioned to my _quarto.yml file, but that didn’t make a difference. I think this xml file only matters for the output of Quarto, right?
Sorry I might not have been very clear. I’m editing a Stan code chunk in a .qmd file (not what I typically do, but I wanted to put something concise as an example for something). Here’s a screenshot of what I’m talking about. Notice the correct syntax coloring in RStudio and the difference between the stan and cmdstan chunks?
It seems like you can use either if you don’t need both rstan and cmdstanr in the same document (based on the docs, anyway, I haven’t personally tried it).
I’m not sure if cmdstanr has any way to control how rstudio highlights inside of markdown documents, but I’ll ping @jonah just in case
the code snippet is the Stan program, therefore the syntax coloring is stan.
what interface you use to run the Stan program is another question, but RStudio
is only looking at file types - the qmd file you’re editing, and syntax tags -
the src code language, which is stan.
I don’t want to use option 1, when I use option 2 the call to register_knitr_engine doesn’t do anything and I still end up with an rstan model, and finally option 3 is what I’m actually doing (using a cmdstan code chunk) and I am able to build a cmdstanr model but the syntax coloring is all wrong.
I feel that both the cmdstan and stan code chunks should have the same syntax coloring. I don’t know how things are happening in the backend, but as a user I would expect that a supported Stan code chunk (i.e., a cmdstan code chunk) would have correct syntax coloring.
I also just tried to use knitr::knit_engines$set(stan = cmdstanr::eng_cmdstan) but it doesn’t work because I’m still building an rstan model.
have you tried changing the {cmdstan} tag to {stan}.
CmdStan, CmdStanR, and all the other interfaces are interfaces to Stan - any model written using the Stan language is a Stan model. In CmdStanR, a CmdStanModel object manages the underlying Stan program. therefore, when showing the source code for the contents of a .stan file, please use the tag stan.
in that article, the example is correct for both options -
```{stan, output.var="model"}
// Stan model code
(not sure what output.var="model" is doing - the relevant part is that the code block is type stan)
The stan tag should cause RStudio to use rstan, the cmdstan tag should cause it to use cmdstanr, in conjunction with cmdstanr::register_knitr_engine(). This definitely used to work properly in Rmd files (although I haven’t tested qmd files), so if it’s not working properly in Rmd anymore perhaps there’s an RStudio bug, or perhaps they changed something that we’re unaware of and we haven’t made the necessary updates. Maybe @bearloga has an idea (they contributed the very nice functionality to allow switching between rstan and cmdstanr, or using both in the same document)?
Come on now, I wrote that above and I obviously know that removing the “cmd” part would make the highlighting work :-D
My problem is I want to use cmdstan so I would end up using cmdstanr and not rstan! I was trying to follow the instructions on the cmdstanr website (above) to do exactly that.
Listen, if the highlighting can’t work with cmdstan that’s ok. I never write a real model in a chunk like this. I just wanted to include a simple pretty example in an RStudio notebook, something embedded in there and not in a separate Stan file.
No need to apologize. I hope I’m not missing something obvious here.
If you look here R Markdown CmdStan Engine • cmdstanr you will see that the tag cmdstan is a legitimate one to generate a cmdstanr model in the notebook. Such a chunk of code in the file will not display correct syntax highlighting in RStudio (I’m not talking about the html output). There are supposed to be ways (see other options in same document) that allow us to force the tag “stan” to use cmdstanr. However, this is broken; rstan is always used no matter what I do to override it.