Stan Math library in R package, installation error on Windows

Hi all,

I am trying to use Stan Math C++ library in an R package (for automatic differentiation). It is working fine on Linux, but Windows users reported an error installing the package. I made a simpler package stanmathtest for testing this.

On Windows, the error is something like:

Error: package or namespace load failed for 'stanmathtest' in inDL(x, as.logical(local), as.logical(now), ...):
 unable to load shared object '[...]/R-library/00LOCK-stanmathtest/00new/stanmathtest/libs/x64/stanmathtest.dll':
  LoadLibrary failure:  The specified module could not be found.

The error is the same whether I run on a personal computer or on win-builder. The compilation is working, then something strange with DLL is happening that I don’t understand. (I can get the package functions working on Windows by downloading the source and running devtools::load_all(path_to_package_source), but installation still fails.)

I made the the simple package by running Rcpp.package.skeleton then adding in Makevars and Makevars.win and copying the first bit of C++ code from the StanHeaders vignette. I seem to get the same error whenever I have #include <stan/math.hpp> in the C++ code (I get the error even when no features of Stan Math library are used in the code itself).

Any thoughts or ideas for a solution or workaround would be much appreciated.

Thank you!
Helen

This tends to happen when the package isn’t properly linking to RcppParallel (and the TBB). Your Makevars files are properly configured, but you need to also update your DESCRIPTION and NAMESPACE files to import RcppParallel so that the RcppParallelLibs function is available.

You can see instructions for this in the RcppParallel docs here: https://rcppcore.github.io/RcppParallel/#r_packages

Thank you very much. I can confirm this worked and my test package now installs without problems on Windows.

Perhaps it could be useful to others update the instructions on “Using the StanHeaders Package from Other R Packages” in the vignette to reflect this? Currently it says RcppParallel should be in LinkingTo in the DESCRIPTION, but the suggested Makevars.win doesn’t include everything needed for RcppParallel (as described in those RcppParallel docs).

Oh great catch, that would definitely be clearer. Will do!

3 Likes