# R package using cmdstanr

**URL:** <https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758>\
**Category:** CmdStan\
**Created:** [September 13, 2023, 9:50am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758 "2023-09-13T09:50:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 13, 2023, 9:50am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/1 "2023-09-13T09:50:21Z")

</div>

I am writing an R package that includes various Stan models. I hope to run these models using `cmdstanr` rather than `rstan` but I can only find guidance on using `rstan` in packages.

Should I just follow that guidance but include `cmdstanr` and just never use the `rstan` material? Or is there a better way?

---

<div class="post-metadata">

**Author:** ![maxbiostat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/maxbiostat/32/1326_2.png) [@maxbiostat](https://discourse.mc-stan.org/u/maxbiostat)\
**Post date:** [September 13, 2023, 10:14am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/2 "2023-09-13T10:14:18Z")

</div>

@andrjohns@jonah @rok_cesnovar

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [September 13, 2023, 11:56am UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/3 "2023-09-13T11:56:34Z")

</div>

Do you want/need to ship precompiled models inside your package, or are you fine with a package that (like `brms`) compiles the models at runtime?

---

<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:** [September 13, 2023, 12:07pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/4 "2023-09-13T12:07:27Z")

</div>

I dont think there is any special guidance written down for cmdstanr, as there isnt really anything particulary special you need to do (not makefile stuff or anything of that sort).

I have a minimal package that uses cmdstanr to sample here [https://github.com/rok-cesnovar/misc/tree/master/democmdstanr](https://github.com/rok-cesnovar/misc/tree/master/democmdstanr)  
Hopefully that may help.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 13, 2023, 12:51pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/5 "2023-09-13T12:51:01Z")

</div>

Precompiled models is what I am planning to use.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 13, 2023, 12:59pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/6 "2023-09-13T12:59:14Z")

</div>

Very helpful, thank you. I have been trying to use the step by step guide [[Step by step guide for creating a package that depends on RStan • rstantools](https://mc-stan.org/rstantools/articles/minimal-rstan-package.html)] and that creates a vastly more complicated package with lots of references to `rstan`.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 13, 2023, 2:42pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/7 "2023-09-13T14:42:51Z")

</div>

I also recommend checking out @wlandau’s new R package that helps you make a package with cmdstanr that has precompiled models:

> [@{instantiate}: pre-compiled CmdStan models in R packages](https://discourse.mc-stan.org/t/instantiate-pre-compiled-cmdstan-models-in-r-packages/32191):
>
> The rstantools package has served the community beautifully, allowing us to pre-compile Stan models in R packages much like rstanarm. Aside from the obvious efficiency gains, especially in simulation studies, pre-compiling saves us the trouble of diagnosing compiler errors near runtime. However, the version of Stan in CmdStan is far more advanced, and many of us R package developers are keen to work it into our packages in a seamless way. To meet the need, I created a package called instantiate.…

It’s basically like rstantools but for cmdstanr.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 13, 2023, 3:04pm UTC](https://discourse.mc-stan.org/t/r-package-using-cmdstanr/32758/8 "2023-09-13T15:04:53Z")

</div>

Many thanks. I’ll give it a try.
