Rccp Error - Failed to initialize module pointer


I wanted to see if building an R package which depends on RStan was feasible for me and so I followed the demo : Step by step guide for creating a package that depends on RStan and have had a problem using roxygen.

I followed the demo step-by-step and recieve the following error when running , roxygen2::roxygenize() :

Loading rstanlmdemo
*Error in Rcpp::loadModule(module = “stan_fit4lm_mod”, what = TRUE, env = ns, : *

  • Unable to load module “stan_fit4lm_mod”: Failed to initialize module pointer: Error in FUN(X[[i]], …): no such symbol _rcpp_module_boot_stan_fit4lm_mod in package rstanlmdemo*

I have seen a few similar problems across different boards [1] but nothing that has been able to fix it. Does anyone have any idea how to address this? Thanks in advance.


pkgbuild::compile_dll() worked?

Yes, it runs with no problem

I would try calling R CMD INSTALL --preclean pkg once outside of RStudio where pkg is the directory where your package is.

I am experiencing the same issue. R CMD INSTALL --preclean does not work as well. Do you have any other suggestions?
Thank you

I tried to create an R package that depends on Stan following this vignette. Unfortunately, I encountered exactly the same problem as mentioned above when I tried to call roxygen2::roxygenize()
Does anybody know how to solve this problem?

Thank you!

I recently encountered this, but I cannot recall how I fixed it.

I think I fixed it by removing the stanmodels.R in R/, and any compilation output in src/ (the .so and .dlls, RcppExports.cpp, *.cc). Then running roxygen. I think the issue was that it needed useDynLib(blahblah) in the NAMESPACE before it could handle dynamic libs, but the dynamic lib is loaded before roxygen is processed. I’m not 100% sure. Note that it is safe to delete these files, because the configure script generates them at build time anyway.

Thank you very much for your assistance!
Unfortunately, your proposition did not work for me. I have removed the stanmodels.R and compilation output in src/ and then ran roxygen. As indicated below, it seems that R is compiling the stan model and then issues the error.
I really don’t know how this issue can be solved.
Thanks again in advance!

Loading rstanlm
Re-compiling rstanlm
-  installing *source* package 'rstanlm' ...
   ** using staged installation
   ** libs
   C:/RBuildTools/3.5/mingw_64/bin/g++ -m64 -std=c++1y  -I"C:/Users/n15059/DOCUME~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/n15059/Documents/R/R-3.6.2/library/BH/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/Rcpp/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/RcppEigen/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/rstan/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=corei7 -mtune=corei7 -c RcppExports.cpp -o RcppExports.o
   C:/RBuildTools/3.5/mingw_64/bin/g++ -m64 -std=c++1y  -I"C:/Users/n15059/DOCUME~1/R/R-36~1.2/include" -DNDEBUG -I"../inst/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/n15059/Documents/R/R-3.6.2/library/BH/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/Rcpp/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/RcppEigen/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/rstan/include" -I"C:/Users/n15059/Documents/R/R-3.6.2/library/StanHeaders/include"        -O3 -march=corei7 -mtune=corei7 -c stanExports_lm.cc -o stanExports_lm.o
   C:/RBuildTools/3.5/mingw_64/bin/g++ -m64 -std=c++1y -shared -s -static-libgcc -o rstanlm.dll tmp.def RcppExports.o stanExports_lm.o -LC:/Users/n15059/DOCUME~1/R/R-36~1.2/bin/x64 -lR
   installing to C:/Users/n15059/AppData/Local/Temp/RtmpGgB2gm/devtools_install_3930363f264c/00LOCK-rstanlm/00new/rstanlm/libs/x64
-  DONE (rstanlm)
Error in Rcpp::loadModule(module = "stan_fit4lm_mod", what = TRUE, env = ns,  : 
  Unable to load module "stan_fit4lm_mod": Failed to initialize module pointer: Error in FUN(X[[i]], ...): no such symbol _rcpp_module_boot_stan_fit4lm_mod in package rstanlm

I finally found the solution. It is important to write the following lines of code in the NAMESPACE file, otherwise you will have the error mentioned above.

useDynLib(rstanlm, .registration = TRUE)

(Replace rstanlm with the name of your newly created package)

In fact, if someone executes rstan_create_package(path = 'rstanlm'), the Rstudio console indicates that you have to add these lines but will also open your new package in a new window at the same time. Since this happens in less than a second and this instruction is not indicated in the vignette, one (like me and others) can easily miss this instruction and struggle to run roxygen properly.

For future users, I think it would be great if either the step by step vignette is updated mentioning that it is important to add these lines, or create a NAMESPACE file which already contains these instructions.


Ah, then I bet I did one more thing: I likely renamed configure to configure.bak (or something similar), ran roxygen on it, then added configure back again. I don’t recall ever manually adding a line to the NAMESPACE file. Basically, I wanted to build the package without the dynamic lib, just to generate a namespace file that includes the useDynLib line. Then build it again with the rstan files available. After that, it should continue working fine.

It works! Thank you so much @Kbus007. I agree with you that it would be better a NAMESPACE file which already contains these instructions. I usually avoid to edit it by hand.


(Apologies for bumping a topic over a year old. I am not sure if this is considered necro-bumping, and therefore bad form. Apologies if so).

I wanted to add to this thread because I just ran into this issue now and was a bit stumped by it and I presume others will be too. The solution is, as @Kbus007 says, a matter of adding some lines to NAMESPACE, but we don’t do that (as least not usually) by editing NAMESPACE directly. We add roxygen2 statements to our R package -package.R file. You can see examples that in one section of the vignette. For example, if my package is foobar, I would have in the R/ directory a file named foobar-package.R with (at least) these lines:

## usethis namespace: start
#' @import Rcpp
#' @import methods
#' @importFrom rstan sampling
#' @useDynLib foobar, .registration = TRUE
## usethis namespace: end

When you build your documentation (e.g. Ctrl + Shift + D), the appropriate lines will be written to NAMESPACE.

As a final note, even after adding those lines, I still had to clean up a lot of files (deleting contents from src, deleting R/stanmodels.R, etc) before things were working again.

1 Like