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

Viewed 44107

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?

7 Answers

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

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

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)"

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
Related