[英]Offline installation of a list of packages: getting dependencies in order
我有一堆軟件包及其依賴項的源文件,我想安裝在沒有Internet訪問權限的計算機上。 我想使用USB記憶棒在其他計算機上安裝所有這些,但是某些軟件包的安裝失敗,因為在軟件包之前沒有安裝依賴項。 如何在需要它們的軟件包之前按順序安裝依賴項?
這是我當前獲取包及其依賴項的方法,並以正確的順序獲取它們:
# find the dependencies for the packages I want
# from http://stackoverflow.com/a/15650828/1036500
getPackages <- function(packs){
packages <- unlist(
tools::package_dependencies(packs, available.packages(),
which=c("Depends", "Imports"), recursive=TRUE)
)
packages <- union(packages, packs)
packages
}
# packages I want
my_packages <- c('stringr', 'devtools', 'ggplot2', 'dplyr', 'tidyr', 'rmarkdown', 'knitr', 'reshape2', 'gdata')
# get names of dependencies and try to get them in the right order, this seems ridiculous...
my_packages_and_dependencies <- getPackages(my_packages)
dependencies_only <- setdiff(my_packages_and_dependencies, my_packages)
deps_of_deps <- getPackages(dependencies_only)
deps_of_deps_of_deps <- getPackages(deps_of_deps)
my_packages_and_dependencies <- unique(c(deps_of_deps_of_deps, deps_of_deps, dependencies_only, my_packages))
# where to keep the source?
local_CRAN <- paste0(getwd(), "/local_CRAN")
# get them from CRAN, source files
download.packages(pkgs = my_packages_and_dependencies, destdir = local_CRAN, type = "source")
# note that 'tools', 'methods', 'utils, 'stats', etc. art not on CRAN, but are part of base
# from http://stackoverflow.com/a/10841614/1036500
library(tools)
write_PACKAGES(local_CRAN)
現在假設我在另一台安裝了R和RStudio(以及Rtools或Xcode)並且沒有互聯網連接的計算機上,我插入USB記憶棒,打開RProj文件來設置工作目錄,然后運行這個腳本:
#############################################################
## Install from source (Windows/OSX/Linux)
# What do I want to install?
my_packages_and_dependencies <- c("methods", "tools", "bitops", "stats", "colorspace", "graphics",
"tcltk", "Rcpp", "digest", "jsonlite", "mime", "RCurl", "R6",
"stringr", "brew", "grid", "RColorBrewer", "dichromat", "munsell",
"plyr", "labeling", "grDevices", "utils", "httr", "memoise",
"whisker", "evaluate", "rstudioapi", "roxygen2", "gtable", "scales",
"proto", "MASS", "assertthat", "magrittr", "lazyeval", "DBI",
"stringi", "yaml", "htmltools", "caTools", "formatR", "highr",
"markdown", "gtools", "devtools", "ggplot2", "dplyr", "tidyr",
"rmarkdown", "knitr", "reshape2", "gdata")
# where are the source files?
local_CRAN <- paste0(getwd(), "/local_CRAN")
# scan all packages and get files names of wanted source pckgs
# I've got other things in this dir also
wanted_package_source_filenames <- list.files(local_CRAN, pattern = "tar.gz", full.names = TRUE)
# put them in order to make sure deps go first, room for improvement here...
trims <- c(local_CRAN, "/", "tar.gz")
x1 <- gsub(paste(trims, collapse = "|"), "", wanted_package_source_filenames)
x2 <- sapply( strsplit(x1, "_"), "[[", 1)
idx <- match(my_packages_and_dependencies, x2)
wanted_package_source_filenames <- na.omit(wanted_package_source_filenames[idx])
install.packages(wanted_package_source_filenames,
repos = NULL,
dependencies = TRUE,
contrib.url = local_CRAN, # I thought this would take care of getting dependencies automatically...
type = "source" )
這工作得相當好,但仍然有些軟件包無法安裝:
sapply(my_packages_and_dependencies, require, character.only = TRUE)
methods tools bitops stats
TRUE TRUE TRUE TRUE
colorspace graphics tcltk Rcpp
TRUE TRUE TRUE TRUE
digest jsonlite mime RCurl
TRUE TRUE TRUE FALSE
R6 stringr brew grid
TRUE TRUE TRUE TRUE
RColorBrewer dichromat munsell plyr
TRUE TRUE TRUE TRUE
labeling grDevices utils httr
TRUE TRUE TRUE FALSE
memoise whisker evaluate rstudioapi
TRUE TRUE TRUE TRUE
roxygen2 gtable scales proto
TRUE TRUE TRUE TRUE
MASS assertthat magrittr lazyeval
TRUE TRUE TRUE TRUE
DBI stringi yaml htmltools
TRUE TRUE TRUE TRUE
caTools formatR highr markdown
TRUE TRUE TRUE TRUE
gtools devtools ggplot2 dplyr
TRUE FALSE FALSE TRUE
tidyr rmarkdown knitr reshape2
FALSE FALSE TRUE TRUE
gdata
TRUE
Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘RCurl’
2: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘httr’
3: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘devtools’
4: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘ggplot2’
5: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘tidyr’
6: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘rmarkdown’
似乎knitr必須在rmarkdown之前,在tidyr和ggplot2等之前重塑2等。
對於以非常特定的順序獲取源文件列表的問題,必須有一個更簡單,更完整的解決方案,需要以正確的順序放置所有依賴項。 最簡單的方法是什么(不使用任何貢獻包)?
這是我目前正在處理的系統,我正在使用軟件包的源版本,試圖為離線計算機(OSX / Linux / Windows)做任何准備:
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] tcltk grid tools stats graphics
[6] grDevices utils datasets methods base
other attached packages:
[1] gdata_2.13.3 reshape2_1.4.1
[3] knitr_1.9 dplyr_0.4.1
[5] gtools_3.4.1 markdown_0.7.4
[7] highr_0.4 formatR_1.0
[9] caTools_1.17.1 htmltools_0.2.6
[11] yaml_2.1.13 stringi_0.4-1
[13] DBI_0.3.1 lazyeval_0.1.10
[15] magrittr_1.5 assertthat_0.1
[17] proto_0.3-10 scales_0.2.4
[19] gtable_0.1.2 roxygen2_4.1.0
[21] rstudioapi_0.2 evaluate_0.5.5
[23] whisker_0.3-2 memoise_0.2.1
[25] labeling_0.3 plyr_1.8.1
[27] munsell_0.4.2 dichromat_2.0-0
[29] RColorBrewer_1.1-2 brew_1.0-6
[31] stringr_0.6.2 R6_2.0.1
[33] mime_0.2 jsonlite_0.9.14
[35] digest_0.6.8 Rcpp_0.11.4
[37] colorspace_1.2-5 bitops_1.0-6
[39] MASS_7.3-35
loaded via a namespace (and not attached):
[1] parallel_3.1.2
編輯跟隨Andrie的有用評論,我已經開始使用miniCRAN,小插圖中缺少的是如何從本地倉庫實際安裝軟件包。 這就是我嘗試過的:
library("miniCRAN")
# Specify list of packages to download
pkgs <- c('stringr', 'devtools', 'ggplot2', 'dplyr', 'tidyr', 'rmarkdown', 'knitr', 'reshape2', 'gdata')
# Make list of package URLs
revolution <- c(CRAN="http://cran.revolutionanalytics.com")
pkgList <- pkgDep(pkgs, repos=revolution, type="source" )
pkgList
# Set location to store source files
local_CRAN <- paste0(getwd(), "/local_CRAN")
# Make repo for source
makeRepo(pkgList, path = local_CRAN, repos = revolution, type = "source")
# install...
install.packages(pkgs,
repos = local_CRAN, # do I really need "file:///"?
dependencies = TRUE,
contrib.url = local_CRAN,
type = "source" )
結果是:
Installing packages into ‘C:/emacs/R/win-library/3.1’
(as ‘lib’ is unspecified)
Warning in install.packages :
unable to access index for repository C:/Users/.../local_CRAN/src/contrib
Warning in install.packages :
packages ‘stringr’, ‘devtools’, ‘ggplot2’, ‘dplyr’, ‘tidyr’, ‘rmarkdown’, ‘knitr’, ‘reshape2’, ‘gdata’ are not available (for R version 3.1.2)
我在這里錯過了什么?
編輯是的,我錯過了正確使用file:///
,它應該是這樣的:
install.packages(pkgs,
repos = paste0("file:///", local_CRAN),
type = "source")
這讓我感覺很快,現在基本上都按預期工作了。 非常感謝。 現在我只是要查看: fatal error: curl/curl.h: No such file or directory
,它阻止了RCurl和httr的安裝。
miniCRAN
包可以幫助解決這個問題。 你告訴miniCRAN
你想要安裝的軟件包列表,然后計算出依賴關系,下載這些軟件包並在你的本地機器上創建一個類似於CRAN的存儲庫,即它尊重install.packages()
等。
更多信息:
閱讀小插圖
我們正在積極開發miniCRAN
。 跟蹤進度並在github miniCRAN存儲庫中找到最新的開發版本
有關演示文稿,博客文章等的鏈接,請參閱項目Wiki
要從本地miniCRAN
存儲庫安裝,您有兩個選擇。
首先,您可以使用URI約定file:///
。 例如
install.packages("ggplot2", repos="file:///path/to/file/")
或者,您可以將目標配置為HTTP服務器,並通過URL使您的存儲庫可用。 在這種情況下,您的本地存儲庫外觀和感覺就像一個CRAN鏡像,除了它只包含您想要的包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.