From 421d28fc53e0cd33170739d40ad0d8fd9d47ed16 Mon Sep 17 00:00:00 2001
From: jgabry R Markdown supports a variety of languages through the use of knitr
-language engines. One such engine is the Behind the scenes, the engine relies on RStan to compile the model
-code into an in-memory Mikhail
stan
engine, which
-allows users to write Stan programs directly in their R Markdown
-documents by setting the language of the chunk to stan
.stanmodel
, which is assigned to a
-variable with the name given by the output.var
chunk
-option. For example:
Behind the scenes in each option, the engine compiles the model code
+in each chunk and creates an object that provides methods to run the
+model: a stanmodel
if Rstan is being used, or a
+CmdStanModel
in the CmdStanR case. This model object is
+assigned to a variable with the name given by the
+output.var
chunk option.
This is the default option. In that case we can write, for +example:
```{stan, output.var="model"}
// Stan model code
```
@@ -157,19 +171,21 @@ Mikhail
```{r}
rstan::sampling(model)
```
CmdStanR provides a replacement engine, which can be registered as -follows:
+If CmdStanR is being used a replacement engine needs to be registered +along the following lines:
library(cmdstanr)
-check_cmdstan_toolchain(fix = TRUE, quiet = TRUE)
-
-register_knitr_engine()
By default, this overrides knitr’s built-in stan
engine
-so that all stan
chunks are processed with CmdStanR, not
-RStan. Of course, this also means that the variable specified by
+register_knitr_engine(override = TRUE)
This overrides knitr’s built-in stan
engine so that all
+stan
chunks are processed with CmdStanR, not RStan. Of
+course, this also means that the variable specified by
output.var
will no longer be a stanmodel
-object, but instead a CmdStanModel
object, so the code
-above would look like this:
CmdStanModel
object, so the example
+code above would look like this:
```{stan, output.var="model"}
// Stan model code
```
@@ -177,6 +193,7 @@ Mikhail
```{r}
model$sample()
```
Use cache=TRUE
chunk option to avoid re-compiling the
-Stan model code every time the R Markdown is knit/rendered.
You can find the Stan model file and the compiled executable in the -document’s cache directory.
-While the default behavior is to override the built-in
stan
engine because the assumption is that the user is
@@ -259,6 +269,14 @@
Use cache=TRUE
chunk option to avoid re-compiling the
+Stan model code every time the R Markdown is knit/rendered.
You can find the Stan model file and the compiled executable in the +document’s cache directory.
+When running chunks interactively in RStudio (e.g. when using R