Using --vanilla with R CMD build - r

According to the document Writing R Extensions ss 1.3 "R CMD check and R CMD build run R with --vanilla, so none of the user's startup files are read".
This doesn't seem to happen on my installation. I have found that my version of R (2.15.1 on Mac) loads the packages in my .Rprofile - i am pretty sure about this, as i've managed to reliably break it by adding library(hobblegobble) to my .Rprofile.
does this matter? If so, should i always prefer to build packages with
R --vanilla CMD build
thanks

You can use either R --vanilla CMD build myPackage or R --vanilla CMD INSTALL --build myPackage if your .Rprofile loads packages that you are trying to build (or install).
The only drawback I am aware of to using --vanilla is that the terminal's completion (i.e. having the package directory name completed for you when you tap tab) may not work while you're typing the path to the package directory.
However, a better solution may be to wrap if (interactive()) {} around code in your .Rprofile that you only want to be run in interactive sessions. e.g. library or require calls.
In your case if your .Rprofile had if (interactive()) library(hobblegobble) you wouldn't need to use --vanilla.

Related

R CMD build versus devtools::build() (Non-standard file/directory found at top level)

I am writing an R package and build and test it with:
Rscript -e "devtools::document()" && R CMD build . && Rscript -e "devtools::test();devtools::check()"
I get a note:
checking top-level files
Non-standard file/directory found at top level:
‘PosteriorBootstrap_0.0.1.tar.gz’
I get the note whether devtools::check() comes first or second.
This
thread suggests:
The note is telling you that you usually shouldn't have a file called
build in the top level of your package.
The tar.gz file is created by R CMD build and I get the same error even if I delete it before running it.
This
thread
suggests adding the tar.gz file to .Rbuildinore, which removes the note.
Another way to remove it is to run everything from devtools:
Rscript -e "devtools::document(); devtools::build(); devtools::load_all('.'); devtools::test(); devtools::check()"
And then I don't get that note.
What's the difference between R CMD build and devtools::build() and why does the former throw that note?
You are combining a number of steps that perform similar and/or competing functions. I would suggest reading this for a best-practice build and check workflow.
When you run R CMD build it builds the package to the current directory, which is the top level package directory. Therefore, when you run your checks, it sees the .tar.gz file in the package root, which is a non-standard file to have in a package, thus the warning. devtools::build() is smart and builds the package to the package parent directory (regardless of where you are calling it from). Trying to call R CMD commands mixed with devtools functions can create issues, because devtools also calls R CMD commands, so you may be duplicating actions at various points in time or causing commands to be called in the incorrect order.
Per the link above, a best-practice workflow would be:
Rscript -e "devtools::document();devtools::check();devtools::build()"
called from the package root, and you avoid dealing with R CMD altogether. If you want to use R CMD it would look something like this:
Rscript -e "devtools::document()" && cd .. && R CMD build PosteriorBootstrap && R CMD check PosteriorBootstrap*.tar.gz
starting in the package root and then changing to the parent directory.

devtools equivalent of RStudio Build panel buttons

