Using rstan() in R package: Error with R CMD check

I am developing an R package that uses rstan (a few functions include sampling(), extract()).

When I run:
R CMD check --as-cran packagename
I get the following error:
Error in loadNamespace(j ← i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
there is no package called ‘rstan’

However, when I run devtools::check() I do not get this error.

In my DESCRIPTION file, I have under Imports:
rstan (>= 2.18.1),
rstantools (>= 2.3.1.1)

And in the NAMESPACE file, I have:
importFrom(rstan,sampling)

The line in the NAMESPACE file was added when the R package skeleton was first created with rstan_create_package().

Also within the functions in the package, every time I use a function in the rstan package, I use rstan::function().

Does anyone have any suggestions? Should I just try to submit to Cran since devtools::check() is working without errors?

Do you have the package code on GitHub or another public repository somewhere? I can try running R CMD check on it too and see what happens. That’s strange that devtools::check() runs fine but R CMD check errors like that.

Yep! Here’s the repository: GitHub - abigailkeller/eDNAjoint: R package eDNAjoint

Thanks so much for your help – let me know if you have any problems or if the way I’ve synced to github is creating challenges.

This sort of thing has happened to me when R looks for packages in different places, depending on how R was opened. For example, if you did devtools::check() from inside Rstudio, then maybe it is looking for packages in a different folder as compared to the command-line R CMD check.

You might open R from the command line (by typing R), then .libPaths() will show the folders where it is looking for packages (maybe R CMD check only looks in the first folder? not sure). If you do install.packages("rstan") while you are there, it usually gets installed where needed and works. This is maybe not the most elegant solution (you could also point R to the folder where rstan is installed), but possibly the easiest solution.

1 Like

I don’t get any errors, so I would try @edm’s suggestion and see if that avoids the error for you.

I ran R CMD check and I got a few Notes but no Errors. So this seems ready to submit once you fix the issues in the Notes and one other issue I ran into:

  1. I get a Note from R CMD check:
   Namespaces in Imports field not imported from:
     ‘RcppParallel’ ‘rstantools’
     All declared Imports should be used.

We should probably handle this automatically in rstantools, but until then you can fix this by adding these lines to eDNAjoin-package.R

#' @importFrom rstantools rstan_config
#' @importFrom RcppParallel RcppParallelLibs

and regenerating the doc (this will update the NAMESPACE file).

  1. The checking examples step of R CMD check took a long time and gives the note
   Examples with CPU (user + system) or elapsed time > 5s
                          user system elapsed
   detection.plot      121.701 26.082 148.616
   modelselect         125.371 21.328 147.265
   jointModel          101.390 13.173 115.150
   traditionalModel     99.586 14.685 114.640
   detection.calculate  99.605 12.584 112.524
   mu.critical          99.435 11.810 111.618
   summarize            78.719  8.749  87.730

You can keep those examples but avoid CRAN running them by wrapping them in \donttest{}, e.g.

#' @examples
#' \donttest{
#' EXAMPLE CODE
#' }

Or you can use \dontrun to avoid running them ever. cran - When and when not to use \donttest and \dontrun in R package development? - Stack Overflow

  1. Before I could get it to run at all I got this error:
ERROR: 'configure' exists but is not executable -- see the 'R Installation and Administration Manual'

I’m not sure why the configure files aren’t executable (I haven’t seen that before) but before I could get it to work I had to open up the terminal cd to the eDNAjoint folder and then change the permissions using:

chmod +x configure
chmod +x configure.win 

I would run that just in case and update the files on GitHub if that changes anything before submitting to CRAN.

Hope that helps!

@jonah and @edm

Wow – thanks to you both so much for your help!

@edm suggestion was exactly right – I had been working on a remote server where I had some packages in my user library and some packages in my system library. rstan was in my user library, and I assume the build was defaulting to the system library. My solution was just to build the package locally, and the problem went away!

And @jonah thanks for your advice – I just implemented your suggestions.

Thanks so much to you both!

2 Likes

Hi @edm thanks so much for your response! I’m wondering if you know how to indicate the library to use when building the package?

@edm Do you mean that .libPaths(‘path_I_want’) should be somewhere in the R package? Like in the NAMESPACE?

I think that .libPaths() only shows you the folders where R looks for packages; it doesn’t let you set a folder. I think folders are typically set in R (as opposed to in the package), before you build the package. To set the folder, you can set an environment variable R_LIBS to point at the folder you want. To do this automatically (on linux, at least), I have a file in my home folder called .Renviron with this line:

R_LIBS=/usr/lib/R/site-library

and then R looks for packages in that folder first.

Another thing that often works is to decide how you want to build the package, then ensure you have updated all your packages under that R instantiation. For example, if you wanted to always build your package from Rstudio, then I’d open Rstudio and ensure all the other packages were installed and updated in Rstudio before I tried to build my package there. So instead of using R_LIBS to set the folder yourself, you use whatever folder Rstudio is already using and make sure the packages are all installed/updated there.

2 Likes

That worked!! Thanks so much for your help. I appreciate it!

1 Like