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

RDependenciesPackage

R Problem Overview


I'm developing my first R package (using R 2.13, Ubuntu 10.10). Let's call it foo and let's say that the code in the R/ directory begins with the line library(bar), where bar is an existing package, in CRAN, on which foo depends. My DESCRIPTION file contains the line:

Depends: bar

When package foo is ready for testing, I install it locally using:

R CMD INSTALL foo_1.0.tar.gz

However, if bar is not installed, I see:

ERROR: dependency ‘bar’ is not available for package ‘foo’

Obviously, if my foo were installed from CRAN using install.packages(), bar would be installed at the same time. So my question is: how can I ensure that CRAN package bar is installed, if required, when I install my package foo using R CMD INSTALL? Is this a job for a configuration script?

R Solutions


Solution 1 - R

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.

Solution 2 - R

Fortunately Devtools provides an easy solution: install_deps()

> install_deps(pkg = ".", dependencies = logical, threads = getOption("Ncpus",1))

> Arguments:
> pkg: package description, can be path or package name. See > ‘as.package’ for more information

> dependencies: ‘logical’ indicating to also install uninstalled packages > which this ‘pkg’ depends on/links to/suggests. See argument > ‘dependencies’ of ‘install.packages’.

> threads: number of concurrent threads to use for installing > dependencies. It defaults to the option ‘"Ncpus"’ or ‘1’ if > unset.

Examples:

install_deps(".")
install_deps("/path/to/package",dependencies="logical")

Solution 3 - R

I ended up just using a bash here-document and specifying the cloud mirror to find the dependencies:

sudo R --vanilla <<EOF
install.packages('forecast', repos='http://cran.us.r-project.org')
q()
EOF

the R package is "forecast", the cloud mirror I used was http://cran.us.r-project.org. If you want to use a different mirror, here they all are: https://cran.r-project.org/mirrors.html

The above worked for me in getting R packages into an AWS EMR bootstrap shell script.

Solution 4 - R

Similar to @Jonathan Le, but better for script usage :

sudo R --vanilla -e 'install.packages("forecast", repos="http://cran.us.r-project.org")'

Solution 5 - R

Update; as of Feb 2021, the remotes package does the trick and has a much smaller footprint than devtools:

R -e "install.packages('remotes')"
R -e "remotes::install_local('/path/to/mypackage.tar.gz', dependencies=T)"

Solution 6 - R

Following Romain Rossi's idea, here is a simple shell script which installs every argument you send it's way (assuming it's a package):

#!/bin/sh 
for f in $* 
    do 
    sudo R --vanilla -e "install.packages('"$f"', repos='http://cran.us.r-project.org')" 
done

Solution 7 - R

The mechanism to do this is to add an entry in the depends field in your DESCRIPTION file.

Depends: bar

This will load the bar library if already installed, otherwise will install it from CRAN.

This is described in section 1.1.1 of the Writing R extensions manual: http://cran.r-project.org/doc/manuals/R-exts.html#The-DESCRIPTION-file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneilfwsView Question on Stackoverflow
Solution 1 - RSpacedmanView Answer on Stackoverflow
Solution 2 - RigauravsehrawatView Answer on Stackoverflow
Solution 3 - RJonathan LeView Answer on Stackoverflow
Solution 4 - RRomain RossiView Answer on Stackoverflow
Solution 5 - RshapiromatronView Answer on Stackoverflow
Solution 6 - RBenjamin ChausseView Answer on Stackoverflow
Solution 7 - RAndrieView Answer on Stackoverflow