# Developing a package that uses rstan - Pre-translated c++ files

**URL:** <https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251>\
**Category:** Developers\
**Tags:** rstan\
**Created:** [October 29, 2018, 3:55am UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251 "2018-10-29T03:55:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlemfh96](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dlemfh96/32/3539_2.png) [@dlemfh96](https://discourse.mc-stan.org/u/dlemfh96)\
**Post date:** [October 29, 2018, 3:55am UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/1 "2018-10-29T03:55:28Z")

</div>

Hello, I’m a student and my lab is developing an R package that uses RStan.  
We’re trying to refactor the package structure, to go with that suggested by rstantools(v 1.5.1).  
I’ve read the rstantools vignettes, and used the `rstan_package_skeleton` to create the basic directories and files.

I understand that RStan works like this: stan files are first **translated** to c++ code, then it is **compiled** into dso objects that can now be used to draw samples with.

I wonder what would be the way to provide “pre-translated” c++ code to the users.  
Would it just be as simple as including the created .hpp files within the package before distributing the package? Or would I also need to edit some lines of code to make it use the pre-translated hpp files (instead of translating the raw stan files again)?

I’ve been trying to figure this out on my own, reading the code of stanc() and make\_cc.R and others, but I just couldn’t wrap my head around which line of code it is I should be looking for.

Also, _would_ including pre-translated c++ files in the package be recommended?  
To my understanding the rstanarm package is not doing that, and instead translates and compiles all its stan files during package installation.

Thank you for the time and consideration!

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [October 29, 2018, 4:27am UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/2 "2018-10-29T04:27:00Z")

</div>

Glad to hear of your interest. For starters, you are probably better off installing a branch of **rstantools** , which will likely be merged and release soon via

```no-highlight
remotes::install_github("stan-dev/rstantools", ref = "mlysy-rstantools_patch")

```

To answer your question,

> [@dlemfh96](#):
>
> I wonder what would be the way to provide “pre-translated” c++ code to the users.

in the vast majority of circumstances, it is un-necessary to provide the transpiled C++ code in your package, as you alluded to with **rstanarm** and now about 25 R packages like it on CRAN.

But, for clarity’s sake, when you do `R CMD build` or something that calls it, `make` is invoked on the src/Makevars (.win on Windows), which calls

```no-highlight
"$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "source(file.path('..', 'tools', 'make_cc.R')); make_cc(commandArgs(TRUE))" $<

```

on each of your .stan files. The tools/make\_cc.R file calls `rstan::stanc` to generate the C++ code implied by the Stan program and calls `Rcpp::exposeClass` to create the corresponding C++ class that can be called by the Modules mechanism in the **Rcpp** package. Once these .cc files are created, `make` then compiles them into a shared object that is included in the binary packages for Windows and Mac on CRAN.

So, you are probably thinking too long about something that does not need to be thought about that much. But if what you are really asking is “How can I include regular C++ code alongside the C++ code that is generated from the Stan programs?”, apparently that is now better supported by the aforementioned branch of **rstantools** and you basically just put your regular C++ code into src/ perhaps best with a .cpp extension.

---

<div class="post-metadata">

**Author:** ![dlemfh96](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dlemfh96/32/3539_2.png) [@dlemfh96](https://discourse.mc-stan.org/u/dlemfh96)\
**Post date:** [October 29, 2018, 7:38am UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/3 "2018-10-29T07:38:00Z")

</div>

Thank you so much Ben!

So, would I have understood it correctly if I were to say:

The stan files in `src/stan_files` are passed through `stanc()` which generates C++ codes, which are then passed through `exposeClass()` to create Rcpp modules. (All of which are done by invoking `make_cc`.)  
And then, R/stanmodels.R would create a list `stanmodels` that is a list of `stanmodel` class objects, which have a `mk_cppmodule` field to which the previously created Rcpp modules are each assigned?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [October 29, 2018, 12:46pm UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/4 "2018-10-29T12:46:31Z")

</div>

Yes, I think that is right. But if it is wrong in some respect, it doesn’t really matter. For one thing, with the next version of **rstantools** , then Stan programs get put under inst/ . It took us a while to figure out how to get **rstanarm** to build, but once we incorporated that process into **rstantools** a lot of other people were able to copy it without knowing much more than

1. Write good Stan programs
2. Write wrapper functions that call `sampling` on those `stanmodels` that are in the `stanmodels` list

Keep an eye on

> [@How to update a package that has StanHeaders and rstan in its LinkingTo](https://discourse.mc-stan.org/t/how-to-update-a-package-that-has-stanheaders-and-rstan-in-its-linkingto/6041):
>
> This thread is for maintainers of R packages that ship with C++ code generated by Stan. Due to the move to C++14 by Stan, many such packages need to get updated ASAP because they currently do not build from source. I will make PRs on GitHub for the applicable packages, but the basic idea in each case is to install rstan 2.18.1 from CRAN and then in your package make the following changes: In DESCRIPTION, Under Depends: edit it to R \>= 3.4.0, Rcpp (\>= 0.12.0), methods (plus perhaps other pac…

and

> [@How to update a package that has StanHeaders and rstan in its LinkingTo, part II](https://discourse.mc-stan.org/t/how-to-update-a-package-that-has-stanheaders-and-rstan-in-its-linkingto-part-ii/6214):
>
> If you have a R package on CRAN that has StanHeaders in its LinkingTo, you likely received an email from CRAN objecting to some of the patterns rstanarm has used since 2015 to build. Please do not forward or quote from that email because reasons. If you have a R package that is going to CRAN eventually but was created since 2015, you will likely need to change some of them. I will update this thread after trying some alternative build syntax and seeking clarification from CRAN.

though.

---

<div class="post-metadata">

**Author:** ![dlemfh96](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dlemfh96/32/3539_2.png) [@dlemfh96](https://discourse.mc-stan.org/u/dlemfh96)\
**Post date:** [October 29, 2018, 1:35pm UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/5 "2018-10-29T13:35:44Z")

</div>

Thank you for the hard work so that packages can be made more easily!!  
Also, thank you for the kind replies! :D

---

<div class="post-metadata">

**Author:** ![Michael\_Bertolacci](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/michael_bertolacci/32/3483_2.png) [@Michael\_Bertolacci](https://discourse.mc-stan.org/u/Michael_Bertolacci)\
**Post date:** [October 29, 2018, 2:10pm UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/6 "2018-10-29T14:10:29Z")

</div>

I’ve been meaning to post about this for some time but moved on to other things and forgot about it.

A few months ago I was developing a package using Stan and got frustrated with how the rstantools build process converts Stan to C++ files on the fly, and has to use complicated dynamic loading of the DSO.

So I modified the process and wrote a little package called [stanmodularise](https://github.com/mbertolacci/stanmodularise) with the following approach based on pre-translating the C++ files:

- You write some Stan files in src/stan\_files
- You run `modularise_stan_files()` on your package (like `devtools::document` or `Rcpp::compileAttributes`). This creates header and C++ files in src/.
- You compile/load/install with `devtools::document`/`load_all`/`install` as usual. If you update a stan file, you have to re-run `modularise_stan_files()` before recompiling

The advantages of this are: faster package recompilation, you don’t have to use `local = FALSE`, and the module loading process is simpler and more robust because it doesn’t use the dynamic loading step to discover the contents of the DSO (Rcpp attributes are used to register the model code instead).

The disadvantage is you have to remember to re-run `modularise_stan_files()` if you change a Stan model.

The package doesn’t do the nice things rstantools does like add to the LinkingTo, Makevars, etc, yet, because I figured it would be better contribute this to rstantools. Maybe this will do what you need, or at least provide a basis for an extension to rstantools. I also haven’t touched this since the C++14 changes.

I wouldn’t mind doing some work to add it to rstantools if it was considered useful, but it is quite a different approach and I wouldn’t presume to claim that it’s better overall; I just found it suited my projects a bit better.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [October 29, 2018, 5:32pm UTC](https://discourse.mc-stan.org/t/developing-a-package-that-uses-rstan-pre-translated-c-files/6251/7 "2018-10-29T17:32:54Z")

</div>

> [@Michael\_Bertolacci](#):
>
> So I modified the process and wrote a little package called [stanmodularise](https://github.com/mbertolacci/stanmodularise) with the following approach based on pre-translating the C++ files:

Nice! Thanks for sharing.

> [@Michael\_Bertolacci](#):
>
> I wouldn’t mind doing some work to add it to rstantools if it was considered useful, but it is quite a different approach and I wouldn’t presume to claim that it’s better overall; I just found it suited my projects a bit better.

@bgoodri or @jonah should be able to answer this.
