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?
I ended up just using a bash here-document and specifying the cloud mirror to find the dependencies:
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.
The mechanism to do this is to add an entry in the
depends
field in yourDESCRIPTION
file.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-fileActually, 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:
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.
Fortunately Devtools provides an easy solution:
install_deps()
Examples:
Similar to @Jonathan Le, but better for script usage :