@@ -21,13 +21,21 @@ knitr::opts_chunk$set(
2121```
2222
2323R Markdown supports a variety of languages through the use of knitr language
24- engines. One such engine is the ` stan ` engine, which allows users to write Stan
25- programs directly in their R Markdown documents by setting the language of the
26- chunk to ` stan ` .
24+ engines. Where users wish to write Stan programs as chunks directly in R Markdown documents there are three options:
2725
28- Behind the scenes, the engine relies on RStan to compile the model code into an
29- in-memory ` stanmodel ` , which is assigned to a variable with the name given by
30- the ` output.var ` chunk option. For example:
26+ 1 . the user wishes all the Stan chunks in the R Markdown document to be processed using RStan;
27+ 2 . all Stan chunks are to be processed using CmdStanR; and.
28+ 3 . some chunks are to be processed by RStan and some by CmdStanR.
29+
30+ Behind the scenes in each option, the engine compiles the model code in each
31+ chunk and creates an object that provides methods to run the model: a
32+ ` stanmodel ` if Rstan is being used, or a ` CmdStanModel ` in the CmdStanR case.
33+ This model object is assigned to a variable with the name given by the
34+ ` output.var ` chunk option.
35+
36+ ## Option 1: Using RStan for all chunks
37+
38+ This is the default option. In that case we can write, for example:
3139
3240```` {verbatim}
3341```{stan, output.var="model"}
@@ -39,19 +47,19 @@ rstan::sampling(model)
3947```
4048````
4149
42- CmdStanR provides a replacement engine, which can be registered as follows:
50+ ## Option 2: Using CmdStanR for all chunks
51+
52+ If CmdStanR is being used a replacement engine needs to be registered along the following lines:
4353
4454``` {r register-engine, message=FALSE}
4555library(cmdstanr)
46- check_cmdstan_toolchain(fix = TRUE, quiet = TRUE)
47-
48- register_knitr_engine()
56+ register_knitr_engine(override = TRUE)
4957```
5058
51- By default, this overrides knitr's built-in ` stan ` engine so that all ` stan `
59+ This overrides knitr's built-in ` stan ` engine so that all ` stan `
5260chunks are processed with CmdStanR, not RStan. Of course, this also means that
5361the variable specified by ` output.var ` will no longer be a ` stanmodel ` object,
54- but instead a ` CmdStanModel ` object, so the code above would look like this:
62+ but instead a ` CmdStanModel ` object, so the example code above would look like this:
5563
5664```` {verbatim}
5765```{stan, output.var="model"}
@@ -89,15 +97,8 @@ fit <- ex1$sample(
8997print(fit)
9098```
9199
92- ## Caching chunks
93-
94- Use ` cache=TRUE ` chunk option to avoid re-compiling the Stan model code every
95- time the R Markdown is knit/rendered.
96-
97- You can find the Stan model file and the compiled executable in the document's
98- cache directory.
99100
100- ## Using RStan and CmdStanR engines side-by-side
101+ ## Option 3: Using both RStan and CmdStanR in the same R Markdown document
101102
102103While the default behavior is to override the built-in ` stan ` engine because the
103104assumption is that the user is probably not using both RStan and CmdStanR in the
@@ -130,6 +131,17 @@ model_obj2$sample()
130131```
131132````
132133
134+
135+ ## Caching chunks
136+
137+ Use ` cache=TRUE ` chunk option to avoid re-compiling the Stan model code every
138+ time the R Markdown is knit/rendered.
139+
140+ You can find the Stan model file and the compiled executable in the document's
141+ cache directory.
142+
143+
144+
133145## Running interactively
134146
135147When running chunks interactively in RStudio (e.g. when using
0 commit comments