我看到很多用RCurl下载二进制文件的例子都是这样的:
library("RCurl")
curl = getCurlHandle()
bfile=getBinaryURL (
"http://www.example.com/bfile.zip",
curl= curl,
progressfunction = function(down, up) {print(down)}, noprogress = FALSE
)
writeBin(bfile, "bfile.zip")
rm(curl, bfile)
Run Code Online (Sandbox Code Playgroud)
如果下载非常大,我想最好将它同时写入存储介质,而不是在内存中获取所有内容.
在RCurl文档中,有一些示例可以通过块获取文件并在下载时对其进行操作,但它们似乎都是指文本块.
你能给出一个有效的例子吗?
用户建议使用R native download file和mode = 'wb'二进制文件选项.
在许多情况下,本机函数是一个可行的替代方案,但是有许多用例不适合这种本机函数(https,cookie,表单等),这就是RCurl存在的原因.
ant*_*nio 18
这是工作示例:
library(RCurl)
#
f = CFILE("bfile.zip", mode="wb")
curlPerform(url = "http://www.example.com/bfile.zip", writedata = f@ref)
close(f)
Run Code Online (Sandbox Code Playgroud)
它将直接下载到文件.返回的值将是(而不是下载的数据)请求的状态(如果没有错误,则为0).
提到CFILERCurl手册有点简洁.希望将来它将包含更多细节/示例.
为方便起见,相同的代码被打包为一个函数(并带有一个进度条):
bdown=function(url, file){
library('RCurl')
f = CFILE(file, mode="wb")
a = curlPerform(url = url, writedata = f@ref, noprogress=FALSE)
close(f)
return(a)
}
## ...and now just give remote and local paths
ret = bdown("http://www.example.com/bfile.zip", "path/to/bfile.zip")
Run Code Online (Sandbox Code Playgroud)