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.


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

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.


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