使用RCurl(或任何其他方法)从FTP下载.RData和.csv文件

问题描述:

我已将.RData文件(使用 save()创建)添加到ftp服务器,而我正在尝试使用getURL()下载相同的文件。对于我读过的所有示例和帖子,我似乎无法让它工作。

I've uploaded a .RData file (created using save()) to an ftp server, and I'm trying to use getURL() to download that same file. For all the examples and posts I've read, I can't seem to get this to work.

.RData文件使用以下方式保存:

The .RData file was saved using:

save(results, file=RDataFilePath, compress="xz") #save object "results" w/ compression
#RDataFilePath is the location of the results.RData file on my harddrive

这些数据是使用以下方式上传的:

These data were uploaded using:

uploadURL <-"ftp://name:password@host/folder/results.RData" #name the url
ftpUpload(RDataFilePath, to=uploadURL, connecttimeout=120) #upload

这是我如何尝试下载结果。 RData使用 getURL

This is how I try to download results.RData using getURL:

downloadURL <- "host/folder/results.RData"
load(getURL(downloadURL, userpwd="name:password", connecttimeout=120))

其中出现以下错误:

Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) : 
  embedded nul in string: 'ý7zXZ'

当我粘贴 downloadURL string到我的浏览器中,.rdata文件立即下载,所以我知道没有打字错误。该错误消息表明url无法读取压缩格式的b / c;但是,当我使用 save() w / o压缩时,我收到类似的错误消息。

When I paste the downloadURL string into my browser, the .RData file downloads immediately, so I know there isn't a typo. The error message suggests that the url can't get read b/c of the compression formatting; however, I get a similar error message when I use save() w/o compression.

我也得到尝试从FTP下载.csv时出现错误消息:

I also get an error message when trying to download a .csv from the FTP:

read.csv(getURL(downloadURL1)) #downloadURL1 is similar to downloadURL, but points to the .csv file
Error in file(file, "rt") : cannot open the connection 

然后发出一条警告,说明另外:警告消息:
在文件(文件rt)中:无法打开文件
然后开始列出.csv的内容。

and then a warning which states In addition: Warning message: In file(file, "rt") : cannot open file and then starts listing the contents of the .csv.

我一直在试图想出这个更好的部分,我觉得我必须缺少一些真正基本的东西。我猜想我需要改变一些curl选项,以便它知道它将要读取什么类型的文件。我的语法可能有点不对,我没有正确使用getURL,但我不知道我应该做什么。

I've been trying to figure this out for the better part of the morning, and I feel like I must be missing something really basic. I'm guessing that I need to change some curl option so that it knows what type of file it is going to read. My syntax is probably a bit off, and I'm not using getURL correctly, but I'm not sure what I should be doing.

非常感谢任何提示。

p.s。我目前的方法是基于这个发布

p.s. My current approach is based on this Post

您可以尝试将其分为两个步骤:首先下载文件,然后加载。

You can try breaking it into two steps: first download the file, then load it.

download.file(downloadURL, "temp.rData")
load("temp.rData")

或使用rCurl,您可以尝试:

or sticking with rCurl you can try:

bin = getBinaryURL(downloadURL, ...yourOtherParams...) 
writeBin(bin, "temp.rData")  
load("temp.rData")