# R package using cmdstanr

**URL:** <https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239>\
**Category:** Interfaces\
**Tags:** cmdstanr\
**Created:** [November 12, 2020, 6:38pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239 "2020-11-12T18:38:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [November 12, 2020, 6:38pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/1 "2020-11-12T18:38:51Z")

</div>

Has anyone written a package that uses cmdstanr models inside it?

I was writing a little package to test some things and I could use an example of how to organize things (how to handle the model compile, how to set up tests, etc. etc.).

Pinging a few people who might know @jonah @mike-lawrence @rok_cesnovar

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [November 12, 2020, 6:47pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/2 "2020-11-12T18:47:12Z")

</div>

Brms and the rethinking package use cmdstanr, but those build models on the fly to compile them so that is probably not a good comparison. I have seen some other packages like EpiNow2 that either use cmstanr already or are transitioning.

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [November 12, 2020, 11:01pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/3 "2020-11-12T23:01:04Z")

</div>

@wlandau has a package combining his targets system with Stan for simulation/validation: [https://github.com/wlandau/targets-stan](https://github.com/wlandau/targets-stan)

---

<div class="post-metadata">

**Author:** ![wlandau](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wlandau/32/9846_2.png) [@wlandau](https://discourse.mc-stan.org/u/wlandau)\
**Post date:** [November 12, 2020, 11:35pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/4 "2020-11-12T23:35:14Z")

</div>

Thanks for the plug, @mike-lawrence. That’s actually more of a workflow archetype though. I’m also thinking about a pipeline package for Stan models (basically [`tarchetypes`](https://github.com/wlandau/tarchetypes) for `cmstanr`) but that won’t have any actual models in it.

@bbbales2 I have been wondering about this too, and I think it is related to [Cmdstanr and R packages with precompiled models](https://discourse.mc-stan.org/t/cmdstanr-and-r-packages-with-precompiled-models/19153).

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [November 13, 2020, 12:20am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/5 "2020-11-13T00:20:35Z")

</div>

OOo, this is useful information, thanks!

---

<div class="post-metadata">

**Author:** ![wlandau](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wlandau/32/9846_2.png) [@wlandau](https://discourse.mc-stan.org/u/wlandau)\
**Post date:** [November 30, 2020, 4:58pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/6 "2020-11-30T16:58:04Z")

</div>

FYI, I just released the dev version of a new package called [`stantargets`](https://github.com/wlandau/stantargets) ([announced here](https://discourse.mc-stan.org/t/stantargets-a-new-workflow-automation-package-for-cmdstanr-projects/19564)).

---

<div class="post-metadata">

**Author:** ![wlandau](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wlandau/32/9846_2.png) [@wlandau](https://discourse.mc-stan.org/u/wlandau)\
**Post date:** [March 29, 2021, 1:54pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/7 "2021-03-29T13:54:48Z")

</div>

Currently using [Cmdstanr and R packages with precompiled models - #4 by wlandau](https://discourse.mc-stan.org/t/cmdstanr-and-r-packages-with-precompiled-models/19153/4) to reduce compilation in a `cmstanr`-powered modeling package for work.

---

<div class="post-metadata">

**Author:** ![stemangiola](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stemangiola/32/260_2.png) [@stemangiola](https://discourse.mc-stan.org/u/stemangiola)\
**Post date:** [June 16, 2022, 6:53am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/8 "2022-06-16T06:53:26Z")

</div>

Any good templates/packages that came out based on cmdstanr (brms mainly uses rstan) since then?

I was mostly interested in allowing as much as possible the one-line installation that is typical of R. I can compile the model on the fly and save it locally to avoid recompiation, but it is the installation process that bothers me.

I would like to do

```r
package.install("MY_PACKAGE")

```

Without the need for the user to do

```r
library(cmdstanr)
check_cmdstan_toolchain()
install_cmdstan(cores = 2)

```

Basically the ease of installation of `rstan` with the robustness of `cmdstanr`

Can

```r
library(cmdstanr)
check_cmdstan_toolchain()
install_cmdstan(cores = 2)

```

be done in MY\_PACKAGE backend at the first run?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [June 16, 2022, 6:58am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/19239/9 "2022-06-16T06:58:46Z")

</div>

You should define a `.onLoad` function in your package that tries to check whether the user has a valid CmdStan installation (see `cmdstanr::cmdstan_version()`). If it does not found one then run

```nohighlight
cmdstanr::check_cmdstan_toolchain()
cmdstanr::install_cmdstan(cores = 2)

```

or something the like.
