Using R to download SAS file from ftp-server

别说谁变了你拦得住时间么 提交于 2019-12-18 07:05:38

问题


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!


回答1:


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)


来源:https://stackoverflow.com/questions/43856937/using-r-to-download-sas-file-from-ftp-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!