使用R从ftp-server下载SAS文件 [英] Using R to download SAS file from ftp-server
问题描述
我试图从ftp-server上将一些文件下载到本地。我已经成功地使用以下方法从服务器移动.txt和.csv文件,但不需要.sas7bdat文件,我需要。
协议< - sftp
服务器< - ServerName
userpwd< - User:Pass
tsfrFilename< - /filepath/file.sas7bdat
ouptFilename< - out.sas7bdat
#运行#
##下载数据
url< - paste0(protocol,://,服务器,tsfrFilename)
数据< - getURL(url = url,userpwd = userpwd)
##创建文件
fconn< - 文件(ouptFilename)
writeLines(data,fconn)
close(fconn)
当我运行getURL命令时,但是,遇到以下错误:
curlPerform中的错误(curl = curl,.opts = opts,.encoding = .encoding):
字符串中嵌入的nul:
有谁知道任何其他方式我可以从ftp服务器下载sas7bdat文件到我的本地,或者如果有方法t o修改我的代码以成功下载文件。感谢!
正如@MrFlick所示,我使用
getBinaryURL
而不是 getURL()
。此外,我不得不使用函数 write()
而不是 writeLines()
。结果如下: 协议< - sftp
服务器< - 服务器名称
userpwd< - User:Pass
tsfrFilename< - /filepath/file.sas7bdat
ouptFilename< - out.sas7bdat
#Run #
##下载数据
url< - paste0(protocol,://,server,tsfrFilename)
data< - getBinaryURL(url = url,userpwd = userpwd)
##创建文件
fconn< - 文件(ouptFilename)
写入(数据,fconn)
关闭(fconn)
$ c $另外,为了将读取的数据转换成R数据帧,可以使用库 haven
$ b> >,如下
library(haven)
df_data = read_sas(data)
I am attempting to download some files onto my local from an ftp-server. I have had success using the following method to move .txt and .csv files from the server but not the .sas7bdat files that I need.
protocol <- "sftp"
server <- "ServerName"
userpwd <- "User:Pass"
tsfrFilename <- "/filepath/file.sas7bdat"
ouptFilename <- "out.sas7bdat"
# Run #
## Download Data
url <- paste0(protocol, "://", server, tsfrFilename)
data <- getURL(url = url, userpwd=userpwd)
## Create File
fconn <- file(ouptFilename)
writeLines(data, fconn)
close(fconn)
When I run the getURL command, however, I am met with the following error:
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
embedded nul in string:
Does anyone know of any alternative way which I can download a sas7bdat file from an ftp-server to my local, or if there is a way to alter my code below to successfully download the file. Thanks!
解决方案 As @MrFlick suggested, I solved this problem using getBinaryURL
instead of getURL()
. Also, I had to use the function write()
instead of writeLines()
. The result is as follows:
protocol <- "sftp"
server <- "ServerName"
userpwd <- "User:Pass"
tsfrFilename <- "/filepath/file.sas7bdat"
ouptFilename <- "out.sas7bdat"
# Run #
## Download Data
url <- paste0(protocol, "://", server, tsfrFilename)
data <- getBinaryURL(url = url, userpwd=userpwd)
## Create File
fconn <- file(ouptFilename)
write(data, fconn)
close(fconn)
Alternatively, to transform the read data into R data frame, one can use the library haven
, as follows
library(haven)
df_data= read_sas(data)
这篇关于使用R从ftp-server下载SAS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!