Rstantools gives object 'm' not found error even after totally fresh install of R, Rstudio, Rtools, Rstan

I’m trying to create a package to use as a personal repository of pre-compiled Stan models and utility functions I use on a regular basis. Everything was going fine, but I started to get the error “Error in is(module, “character”) : object ‘m’ not found” when trying to document the package. The manual files for everything in the package ended up being deleted somehow after this. I spent hours trying to figure out the issue, but no luck. I ended up uninstalling R, R Studio, Rtools and deleting all of the files and libraries from my computer to get a completely fresh start.

After getting everything set up again, with the latest versions, I try to create a package.

rstan_package_skeleton(path="Stanpackage2")

Everything goes fine, and it gets to the following

  • installing source package ‘Stanpackage2’ … (955ms)
    ** libs
    C:/Rtools/mingw_64/bin/g++ -m64 -std=c++1y -I"C:/PROGRA~1/R/R-35~1.1/include" -DNDEBUG -I"…/inst/include" -I""C:/PROGRA~1/R/R-35~1.1/bin/x64/Rscript" --vanilla -e "cat(system.file('include', 'src', package = 'StanHeaders'))"" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -I"C:/Users/Brandon/Documents/R/win-library/3.5/BH/include" -I"C:/Users/Brandon/Documents/R/win-library/3.5/Rcpp/include" -I"C:/Users/Brandon/Documents/R/win-library/3.5/RcppEigen/include" -I"C:/Users/Brandon/Documents/R/win-library/3.5/rstan/include" -I"C:/Users/Brandon/Documents/R/win-library/3.5/StanHeaders/include" -O3 -Wno-unused-variable -Wno-unused-function -c init.cpp -o init.o
    C:/Rtools/mingw_64/bin/g++ -m64 -std=c++1y -shared -s -static-libgcc -o Stanpackage2.dll tmp.def init.o -LC:/PROGRA~1/R/R-35~1.1/bin/x64 -lR
    installing to C:/Users/Brandon/AppData/Local/Temp/RtmpeKWLei/devtools_install_1a945327d00/Stanpackage2/libs/x64
  • DONE (Stanpackage2)

After which it immediately throws this same error!

In R CMD INSTALL
Error in is(module, “character”) : object ‘m’ not found

What is this error, and what can I do to fix this that a complete re-installation of the R environment won’t fix?

Below is the session/system info:

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

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

other attached packages:
[1] Rcpp_0.12.19 rstantools_1.5.1

loaded via a namespace (and not attached):
[1] rstan_2.18.1 RcppEigen_0.3.3.4.0 clisymbols_1.2.0 tidyselect_0.2.5
[5] purrr_0.2.5 lattice_0.20-35 colorspace_1.3-2 usethis_1.4.0
[9] stats4_3.5.1 loo_2.0.0 base64enc_0.1-3 rlang_0.3.0.1
[13] pkgbuild_1.0.2 pillar_1.3.0 glue_1.3.0 withr_2.1.2
[17] bindrcpp_0.2.2 matrixStats_0.54.0 plyr_1.8.4 bindr_0.1.1
[21] stringr_1.3.1 munsell_0.5.0 commonmark_1.6 gtable_0.2.0
[25] inline_0.3.15 callr_3.0.0 ps_1.2.0 parallel_3.5.1
[29] backports_1.1.2 scales_1.0.0 desc_1.2.0 pkgload_1.0.1
[33] Stanpackage2_0.0.0.9000 StanHeaders_2.18.0 jsonlite_1.5 fs_1.2.6
[37] gridExtra_2.3 ggplot2_3.1.0 stringi_1.2.4 processx_3.2.0
[41] gh_1.0.1 dplyr_0.7.7 grid_3.5.1 rprojroot_1.3-2
[45] BH_1.66.0-1 cli_1.0.1 tools_3.5.1 magrittr_1.5
[49] lazyeval_0.2.1 tibble_1.4.2 crayon_1.3.4 whisker_0.3-2
[53] pkgconfig_2.0.2 Matrix_1.2-14 xml2_1.2.0 prettyunits_1.0.2
[57] assertthat_0.2.0 roxygen2_6.1.0 httr_1.3.1 rstudioapi_0.8
[61] R6_2.3.0 compiler_3.5.1

1 Like

At the moment, you need to specify the stan_files argument to rstan_package_skeleton. The vignette says you can add the Stan programs later but that actually became false when roxygen2 started compiling all the C++ code in order to generate the documentation from the R sources.

Thank you soo much! You know, it was only after I happend to update roxygen2 that I started having that problem, and obviously the newest roxygen2 would get installed with the new R installation, so that makes sense. One question though, if I want to add more stan programs later do I need to build an entirely new package skeleton?

No, you can add more Stan programs but you have to start with at least one until the next rstantools comes out.

After trying to add some new stan files to my package I’m having a bit of trouble. This is probably a dumb question, but how do you force the package to re-compile so that the .o and .hpp files are generated for the package?

So far I’ve attempted two solutions that almost work, but don’t.

The first:

  1. Comment out the function in the zzz file to prevent Error in is(module, “character”) : object ‘m’ not found
  2. roxygen2::roxygenise(clean=TRUE)

This doesn’t really work, of course, because the stan files are not compiled.

Solution number 2 technically works but is a pain in the posterior.

  1. Cut and paste the R functions, description file, and manual pages into another folder.
  2. Delete the package directory.
  3. Create a brand new project with rstan_package_skeleton.
  4. Paste back the R files after the stan models compiles so the package is whole again.

They will get build when you do R CMD build or R CMD INSTALL --preclean outside of RStudio or within RStudio if you do devtools::install or something like that.

Should I just leave the zzz file (the .onLoad, specifically) commented out then? Is it of any particular importance when installing? If I don’t comment it out or delete the file, I’m not able to generate the manual files due to the Error in is(module, “character”) : object ‘m’ not found error. It seems that .onLoad just adds them to the namespace on package loading, though I reference them by stanpackage:::stanmodels$modelname in the R functions that call them so I’m not sure if that’s necessary.

Again I apologize if this seems like a trivial question, just would rather avoid waiting on things to compile when I try to install if it’s not going to work. Thanks for your help!

I think once you have compiled any Stan programs in the package, then the .onLoad() function in zzz.R should be fine.

Hm, even after just dropping the stan files into the package and installing it, it claims the models are not found even though packagename$stanmodels$modelname shows they are indeed part of the package.

Everything seemed to get all wonky like this after the roxygen2 update a while back (per my original posts in the thread). I guess the best option is to just re-do the package and pasting the R functions back in afterwards. This one’s been a real head scratcher for me!

You do you have a link to a GitHub repo. I can fix up whatever is the problem.

That was supposed to be written as a question: Do you have a link to a GitHub repo?

Not for this package, but I can make one. I intended to in any case since I work from home sometimes and work from the university at other times. Unfortunately my computer is already in the process of rebuilding the package from scratch, but I’m sure I’ll be adding a new function fairly soon. Do you mind if I post back or PM you when I have something to demonstrate the issue?

Sure