Cmdstanr and R packages with precompiled models

Thanks to rstantools, it is possible to include precompiled Stan/rstan models in R packages. Are there plans to build a similar capability based on cmdstanr alone?

Not possible for CRAN.

1 Like

Yeah like @bgoodri this isn’t going to be possible with CRAN (they won’t have cmdstan on their machines to compile the models), although it could be possible to do something like this for packages installed outside of CRAN. For example, I suppose you could allow people to download pre-compiled models from some repository. So I think the limitation is CRAN but in theory something like this is possible if you offer the pre-compiled models some other way.

2 Likes

On reflection, tools::R_user_dir(package = "custompkg", which = "cache") should make this easy in R >= 4.0.0. Each time the user invokes a model, the package could copy over the source file to the user cache while preserving the modification timestamp (file.copy(copy.date = TRUE)) and then compile the newly copied Stan file in the cache. In theory, the model should compile at most once every package update. I am currently developing an internal package at work that uses this approach.

3 Likes

Update: I created a new package to help pre-compile CmdStan models in R packages:

The models compile at install time, but they skip compilation whenever CmdStan or cmdstanr is not found, which will make it possible to submit to CRAN.

1 Like