r download.file函数的“改进”版本:您只需要指定要下载的资源的URL。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r download.file函数的“改进”版本:您只需要指定要下载的资源的URL。相关的知识,希望对你有一定的参考价值。

download.file2 <- function (url, destfile = NULL, method, quiet = FALSE, mode = "w", cacheOK = TRUE, extra = getOption("download.file.extra")) 
{
  if (is.null(destfile)) destfile <- basename(url)
  else destfile
  method <- if (missing(method)) 
    getOption("download.file.method", default = "auto")
  else match.arg(method, c("auto", "internal", "libcurl", "wget", 
                           "curl", "lynx"))
  if (method == "auto") {
    if (length(url) != 1L || typeof(url) != "character") 
      stop("'url' must be a length-one character vector")
    method <- if (grepl("^file:", url)) 
      "internal"
    else "libcurl"
  }
  switch(method, internal = {
    status <- .External(C_download, url, destfile, quiet, 
                        mode, cacheOK)
    if (!quiet) flush.console()
  }, libcurl = {
    status <- .Internal(curlDownload(url, destfile, quiet, 
                                     mode, cacheOK))
    if (!quiet) flush.console()
  }, wget = {
    if (length(url) != 1L || typeof(url) != "character") stop("'url' must be a length-one character vector")
    if (length(destfile) != 1L || typeof(destfile) != "character") stop("'destfile' must be a length-one character vector")
    if (quiet) extra <- c(extra, "--quiet")
    if (!cacheOK) extra <- c(extra, "--cache=off")
    status <- system(paste("wget", paste(extra, collapse = " "), 
                           shQuote(url), "-O", shQuote(path.expand(destfile))))
    if (status) stop("'wget' call had nonzero exit status")
  }, curl = {
    if (length(url) != 1L || typeof(url) != "character") stop("'url' must be a length-one character vector")
    if (length(destfile) != 1L || typeof(url) != "character") stop("'destfile' must be a length-one character vector")
    if (quiet) extra <- c(extra, "-s -S")
    if (!cacheOK) extra <- c(extra, "-H 'Pragma: no-cache'")
    status <- system(paste("curl", paste(extra, collapse = " "), 
                           shQuote(url), " -o", shQuote(path.expand(destfile))))
    if (status) stop("'curl' call had nonzero exit status")
  }, lynx = stop("method 'lynx' is defunct", domain = NA))
  if (status) 
    warning("download had nonzero exit status")
  invisible(status)
}

以上是关于r download.file函数的“改进”版本:您只需要指定要下载的资源的URL。的主要内容,如果未能解决你的问题,请参考以下文章

R语言download.file函数从互联网下载文件实战

使用 R 下载 NetCDF 文件:手动工作,download.file 产生错误

在R中下载csv文件

如何使用 R 从 xml 页面中提取信息

[C++11 类的改进] --- 继承控制:=default和=delete

R语言e1071包中的支持向量机:仿真数据(螺旋线性不可分数据集)简单线性核的支持向量机SVM(模型在测试集上的表现可视化模型预测的结果添加超平面区域与原始数据标签进行对比分析)如何改进核函数