Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update r-markdown.Rmd #854

Merged
merged 5 commits into from
Sep 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions vignettes/r-markdown.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ knitr::opts_chunk$set(
```

R Markdown supports a variety of languages through the use of knitr language
engines. One such engine is the `stan` engine, which allows users to write Stan
programs directly in their R Markdown documents by setting the language of the
chunk to `stan`.
engines. Where users wish to write Stan programs as chunks directly in R Markdown documents there are three options:
1. the user wishes all the Stan chunks in the R Markdown document to be processed using RStan;
2. all Stan chunks are to be processed using CmdStanR; and
3. some chunks are to be processed by RStan and some by CmdStanR.

Behind the scenes, the engine relies on RStan to compile the model code into an
in-memory `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 into an
in-memory model: a `stanmodel` if Rstan is being used, or a `CmdStanModel` in the CmdStanR case. The in-memory model is assigned to a variable with the name given by the `output.var` chunk option.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One subtlety is that CmdStanR creates a compiled executable that technically isn't in-memory (from R's perspective). The CmdStanModel object itself is in-memory (so what you say here isn't wrong), but to run Stan it runs the compiled executable outside of R and reads the results back in (unlike RStan which does everything in-memory in R via Rcpp). To avoid any confusion about this maybe we should just remove "in-memory" and instead say something like the following, which I hope gets the idea across:

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.

If you're ok with that change I can make it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am entirely content with that change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great, I just went ahead and made that change. I also just read through the updated vignette again and I think it's much clearer now than before. Thank you again for bringing this to our attention and making the edits.

Once all the unit tests pass (they got triggered again because of my edit) I will merge this pull request.


## Option 1: Using RStan for all chunks

This is the default option. In that case we can write, for example:

````{verbatim}
```{stan, output.var="model"}
Expand All @@ -38,20 +42,20 @@ the `output.var` chunk option. For example:
rstan::sampling(model)
```
````

CmdStanR provides a replacement engine, which can be registered as follows:
## Option 2: Using CmdStanR for all chunks
If CmdStanR is being used a replacement engine needs to be registered along the following lines:

```{r register-engine, message=FALSE}
library(cmdstanr)
check_cmdstan_toolchain(fix = TRUE, quiet = TRUE)

register_knitr_engine()
register_knitr_engine(override = FALSE)
jgabry marked this conversation as resolved.
Show resolved Hide resolved
```

By default, this overrides knitr's built-in `stan` engine so that all `stan`
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:
but instead a `CmdStanModel` object, so the example code above would look like this:

````{verbatim}
```{stan, output.var="model"}
Expand Down Expand Up @@ -88,16 +92,7 @@ fit <- ex1$sample(

print(fit)
```

## Caching chunks

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.

## Using RStan and CmdStanR engines side-by-side
## Option 3: Using both RStan and CmdStanR in the same R Markdown document

While the default behavior is to override the built-in `stan` engine because the
assumption is that the user is probably not using both RStan and CmdStanR in the
Expand Down Expand Up @@ -130,6 +125,17 @@ model_obj2$sample()
```
````


## Caching chunks

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.



## Running interactively

When running chunks interactively in RStudio (e.g. when using
Expand Down
Loading