I am making an R package using RStudio. I am comfortable using the buttons on the Build panel. I have a script that I would like to run each time I Build & Reload or Clean and Rebuild. I'd like to write a function that runs my script and then executes the devtools commands associated with one of those buttons, but I am having trouble finding documentation of the correspondence between those buttons and devtools commands. The buttons are as follows:
Build & Reload
Check
Load All
Clean and Rebuild
Test Package
Check Package
Build Source Package
Build Binary Package
For each of the items in that list, what devtools R code would I run to cause the exact same behavior?
In RStudio you can check "Use devtools package functions if available" in Project Options > Build Tools and you can see what devtools functions will be used. If you look at the build console pane, you can check out what RStudio runs. General cases if using devtools:
Build & Reload
devtools::build()
devtools::reload() is possibly an option but Rstudio uses R CMD INSTALL --no-multiarch --with-keep.source <pkgNameGoesHere>
Check
devtools::check()
Load All
devtools::load_all(".")
Clean and Rebuild
R CMD INSTALL --preclean --no-multiarch --with-keep.source <pkgNameGoesHere>
Test Package
devtools::test()
Check Package
devtools::check() (same as Check button)
Build Source Package
devtools::build()
Build Binary Package
devtools::build(binary = TRUE, args = c('--preclean'))
More info at the readme in the devtools repo.
To execute the Clean & Rebuilt action from RStudio inside R you can use the R function
system()
Executing
system("R CMD INSTALL
--preclean
--no-multiarch
--with-keep.source <your_package_name>")
Executes the Shell command from within your R session. Be aware that you will have to refer to the correct location of your package if you run this outside the Package Project (e.g. from another project or session)

R CMD check with specified library path

I am working on a package which I can load using devtools. But
R CMD check asm
gives me an error message
Error : package ‘seedDisp’ required by ‘asm’ could not be found
Which is kind of obvious as it is not installed in the global library.
But: I have installed installed in a local library (./library) and I have a .Rprofile file in the directory from which I run the checks as follow:
.libPaths(normalizePath("library"))
So when I run R and use devtools and load_all(.) it loads as the package seedDisp is installed.
It seems that R CMD check does ignore the library location which is set via the .Rprofile file.
So I tried
R CMD CHECK -l ./library asm_0.0.1.tar.gz
but it seems that -l only is used to install in and not to look for installed packages.
How can I tell R CMD check to look for installed packages in the library at ./library ?
One way to do this is via the R_LIBS_USER variable so I generally do
R_LIBS_USER=/some/other/path R CMD check asm_0.0.1.tar.gz
If that variable is generally set on your system, you need to do the usual trick of appending, or just set it in your shell via e.g.
export R_LIBS_USER="/some/other/path:${R_LIBS_USER}"
R CMD check asm_0.0.1.tar.gz
This mechanism is independent of how you call R CMD check it will work with or with devtools.

R CMD install : clean libs before a new installation

If there is any option I can set to R CMD INSTALL to remove all installed libs ( under package installation directory) before a new installation?
I tried
R CMD install -c --preclean
But it does not work. Should I create a clean config?
PS : I am under windows.
EDIT more context:
I am developing a package. I generate the lib automatically at each build. So I need to remove the old ones each time I have a modification. Removing the entire library is Ok for me also.
I know this is old, but I just ran into this myself. I am assuming here by "installed libs" you mean libraries internal to your package on which your package depends. (In my case, it is libzip and libzippp that I am cleaning up.)
My solution was here: https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Configure-and-cleanup
Under a Unix-alike only, an executable (Bourne shell) script cleanup is executed as the last thing by R CMD INSTALL if option --clean was given, and by R CMD build when preparing the package for building from its source.
I created a file named "cleanup" that is a Bash script and change directories, call make distclean and remove so and o files there. This is called at the beginning and the end of the build/install process. The cleanup script is called from within the package directory.

Roxygen, package building, and use.Rd2=TRUE

I have a simple shell script that builds my Roxygen documents, builds the package, checks, then installs the newly built package on my machine. It's quite simple:
#! /bin/sh
R CMD roxygen -d myPackage
R CMD build myPackage/
R CMD check myPackage_0.01.tar.gz
R CMD INSTALL myPackage myPackage_0.01.tar.gz
But I'm having issues with Roxygen picking up my .onLoad() function as described previously on StackOverflow. The solution is to use the use.Rd2=TRUE option with roxygenize. Well I want to build from the command prompt so I changed this line
R CMD roxygen -d myPackage
to the following line which shoves a roxygenize line to R through the stdin:
echo 'require("roxygen"); roxygenize("myPackage", roxygen.dir="myPackage",
copy.package=FALSE, use.Rd2=TRUE)' | R --no-save < /dev/stdin
This seems to work just dandy. But it feels a little convoluted. Is there an easier and/or more elegant way?
I do something similar, but I use a HERE document in the shell script to make it look cleaner.
#!/bin/sh
##
##
## Must be run from the parent of the package directory (no options
## to change target of check or tarball!?!)
PACKAGE="analyzeNMON"
VERSION=$(awk -F": +" '/^Version/ { print $2 }' ${PACKAGE}/DESCRIPTION)
R --no-restore --slave <<EOR
library(roxygen)
roxygenize(package.dir="${PACKAGE}",
roxygen.dir="${PACKAGE}",
use.Rd2=TRUE,
overwrite=TRUE,
copy.package=FALSE,
unlink.target=FALSE)
EOR
R CMD build ${PACKAGE}
R CMD check ${PACKAGE}_${VERSION}.tar.gz
R CMD INSTALL ${PACKAGE}_${VERSION}.tar.gz
The R code is very similar to that in the script run during R CMD roxygen.
The roxygen that is installed on my system (version 0.1; installed from CRAN this week) doesn't seem to support the -s option mentioned above...
May be the R CMD roxygen -s option will help here. I believe it is effectively the same as setting use.Rd2=TRUE in the roxygenize function.

Resources