R getURL() – Quick SFTP File Transfer

This code will help quickly transfer data from a remote server, instead of using SFTP/FTP client, like WinSCP.


# 1. Load "RCurl" package 
library ("RCurl")

# Input #
protocol <- "sftp"
server <- "12.345.6.789"
userpwd <- "user:pwd"
tsfrFilename <- "/test/example.csv" #you can transfer other kinds, like text, as well.
ouptFilename <- "~/output/example.csv"

# Run #
## Download Data
url <- paste0(protocol, "://", server, tsfrFilename)
data <- getURL(url = url, userpwd=userpwd)

## Create File
fconn <- file(ouptFilename)
writeLines(data, fconn)
close(fconn)
Advertisements

One thought on “R getURL() – Quick SFTP File Transfer

  1. Hi thanks your code works fine with .txt file. But while connecting to linux server and retrieving SAS dataset. I am hitting with below error.

    Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
    embedded nul in string: ‘Âê`³2421Ͻ’\b\tÇ1Œ303720213″3301021010301303720213″330102101301#3( 0301SAS FILEPATCH_WORKLOAD DATA AÕ¥%ÖüÙAAÕ¥%ÖüÙA ¬@ ¬@ I9.0301M1Linux2.6.18-348.el5x86_64AÕ¥%\r›Ê@\r›Ê@\r›Ê@R/|àAÕ¥%ÖüÙA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s