在R脚本中下载文件的便携方法?

时间:2015-03-13 19:06:53

标签: r

我目前使用download.file函数下载文件,我在Linux中运行脚本,因此我使用此命令:

download.file(url="https://example.com/zipfile.zip", destfile= "zipfile.zip", method="wget")

此命令在Windows中不起作用,因为默认情况下wget不存在。如果我使用method=auto,我会收到此错误,这意味着download.file不支持https协议。

Error in download.file(url="https://example.com/zipfile.zip", destfile= "zipfile.zip", method="auto") : unsupported URL scheme

如何使此文件下载部分适用于所有主要操作系统:Windows,Linux,Mac?

从接受的答案中,我查看了RCurl的文档并按照此示例进行了操作:

  u = "http://www.omegahat.org/RCurl/data.gz"

if(url.exists(u)) {

  content = getBinaryURL(u)

  if(require(Rcompression)) {
    x = gunzip(content)
    read.csv(textConnection(x))
  } else {
     tmp = tempfile()
     write(content, file = tmp)
     read.csv(gzfile(tmp))
  }

1 个答案:

答案 0 :(得分:1)

1方法curl或libcurl:

# try capabilities see if it is supported on your build.
capabilities("libcurl")
download.file(url="https://example.com/zipfile.zip", destfile= "zipfile.zip", method="libcurl")

2也许试试RCurl

library(RCurl)