我正在尝试从ftp服务器下载一些文件到我的本地。我已经成功地使用以下方法从服务器上移动了.txt和.csv文件,但没有移动我需要的.sas7bdat文件。
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命令时,遇到以下错误:
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
embedded nul in string:有没有人知道任何替代方法,我可以下载一个sas7bdat文件从ftp服务器到我的本地,或者有没有办法改变我的代码下面成功下载文件。谢谢!
发布于 2017-07-11 02:23:46
正如@MrFlick建议的那样,我使用getBinaryURL而不是getURL()解决了这个问题。此外,我必须使用函数write()而不是writeLines()。结果如下:
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,如下所示
library(haven)
df_data= read_sas(data)https://stackoverflow.com/questions/43856937
复制相似问题