使用 R 从 ftp-server 下载 SAS 文件 [英] Using R to download SAS file from ftp-server
问题描述
我正在尝试从 ftp 服务器下载一些文件到我的本地.我已经成功使用以下方法从服务器移动 .txt 和 .csv 文件,但没有移动我需要的 .sas7bdat 文件.
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)
但是,当我运行 getURL 命令时,我遇到了以下错误:
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:
有谁知道我可以将 sas7bdat 文件从 ftp 服务器下载到我的本地的任何替代方法,或者是否有一种方法可以更改下面的代码以成功下载文件.谢谢!
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!
推荐答案
正如@MrFlick 建议的那样,我使用 getBinaryURL
而不是 getURL()
解决了这个问题.另外,我必须使用函数 write()
而不是 writeLines()
.结果如下:
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)
或者,要将读取的数据转换为R数据帧,可以使用库haven
,如下
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屋!