我可以获取 install.packages 将使用的内容的 URL 吗?
Can I get the URL of what will be used by install.packages?
当 运行 install.packages("any_package")
在 windows 时,我收到消息:
trying URL
'somepath.zip'
我想不下载就得到这个路径,可以吗?
换句话说,我想将 CRAN link 转换为最新版本的 windows 二进制文件(实际上最好能够使用相同的方式调用新函数参数作为 install.packages
并获得正确的 url(s) 作为输出)。
我需要一种在 R 控制台上工作的方法(无需手动检查 CRAN 页面等)。
我不确定这是否是您要找的。这从存储库信息构建 URL 并构建可用包列表的文件名。
#get repository name
repos<- getOption("repos")
#Get url for the binary package
#contrib.url(repos, "both")
contriburl<-contrib.url(repos, "binary")
#"https://mirrors.nics.utk.edu/cran/bin/windows/contrib/3.5"
#make data.frame of avaialbe packages
df<-as.data.frame(available.packages())
#find package of interest
pkg <- "tidyr" #example
#ofinterest<-grep(pkg, df$Package)
ofinterest<-match(pkg, df$Package) #returns a single value
#assemble name, assumes it is always a zip file
name<-paste0(df[ofinterest,]$Package, "_", df[ofinterest,]$Version, ".zip")
#make final URL
finalurl<-paste0(contriburl, "/", name)
这里有几个函数,分别是:
- 从 RStudio 的网站获取最新的 R 版本
- 获取最后发布的 windows 二进制文件的 url
第一个是我在 installr
包中找到的代码变体。似乎没有获得最新版本的干净方法,所以我们必须抓取一个网页。
第二个实际上只是@Dave2e 的代码优化并重构为一个函数(修复了过时的 R 版本),所以请直接对他的回答投赞成票。
get_package_url <- function(pkg){
version <- try(
available.packages()[pkg,"Version"],
silent = TRUE)
if(inherits(version,"try-error"))
stop("Package '",pkg,"' is not available")
contriburl <- contrib.url(getOption("repos"), "binary")
url <- file.path(
dirname(contriburl),
get_last_R_version(2),
paste0(pkg,"_",version,".zip"))
url
}
get_last_R_version <- function(n=3){
page <- readLines(
"https://cran.rstudio.com/bin/windows/base/",
warn = FALSE)
line <- grep("R-[0-9.]+.+-win\.exe", page,value=TRUE)
long <- gsub("^.*?R-([0-9.]+.+)-win\.exe.*$","\1",line)
paste(strsplit(long,"\.")[[1]][1:n], collapse=".")
}
get_package_url("data.table")
# on my system with R 3.3.1
# [1] "https://lib.ugent.be/CRAN/bin/windows/contrib/3.5/data.table_1.11.4.zip"
当 运行 install.packages("any_package")
在 windows 时,我收到消息:
trying URL
'somepath.zip'
我想不下载就得到这个路径,可以吗?
换句话说,我想将 CRAN link 转换为最新版本的 windows 二进制文件(实际上最好能够使用相同的方式调用新函数参数作为 install.packages
并获得正确的 url(s) 作为输出)。
我需要一种在 R 控制台上工作的方法(无需手动检查 CRAN 页面等)。
我不确定这是否是您要找的。这从存储库信息构建 URL 并构建可用包列表的文件名。
#get repository name
repos<- getOption("repos")
#Get url for the binary package
#contrib.url(repos, "both")
contriburl<-contrib.url(repos, "binary")
#"https://mirrors.nics.utk.edu/cran/bin/windows/contrib/3.5"
#make data.frame of avaialbe packages
df<-as.data.frame(available.packages())
#find package of interest
pkg <- "tidyr" #example
#ofinterest<-grep(pkg, df$Package)
ofinterest<-match(pkg, df$Package) #returns a single value
#assemble name, assumes it is always a zip file
name<-paste0(df[ofinterest,]$Package, "_", df[ofinterest,]$Version, ".zip")
#make final URL
finalurl<-paste0(contriburl, "/", name)
这里有几个函数,分别是:
- 从 RStudio 的网站获取最新的 R 版本
- 获取最后发布的 windows 二进制文件的 url
第一个是我在 installr
包中找到的代码变体。似乎没有获得最新版本的干净方法,所以我们必须抓取一个网页。
第二个实际上只是@Dave2e 的代码优化并重构为一个函数(修复了过时的 R 版本),所以请直接对他的回答投赞成票。
get_package_url <- function(pkg){
version <- try(
available.packages()[pkg,"Version"],
silent = TRUE)
if(inherits(version,"try-error"))
stop("Package '",pkg,"' is not available")
contriburl <- contrib.url(getOption("repos"), "binary")
url <- file.path(
dirname(contriburl),
get_last_R_version(2),
paste0(pkg,"_",version,".zip"))
url
}
get_last_R_version <- function(n=3){
page <- readLines(
"https://cran.rstudio.com/bin/windows/base/",
warn = FALSE)
line <- grep("R-[0-9.]+.+-win\.exe", page,value=TRUE)
long <- gsub("^.*?R-([0-9.]+.+)-win\.exe.*$","\1",line)
paste(strsplit(long,"\.")[[1]][1:n], collapse=".")
}
get_package_url("data.table")
# on my system with R 3.3.1
# [1] "https://lib.ugent.be/CRAN/bin/windows/contrib/3.5/data.table_1.11.4.zip"