Only download sources of a package and all dependencies

I am wondering if there's a way to use install.packages() or other related functions to do the following: only download the sources (i.e. tar.gz files) of the specified packages and all their dependencies into a specified folder (on Windows).

One reason to do this is: say I have a Linux account that is not enabled for internet access. In order to install the packages on the Linux machine, I would first download all the needed sources on my Windows machine, then ftp them over to the Linux machine, and install them on the Linux machine using

  install.packages('/home/me/R/Packages/blah.tar.gz', repos = NULL)

Solution 1:

I recently had a problem where I wanted to download all dependencies and I've solved it thus:

Say I want all the dependencies and imports of ggplot2 and MASS:

getPackages <- function(packs){
  packages <- unlist(
    tools::package_dependencies(packs, available.packages(),
                         which=c("Depends", "Imports"), recursive=TRUE)
  )
  packages <- union(packs, packages)
  packages
}

packages <- getPackages(c("ggplot2", "MASS"))

I can now download the packages to another directory.

download.packages(packages, destdir="whereyouactuallywantthefiles", 
                  type="source")

From there if you want to make a local repo on your Linux PC, follow the instructions here.

Solution 2:

Try download.packages(c("xts", "rms"), "c:/TEMP", .....) instead of install.packages(); you can directly give it a target directory in the 2nd argument.

Edit several years later: As stated above on other answers and comments, by now several helper functions have been added to R's tools and utils packages. R 3.4.0 will have tools::CRAN_package_db() to download the top-level PACKAGES.rds file (and of course you could just combine download.file() and readRDS() for that too).