Solved: knitr syntax higlighting of file-stored stan code!

45 views
Skip to first unread message

Mike Lawrence

unread,
Mar 26, 2017, 6:35:48 PM3/26/17
to Stan users mailing list
I couldn't find anyone else posting this solution, so I thought I'd share what I came up with. You simply need to do:

```{r, echo=FALSE}
writeLines(
c(
'```{stan, output.var="mod"}'
, readLines('my_stan_model.stan')
,'```'
)
, con = 'my_stan_model.Rmd'
)
```
```{r , child = 'my_stan_model.Rmd'}
```

Note 1: If you're using an RStudio R Notebook, the stan code won't render in the notebook view but will render in the .nb.html file.

Note2: Running the latter chunk will trigger compilation of the model, stored for access from R in the object "mod". 

Ben Goodrich

unread,
Mar 26, 2017, 7:11:44 PM3/26/17
to Stan users mailing list
On Sunday, March 26, 2017 at 6:35:48 PM UTC-4, Mike Lawrence wrote:
I couldn't find anyone else posting this solution, so I thought I'd share what I came up with.

With the RStudio preview

https://www.rstudio.com/products/rstudio/download/preview/

you can do this more simply with the "knitr code externalization" thing.

Ben

Bob Carpenter

unread,
Mar 27, 2017, 9:11:24 PM3/27/17
to stan-...@googlegroups.com
Thanks---we need a knitr and bookdown best practices doc for Stan.
I'll volunteer to be the first customer.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages