Using compiled cmdstanr models in an R package

I’m creating an R package to fit certain statistical models using cmdstanr (GitHub - maxdrohde/exnexstan: R package to fit EXNEX models with Stan). However, the approach I’m using works when the package is installed on my computer, but not on others, so I don’t think I’m going about this correctly. Any suggestions are appreciated.

The approach I am using is as follows:

  1. Write the Stan code
  2. Compile it with cmdstanr::cmdstan_model()
  3. Put the executable file generated in step 2 into the inst directory of the R package
  4. When the package is installed, the files from the inst directory including the executable Stan models are put into the package directory of the user
  5. Read in the the executable model file from the user’s package directory with cmdstanr::cmdstan_model(exe_file = "executable_file) where fs::path_package() is used to locate the executable Stan model

This process works when I install the package and run it on my own computer (where I compiled the Stan model originally). However, when I install the package on another computer, it doesn’t work because it seems to try reference files from the original computer. I’m not sure if this is an issue with how I compiled the code originally?

Example error:

Chain 1 dyld[46731]: Library not loaded: @rpath/libtbb.dylib
Chain 1   Referenced from: <82EF8E52-BC74-3AFF-8760-5C8A3D2EF828> /Library/Frameworks/R.framework/Versions/4.2/Resources/library/exnexstan/exnex
Chain 1   Reason: tried: '/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/max/.cmdstan/cmdstan-2.32.2/stan/lib/stan_math/lib/tbb/libtbb.dylib' (no such file), '/usr/local/lib/libtbb.dylib' (no such file), '/usr/lib/libtbb.dylib' (no such file, not in dyld cache)
1 Like

I recommend checking out a new package from @wlandau that helps with developing packages using cmdstanr: {instantiate}: pre-compiled CmdStan models in R packages


This worked perfectly, thanks for the suggestion @jonah, and thank you to @wlandau for the great package!


Glad to know it worked!

Your original proposal in (1)-(5) was almost complete, the only thing missing was a way to trigger compilation during install.packages(). For that piece, instantiate writes scripts configure and It gets a little trickier with CRAN, and instantiate implements/documents workarounds for that, but your idea + configure is basically it.