Unable to build package with rstan ('unable to load module error')

Hi all, I’m trying to install my own package that has Stan files in and I get an error to say that I cannot load the Stan modules. I have tried to do:

pkgbuild::compile_dll()

before installing the package, but when I use devtools::install() after, the output I get is the following:

Running "C:/PROGRA~1/R/R-36~1.3/bin/x64/Rcmd.exe" INSTALL \
  "C:\Users\Chan\AppData\Local\Temp\RtmpsFHEMP/mixGaussianFusion_0.0.0.9000.tar.gz" --install-tests 
* installing to library 'C:/Users/Chan/Documents/R/win-library/3.6'
* installing *source* package 'mixGaussianFusion' ...
** using staged installation
** libs
\
*** arch - i386
-
\
C:/Rtools/mingw_32/bin/g++ -m32 -std=c++1y  -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"../inst/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/Chan/Documents/R/win-library/3.6/BH/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/RcppEigen/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/rstan/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include"        -O3 -march=corei7 -mtune=corei7 -c RcppExports.cpp -o RcppExports.o
/
-
C:/Rtools/mingw_32/bin/g++ -m32 -std=c++1y  -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"../inst/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/Chan/Documents/R/win-library/3.6/BH/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/RcppEigen/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/rstan/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include"        -O3 -march=corei7 -mtune=corei7 -c stanExports_mixture_gaussian.cc -o stanExports_mixture_gaussian.o
C:/Rtools/mingw_32/bin/g++ -m32 -std=c++1y -shared -s -static-libgcc -o mixGaussianFusion.dll tmp.def RcppExports.o stanExports_mixture_gaussian.o -LC:/PROGRA~1/R/R-36~1.3/bin/i386 -lR
installing to C:/Users/Chan/Documents/R/win-library/3.6/00LOCK-mixGaussianFusion/00new/mixGaussianFusion/libs/i386
/
*** arch - x64
|
/
C:/Rtools/mingw_64/bin/g++ -m64 -std=c++1y  -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"../inst/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/Chan/Documents/R/win-library/3.6/BH/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/RcppEigen/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/rstan/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include"        -O3 -march=corei7 -mtune=corei7 -c RcppExports.cpp -o RcppExports.o
|
/
C:/Rtools/mingw_64/bin/g++ -m64 -std=c++1y  -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"../inst/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include/src" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/Chan/Documents/R/win-library/3.6/BH/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/Rcpp/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/RcppEigen/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/rstan/include" -I"C:/Users/Chan/Documents/R/win-library/3.6/StanHeaders/include"        -O3 -march=corei7 -mtune=corei7 -c stanExports_mixture_gaussian.cc -o stanExports_mixture_gaussian.o
C:/Rtools/mingw_64/bin/g++ -m64 -std=c++1y -shared -s -static-libgcc -o mixGaussianFusion.dll tmp.def RcppExports.o stanExports_mixture_gaussian.o -LC:/PROGRA~1/R/R-36~1.3/bin/x64 -lR
installing to C:/Users/Chan/Documents/R/win-library/3.6/00LOCK-mixGaussianFusion/00new/mixGaussianFusion/libs/x64
** R
** inst
** byte-compile and prepare package for lazy loading
No man pages found in package ** help
 'mixGaussianFusion' 
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
*** arch - i386
Error: package or namespace load failed for 'mixGaussianFusion' in .doLoadActions(where, attach):
 error in load action .__A__.1 for package mixGaussianFusion: Rcpp::loadModule(module = "stan_fit4mixture_gaussian_mod", what = TRUE, : Unable to load module "stan_fit4mixture_gaussian_mod": Failed to initialize module pointer: Error in FUN(X[[i]], ...): no such symbol _rcpp_module_boot_stan_fit4mixture_gaussian_mod in package mixGaussianFusion

Error: loading failed
Execution halted
*** arch - x64
Error: package or namespace load failed for 'mixGaussianFusion' in .doLoadActions(where, attach):
 error in load action .__A__.1 for package mixGaussianFusion: Rcpp::loadModule(module = "stan_fit4mixture_gaussian_mod", what = TRUE, : Unable to load module "stan_fit4mixture_gaussian_mod": Failed to initialize module pointer: Error in FUN(X[[i]], ...): no such symbol _rcpp_module_boot_stan_fit4mixture_gaussian_mod in package mixGaussianFusion

Error: loading failed
Execution halted
ERROR: loading failed for 'i386', 'x64'
* removing 'C:/Users/Chan/Documents/R/win-library/3.6/mixGaussianFusion'
Error in (function (command = NULL, args = character(), error_on_status = TRUE,  : 
  System command 'Rcmd.exe' failed, exit status: 1, stdout & stderr were printed

From typing in .Last.error.trace, I get:

> .Last.error.trace

 Stack trace:

 1. devtools::install()
 2. pkgbuild::with_build_tools(required = FALSE, callr::rcmd("INSTALL",  ...
 3. withr::with_path(rtools_path(), code)
 4. base:::force(code)
 5. callr::rcmd("INSTALL", c(install_path, opts), echo = !quiet,  ...
 6. callr:::run_r(options)
 7. base:::with(options, with_envvar(env, do.call(processx::run,  ...
 8. base:::with.default(options, with_envvar(env, do.call(processx::run,  ...
 9. base:::eval(substitute(expr), data, enclos = parent.frame())
 10. base:::eval(substitute(expr), data, enclos = parent.frame())
 11. callr:::with_envvar(env, do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 12. base:::force(code)
 13. base:::do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 14. (function (command = NULL, args = character(), error_on_status = TRUE,  ...
 15. throw(new_process_error(res, call = sys.call(), echo = echo,  ...

 x System command 'Rcmd.exe' failed, exit status: 1, stdout & stderr were printed 

Did you create your package with rstantools?

yes, i used rstantools::rstan_create_package to create the package

if I use devtools::document(), I get this error:

> devtools::document()
Updating mixGaussianFusion documentation
Loading mixGaussianFusion
Error in Rcpp::loadModule(module = "stan_fit4mixture_gaussian_mod", what = TRUE,  : 
  Unable to load module "stan_fit4mixture_gaussian_mod": Failed to initialize module pointer: Error in FUN(X[[i]], ...): no such symbol _rcpp_module_boot_stan_fit4mixture_gaussian_mod in package mixGaussianFusion

No (full) solution - but I’ve just had exactly the same.

rstan 2.19.3
rstantools 2.0.0

I was trying to add stan functionality to an existing package through using rstantools::use_rstan(). I noticed that this did not generate a “-package.R” file; but manually copying a version across didn’t work.

Partial solution (at least for me) - I’ve subsequently started afresh using rstantools::rstan_create_package() and this seems to work.

I had similar troubles too. If you could install your package before and you are in windows might be your package got corrupted

Enter to the file: Doucments/R/3.6 and check if a file 00LOCK-mixGaussianFussion exist, if it does, erase it and install it again that should work.

If you cannot compile your package at all you have to had some stuff to your Makevars file

The only way I could manage to install my package from github automatic was changing my makefile to this:

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -mtune=native CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y
CXX14 = g++ -std=c++1y
CXX14FLAGS = -O3 -Wno-unused-variable -Wno-unused-function

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

CXX14FLAGS=-O3 -march=native -mtune=native
CXX11FLAGS=-O3 -march=native -mtune=native

That before works on windows and Linux have not try in mac yet. Hope you fix it soon! :)

Hi! I was trying to update a package of mine following the new guidelines and stumbled upon similar issues.
I then followed the step by step guide using rstantools to construct a test package to try and understand what’s going on and I am unable to install this simple example package.
roxygen2::roxygenize() returns the error:

Loading test
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 test

devtools::install(quick = FALSE)
gives the following error:

Error in (function (command = NULL, args = character(), error_on_status = TRUE,  : 
  System command 'R' failed, exit status: 1, stdout & stderr were printed

Could this be a bug?

Thanks in advance for any help!

sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8      
 [2] LC_NUMERIC=C              
 [3] LC_TIME=fr_FR.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8   
 [6] LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8      
 [8] LC_NAME=C                 
 [9] LC_ADDRESS=C              
[10] LC_TELEPHONE=C            
[11] LC_MEASUREMENT=fr_FR.UTF-8
[12] LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6          lattice_0.20-40      
 [3] prettyunits_1.1.1     ps_1.3.3             
 [5] assertthat_0.2.1      rprojroot_1.3-2      
 [7] digest_0.6.25         R6_2.4.1             
 [9] backports_1.1.7       stats4_3.6.3         
[11] ggplot2_3.3.1         pillar_1.4.4         
[13] rlang_0.4.6           curl_4.3             
[15] rstudioapi_0.11       callr_3.4.3          
[17] Matrix_1.2-18         desc_1.2.0           
[19] devtools_2.3.0        stringr_1.4.0        
[21] RcppEigen_0.3.3.7.0   loo_2.2.0            
[23] munsell_0.5.0         compiler_3.6.3       
[25] xfun_0.14             rstan_2.19.3         
[27] pkgconfig_2.0.3       pkgbuild_1.0.8       
[29] rstantools_2.1.0.9000 tidyselect_1.1.0     
[31] tibble_3.0.1          gridExtra_2.3        
[33] roxygen2_7.1.0        matrixStats_0.56.0   
[35] fansi_0.4.1           crayon_1.3.4         
[37] dplyr_1.0.0           withr_2.2.0          
[39] BH_1.72.0-3           grid_3.6.3           
[41] gtable_0.3.0          lifecycle_0.2.0      
[43] magrittr_1.5          StanHeaders_2.21.0-5 
[45] scales_1.1.1          RcppParallel_5.0.1   
[47] cli_2.0.2             stringi_1.4.6        
[49] fs_1.4.1              remotes_2.1.1        
[51] testthat_2.3.2        xml2_1.3.2           
[53] ellipsis_0.3.1        generics_0.0.2       
[55] vctrs_0.3.1           tools_3.6.3          
[57] glue_1.4.1            purrr_0.3.4          
[59] processx_3.4.2        pkgload_1.1.0        
[61] parallel_3.6.3        yaml_2.2.1           
[63] inline_0.3.15         colorspace_1.4-1     
[65] sessioninfo_1.1.1     memoise_1.1.0        
[67] knitr_1.28            usethis_1.6.1   

No, it is just that roxygen2 breaks things every month. If you do

example(source) # defines the sourceDir() function
roxygen2::roxygenize(load_code = sourceDir)
pkgbuild::compile_dll()

instead of what it says in the vignette, then it should work until next month.

1 Like

Thanks for this quick reply!

example(source) # defines the sourceDir() function
roxygen2::roxygenize(load_code = sourceDir)

returns an error:

Error: Can't extract an environment from a call

However, simply running:

example(source) # defines the sourceDir() function
roxygen2::roxygenize()

solves the issue and now everything works like a charm.

Thanks again, you just made my day.

1 Like