How to Install Dependencies When Using "R Cmd Install" to Install R Packages

How to install dependencies when using R CMD INSTALL to install R packages?

Actually, re-reading the R extensions guide, it doesn't say that R CMD INSTALL will get dependencies from CRAN. The install.packages() method from within R will do that, but at first glance I don't think R CMD INSTALL does.

You can use install.packages to install from a .tar.gz, but you have to set repos=NULL, and then this applies:

 dependencies: logical indicating to also install uninstalled packages
on which these packages depend/suggest/import (and so on
recursively). Not used if repos = NULL.

I suspect the thing to do is to get the dependencies out of the DESCRIPTION file and then run R and do an install.packages() on those when you are testing your build in a clean environment.

Install dependencies of custom package

Figured it out.

The problem was a misunderstanding on my part regarding roxygen, which I've been using for my documentation. I assumed it handled the Imports: section of the DESCRIPTION file, which it doesn't ((1), (2)). So while the NAMESPACE file has all the necessary importFrom(pool, ...) calls, pool wasn't actually on my DESCRIPTION.

After fixing that oversight, using remote::install_local("path/to/pkg") (or devtools::install()) ((3)) worked: it installed my package and pulled its dependencies from CRAN.

Package listed under depends does not install when I rebuild a custom R Package

Solution

Use devtools::install() instead.

Explanation

According the RStudio website,

The Build and Reload command performs several steps in sequence to ensure a clean and correct result:

    1.Unloads any existing version of the package (including shared libraries ifnecessary).

    2.Builds and installs the package using R CMD INSTALL.

    3.Restarts the underlying R session to ensure a clean environment for re-loading the package.

    4.Reloads the package in the new R session by executing the library function.

While devtools::install() will install dependencies for you -- from help("install.packages"):

Uses R CMD INSTALL to install the package. Will also try to install
dependencies of the package from CRAN, if they're not already
installed.

(emphasis added) this is not the case for R CMD INSTALL alone (see ?INSTALL from R or R CMD INSTALL --help from the command line, etc. -- there is no mention of installing required dependencies).

So, it appears the language

In fact, any time your package is installed, those packages will, if
not already present, be installed on your computer
(devtools::load_all() also checks that the packages are installed).

from Hadley's R Packages is a little specific; it does not pertain to using R CMD INSTALL (which RStudio's build function apparently uses), but does work for devtools::install(). It's a matter of personal taste, but honestly I highly recommend using devtools in your package development workflow.

Example

I removed the package rbenchmark from my system via

remove.packages("rbenchmark")

then created a dummy package by

devtools::create("SOexample", rstudio = FALSE)

and edited the DESCRIPTION to put rbenchmark in Imports, so that SOexample depends on it. I added the following code in R/hello_world.R:

hello_world <- function() print("Hello, world!")

I tried R CMD INSTALL, but got the error

*installing to library ‘/home/duckmayr/R/x86_64-pc-linux-gnu-library/3.5’

ERROR: dependency ‘rbenchmark’ is not available for package ‘SOexample’

*removing ‘/home/duckmayr/R/x86_64-pc-linux-gnu-library/3.5/SOexample’

But, if I try devtools::install():

> devtools::install("SOexample/")
Installing SOexample
trying URL 'https://cloud.r-project.org/src/contrib/rbenchmark_1.0.0.tar.gz'
Content type 'application/x-gzip' length 5093 bytes
==================================================
downloaded 5093 bytes

Installing rbenchmark
'/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet \
CMD INSTALL '/tmp/RtmpA0NOMe/devtools723832018149/rbenchmark' \
--library='/home/duckmayr/R/x86_64-pc-linux-gnu-library/3.5' --install-tests

* installing *source* package ‘rbenchmark’ ...
** package ‘rbenchmark’ successfully unpacked and MD5 sums checked
** R
** demo
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (rbenchmark)
'/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet \
CMD INSTALL '/home/duckmayr/SOexample' \
--library='/home/duckmayr/R/x86_64-pc-linux-gnu-library/3.5' --install-tests

* installing *source* package ‘SOexample’ ...
** R
** byte-compile and prepare package for lazy loading
** help
No man pages found in package ‘SOexample’
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (SOexample)

Package dependencies when installing from source in R

You could make your own repository and set repos to be a vector of the places to look for packages, where it would start with your own repository and then include a link to a CRAN mirror. This is what I do and it works quite nicely, as then I can easily share my packages with others and update them from whatever computer I happen to be on.

How does R check for system external dependencies when installing an R package?

Briefly:

  1. This is all in Writing R Extension.
  2. In short, it doesn't i.e. R does not help you. See below.
  3. CRAN (and Bioconductor) package dependencies can be declared.
  4. For everything else you can only approximate via SystemRequirements in DESCRIPTION
  5. This is not a solved or solvable problem of all depends across all operating systems
  6. So no other language has it either.
  7. What one can get is a package manager in a 'vertical' stack -- say brew or apt. That is pretty good, but it also does not work at the CRAN source level.
  8. So all one can do is, as you show, invoke something executable called configure to test if resources are present, and to abort with clear errors if not.
  9. For the package you show, that test code in configure is here.
  10. There is a sibbling Windows version too.


Related Topics



Leave a reply



Submit