Only download sources of a package and all dependencies

别来无恙 提交于 2019-12-17 08:21:32

问题


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)

回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/6281322/only-download-sources-of-a-package-and-all-dependencies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!