是否有为给定 R 版本下载包和依赖项的新解决方案

Is there a new solution for downloading package and dependencies for a given R version

我看到了2018年here问的问题。我想知道今天是否有更好的答案。

我们的工作计算机被整个 IT 安全部门所困扰,这个部门的存在似乎是为了让它们变得毫无用处。我们被允许 运行 R 3.6.3(4.x 尚未获得批准)。我们无法从公司防火墙后面连接到 CRAN。在过去,这意味着我们将笔记本电脑带回家安装软件包。但现在我们有了一个下载监视器,即使我们使用自己的 wi-fi 也能阻止 CRAN 下载。

我试图通过在个人计算机上下载软件包 .zip 文件,通过 CD 传输它们,然后使用 repos=NULL 安装来解决这个问题。我使用了这个代码

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

获取我想要的包的依赖项并使用 download.packages() 下载它们。我试图安装 tidyquant 结果发现有 113 个依赖项。

当然,这会下载所有 113 个软件包的最新版本,其中有几个与 R 3.6.3 不兼容。

我上面提到的解决方案建议找到一个兼容的版本,然后使用

install_version("<package name>",
                version = "<version number>",
                repos = "http://cran.us.r-project.org")

但这将涉及手动搜索所有 113 个依赖项...另外,我正在寻找一个通用解决方案,我可以与我办公室中现在遇到此问题的其他 10 个人分享。

我希望自 2018 年以来可能会出现更好的解决方案?

更新: 根据以下答案,我尝试了

tmp <- tempfile()
dir.create(tmp)

checkpoint::checkpoint(snapshot_date = "2020-04-01", 
                       r_version = "3.6.3", 
                       checkpoint_location = tmp,
                       scan_now = FALSE)


packages <- getPackages(c("tidyquant"))

download.packages(packages, destdir="C:\Users\jerem\Downloads\tidyquant", type="win.binary")

我明白了

Running create_checkpoint in the home directory may result
in checkpointing very many packages. Continue? (Y/n) Y

