使用 RCurl(或任何其他方法)从 FTP 下载 .RData 和 .csv 文件 [英] Download .RData and .csv files from FTP using RCurl (or any other method)
问题描述
我已将 .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
这些数据上传使用:
uploadURL <-"ftp://name:password@host/folder/results.RData" #name the url
ftpUpload(RDataFilePath, to=uploadURL, connecttimeout=120) #upload
这是我尝试使用 getURL
下载 results.RData 的方式:
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
字符串粘贴到浏览器中时,.RData 文件会立即下载,所以我知道没有打错字.该错误消息表明该 url 无法读取压缩格式的 b/c;但是,当我使用没有压缩的 save()
时,我收到了类似的错误消息.
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
然后是一个警告,说明 此外:警告消息:在 file(file, "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. 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")
这篇关于使用 RCurl(或任何其他方法)从 FTP 下载 .RData 和 .csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!