devtools equivalent of RStudio Build panel buttons - r

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)

Related

R package installation error: "cannot remove earlier installation"

I'm currently making a R package (tessellation) which includes some C code. When I do some modifications and click "Install and restart" in RStudio, I get this error:
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source tessellation
* installing to library 'C:/PortableApps/R/R-4.1.2/App/R-Portable/library'
* installing *source* package 'tessellation' ...
ERROR: cannot remove earlier installation, is it in use?
When I do "Restart R session" before "Install and restart", sometimes this works, sometimes not. So I have to close the project and reopen it, and this is annoying. Do you know what could I do to work more conveniently?
You need to be sure that the process is not being used (or loaded into R's search path) for the package. I suggest stopping all R sessions, deleting the installed package folder manually, start an R vanilla session, check the attached packages/search path, and finally attempt re-installation/building the package:
# from terminal
R --vanilla
# check search path
ls()
search()
# attempt installing package
devtools::install() # or devtools::build()

Install R packages through "Jobs" tab in Rstudio

In Rstudio, I usually install R packages using the console by install.packages('pkg-name') command.
However, when some R packages are required in an R script, Rstudio opens a pop-up and asks if I want to install those packages. If I click on "Install", it starts installing those packages inside the "Jobs" tab. This is particularly useful for me because my internet is slow and while a large package is being installed I can continue my works on the console tab. I want to know if there is any way to always install packages through this "Jobs" tab, without using the console.
There's an R package {job} which can be used to run any r code directly from a script or console. To install a package as a Rstudio job:
# install.packages("job")
job::job({
install.packages("pkg_name")
})
Also {job} provides Rstudio Addins to easily run selected code as a job.
see here for more examples.
You can create a script, let's call it temp.R and include the install.packages command in it. Something like :
install.packages('dplyr', repos = "http://cran.us.r-project.org")
install.packages('tidyr', repos = "http://cran.us.r-project.org")
You can click on Start Local Job
and point to the location of the script and adjust any other setting that you want.
Now click on Start and use R/RStudio while the script finishes in the background.

No manual pdf created during build of R package

I see that my recent package build no longer creates a manual even though I have not added "--no-manual" to the build settings. I didn't have this problem before - I have recently updated both devtools and roxygen2.
Am I missing something?
Here is how I set my project options:
A solution would just be to run $ R CMD Rd2pdf <package name> if the package is already built, or uncheck Use devtools package functions if available
For whatever reason, devtools injects --as-cran --no-manual into the build process, even though you don't specify it as a user (you can see this in your build log in RStudio).

Reload updated package in R

I used to develop some package for R. So, this is an iterative process where I need to check the working of the package in between by installing it via rstudio, and then again adding some necessary functionalities. So, I used the following process:
create a R package
install it via command prompt: R CMD install <{package_name}>
load the package in rstudio as, library(package_name)
Check the necessary functionality
detach package in rstudio using, detach(package:{package_name})
remove package via command prompt as: R CMD remove <{package_name}>
add/update package
repeat steps 2 - 7,until package is fully developed.
Now the problem is that everytime I close rstudio after step 5, otherwise updated package after installation is not reflected in R.
So, How can I avoid to close rstdio everytime; and always get updated copy of installed package. In other words, I don't want to close rstudio everytime. I have found that detach( ) is not effective.
Note: I use rstudio only for checking the functionality of package. I check,build for building packages on command prompt using R CMD check/build commands
RStudio has functionality for building packages that I does what I think you have described.
Basically, use 'new project' and select the R-package option or just open an old project using the '.Rproj' file
Then use the build and reload each time you make a change to the package
and want to reload the package in (see pic).
Seems to work OK for me.
See link for more details:
https://support.rstudio.com/hc/en-us/articles/200486488-Developing-Packages-with-RStudio

Using --vanilla with R CMD build

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.

Resources