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.
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.
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:
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
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.
@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.
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.