是否有一个强大且与操作系统无关的功能来从 CRAN 下载 tar.gz 档案?

问题描述 投票:0回答:1

我已经做了一些搜索,但我找不到任何可以“开箱即用”完成工作的东西。大多数线程都围绕安装,但我对下载感兴趣。我已经编写了自己的小实用程序(见下文),但我对与操作系统无关的东西感兴趣,这可能也更强大并且不需要个人维护。

我是否忽略了某些事情,或者这根本不存在?

功能:

get_pack_tar_gz <- function(package, version, destination = ".") {
  package_tar_gz_string <- paste0(package, "_", version, ".tar.gz")
  latest_version_url <- paste0("https://cran.r-project.org/src/contrib/",
                               package_tar_gz_string)
  archive_version_url <- paste0("https://cran.r-project.org/src/contrib/Archive/",
                                package,
                                "/",
                                package_tar_gz_string)
  
  latest_return <- suppressWarnings(
    system(paste0("wget --spider ", latest_version_url, " 2> /dev/null"), intern = TRUE)
  )
  latest_return_status <- attributes(latest_return)$status
  
  if(length(latest_return_status) == 0) {
    system(paste0("wget -q https://cran.r-project.org/src/contrib/",
                  package_tar_gz_string,
                  " -P ",
                  destination))
  } else if(latest_return_status == 8) {
    system(paste0("wget -q https://cran.r-project.org/src/contrib/Archive/",
                  package,
                  "/",
                  package_tar_gz_string, 
                  " -P ",
                  destination))
  } else {
    stop("Unexpected!")
  }
  if(! file.exists(paste0(destination, "/", package_tar_gz_string))) {
    stop("No file retrieved!")
  }
}

调用示例:

get_pack_tar_gz(package = "tidyr", version = "1.3.1")
get_pack_tar_gz(package = "tidyr", version = "0.1")
r cran
1个回答
0
投票

根据MrFlick的评论,我们可以更简洁地重写多个包的下载调用,如下所示:

# example data
packs_to_dl <- structure(list(name = c("fastmap", "r2r", "urca"),
                              version = c("1.1.0", "0.1.1", "1.3-3"),
                              out_file = c("fastmap_1.1.0.tar.gz",
                                           "r2r_0.1.1.tar.gz",
                                           "urca_1.3-3.tar.gz")),
                         row.names = c(1L, 2L, 3L),
                         class = "data.frame")

# download
download.file(unlist(purrr::pmap(list(packs_to_dl$name,
                                      packs_to_dl$version,
                                      "https://cran.r-project.org",
                                      "source"),
                                 remotes:::download_version_url)),
              destfile = packs_to_dl$out_file)
© www.soinside.com 2019 - 2024. All rights reserved.