Creating checkpoint directory C:/Users/jerem/AppData/Local/Temp/Rtmpa2YEjU/file1efc6daf58e3/.checkpoint/2020-04-01/lib/x86_64-w64-mingw32/3.6.3
Using checkpoint directory C:/Users/jerem/AppData/Local/Temp/Rtmpa2YEjU/file1efc6daf58e3/.checkpoint/2020-04-01/lib/x86_64-w64-mingw32/3.6.3
Warning messages:
1: In create_checkpoint(snapshot_date, r_version, checkpoint_location,  :
  Specified R version not the same as current R version
2: In use_checkpoint(snapshot_date, r_version, checkpoint_location,  :
  Specified R version not the same as current R version
> 
> packages <- getPackages(c("tidyquant"))
> 
> download.packages(packages, destdir="C:\Users\jerem\Downloads\tidyquant", type="win.binary")
Warning: unable to access index for repository https://mran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/4.1:
  cannot open URL 'https://mran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/4.1/PACKAGES'
Warning in download.packages(packages, destdir = "C:\Users\jerem\Downloads\tidyquant",  :
  no package ‘tidyquant’ at the repositories
Warning in download.packages(packages, destdir = "C:\Users\jerem\Downloads\tidyquant",  :
  no package ‘lubridate’ at the repositories
Warning in download.packages(packages, destdir = "C:\Users\jerem\Downloads\tidyquant",  :
  no package ‘PerformanceAnalytics’ at the repositories

所有 113 个依赖项都会出现“无包”警告。

我认为这里的关键是

Warning: unable to access index for repository https://mran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/4.1:

显然 2020 年 4 月不会有 4.1 存储库。我认为它正在尝试,因为机器是 运行ning 4.1。所以看起来我们非常接近....

如果有帮助

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    
system code page: 65001

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_4.1.2   tools_4.1.2      checkpoint_1.0.2

我不确定它是否完全满足您的需求,但 checkpoint 包在这里似乎很合适。它允许您从指定日期拍摄的 CRAN 快照下载源包,回到 2014-09-17。 R 4.0.0 是在 2020-04-24 左右发布的,因此来自 2020-04-01 的快照应该适合您的目的。

这是一个可重现的例子:

tmp1 <- tempfile()
dir.create(tmp1)
cwd <- setwd(tmp1)

getOption("repos")
##                           CRAN 
## "https://cloud.r-project.org/"

available.packages()[c("lattice", "Matrix", "nlme"), c("Version", "Repository")]
##         Version   Repository                               
## lattice "0.20-45" "https://cloud.r-project.org/src/contrib"
## Matrix  "1.4-0"   "https://cloud.r-project.org/src/contrib"
## nlme    "3.1-155" "https://cloud.r-project.org/src/contrib"

download.packages("Matrix", ".", type = "source")
## trying URL 'https://cloud.r-project.org/src/contrib/Matrix_1.4-0.tar.gz'
## Content type 'application/x-gzip' length 2849865 bytes (2.7 MB)
## ==================================================
## downloaded 2.7 MB
## 
##      [,1]     [,2]                   
## [1,] "Matrix" "./Matrix_1.4-0.tar.gz"

tmp2 <- tempfile()
dir.create(tmp2)
checkpoint::checkpoint(snapshot_date = "2020-04-01", 
                       r_version = "3.6.3", 
                       checkpoint_location = tmp2,
                       scan_now = FALSE)
## Creating checkpoint directory /var/folders/n7/v9s56rmd5hn17d3f1qj13l7m0000gn/T//RtmpbrT5Br/filee2045e35c290/.checkpoint/2020-04-01/lib/aarch64-apple-darwin20/3.6.3
## Using checkpoint directory /private/var/folders/n7/v9s56rmd5hn17d3f1qj13l7m0000gn/T/RtmpbrT5Br/filee2045e35c290/.checkpoint/2020-04-01/lib/aarch64-apple-darwin20/3.6.3
## Warning messages:
## 1: In create_checkpoint(snapshot_date, r_version, checkpoint_location,  :
##   Specified R version not the same as current R version
## 2: In use_checkpoint(snapshot_date, r_version, checkpoint_location,  :
##   Specified R version not the same as current R version

getOption("repos")
##                                             CRAN 
## "https://mran.microsoft.com/snapshot/2020-04-01"

available.packages()[c("lattice", "Matrix", "nlme"), c("Version", "Repository")]
##         Version   Repository                                                  
## lattice "0.20-40" "https://mran.microsoft.com/snapshot/2020-04-01/src/contrib"
## Matrix  "1.2-18"  "https://mran.microsoft.com/snapshot/2020-04-01/src/contrib"
## nlme    "3.1-145" "https://mran.microsoft.com/snapshot/2020-04-01/src/contrib"

download.packages("Matrix", ".", type = "source")
## trying URL 'https://mran.microsoft.com/snapshot/2020-04-01/src/contrib/Matrix_1.2-18.tar.gz'
## Content type 'application/octet-stream' length 1871705 bytes (1.8 MB)
## ==================================================
## downloaded 1.8 MB
## 
##      [,1]     [,2]                    
## [1,] "Matrix" "./Matrix_1.2-18.tar.gz"

setwd(cwd)
unlink(c(tmp1, tmp2), recursive = TRUE)

如果您实际上不是 运行 R 3.6.3,则会出现有关版本不匹配的警告。如果你只是下载源码包,可以忽略它们,实际上是为了在另一台机器上安装它们 运行 3.6.3.

您可以查看软件包 README?checkpoint 了解更多详情。

更新

如果您尝试下载 binary 包(.zip for Windows,.tgz for macOS)而不是 source 包 (.tar.gz),那么 checkpoint 会给你带来麻烦。默认情况下,download.packages 和朋友使用 contrib.url(repos, type) 构造一个 URL 来搜索软件包二进制文件。

contrib.url("https://mran.microsoft.com/snapshot/2020-04-01/src/contrib", "win.binary")
## [1] "https://mran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/4.1"

contrib.url("https://mran.microsoft.com/snapshot/2020-04-01/src/contrib", "mac.binary")
## [1] "https://mran.microsoft.com/snapshot/2020-04-01/bin/macosx/contrib/4.1"

但是 URL 两处都没有。那是(部分)因为 contrib.url 附加了您当前 运行 的 R 版本,这在您的快照日期可能不存在。因此:

download.packages("Matrix", ".", type = "win.binary")
## Warning: unable to access index for repository https://mran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/4.1:
##   cannot open URL 'https://mran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/4.1/PACKAGES'
## Warning in download.packages("Matrix", ".", type = "win.binary") :
##   no package 'Matrix' at the repositories
##      [,1] [,2]

download.packages("Matrix", ".", type = "mac.binary")
## Warning: unable to access index for repository https://mran.microsoft.com/snapshot/2020-04-01/bin/macosx/contrib/4.1:
##   cannot open URL 'https://mran.microsoft.com/snapshot/2020-04-01/bin/macosx/contrib/4.1/PACKAGES'
## Warning in download.packages("Matrix", ".", type = "mac.binary") :
##   no package 'Matrix' at the repositories
##      [,1] [,2]

您实际需要的URL是:

## Windows
"https://cran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/3.6"

## macOS, OS X, whatever
"https://cran.microsoft.com/snapshot/2020-04-01/bin/macosx/el-capitan/contrib/3.6"

在这种情况下,最好的方法可能是完全避免 checkpoint,直接将有效的 URL 传递给 available.packagesdownload.packages:

pkg <- "tidyquant"
contriburl <- "https://cran.microsoft.com/snapshot/2020-04-01/bin/windows/contrib/3.6"

db <- available.packages(contriburl)
deps <- tools::package_dependencies(pkg, db, recursive = TRUE)[[pkg]]
download.packages(c(pkg, deps), ".", contriburl = contriburl, type = "win.binary")