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)
npm install (in a package directory, no arguments): Install the dependencies to the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
The pip download command can be used to download packages and their dependencies to the current directory (by default), or else to a specified location without installing them.
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.
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).
There are now better options for this in the tools package that comes with base R: package_dependencies()
. See for example, the Answer from @sebastian-c and this recent Q&A for a related use-case.
There is an unexported getDependencies()
function in the utils package. I haven't studied how it works, but combining that with @Dirk's Answer should get you most of the way there.
Basically though, it appears you use it like:
utils:::getDependencies(pkgs, dependencies, available, lib)
where pkgs
is the character vector of packages to install, dependencies
is a character vector of types of dependencies (Depends, Enhances etc) that you want, available
is the output from available.packages()
and lib
is the library location for the packages within which dependencies are evaluated.
If you debug install.packages()
it is basically doing the getDependencies()
step then @Dirk's download.packages()
step before it actually starts installing anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With