13

How to upload a file to a server via FTP using R?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Tal Galili
  • 24,605
  • 44
  • 129
  • 187

4 Answers4

24

This should work:

library(RCurl)
ftpUpload("Localfile.html", "ftp://User:Password@FTPServer/Destination.html")

Where Localfile.html is the file to be uploaded, User indicates the user name and Password the password to log into the server while FTPServer is a placeholder for the server name and possible path to use while last but not least Destination.html is an example of the name the to be uploaded file gets on the server.

petermeissner
  • 12,234
  • 5
  • 63
  • 63
  • Is there any way to upload directly from r global environment without saving it ( for example - tableData <- data.frame(a = c(1, 2, 3, 4), b = c(1, 4, 4, 8) ) ftpUpload(tableData, "ftp://ftp.blahblah/tableData.csv", userpwd = "user:pass" ) – Agnieszka May 22 '19 at 12:09
  • This might work: `ftp_upload <- function(df, dest, user, pass, server, protocol = "ftp"){ fname <- tempfile() write.csv(df) RCurl::ftpUpload( what = fname, glue::glue("{protocol}://{user}:{pass}@{server}/{dest}") ) }` ... but I do not have a way to test that anymore – petermeissner May 22 '19 at 20:00
9

Your best bet may be the RCurl package. From the DESCRIPTION:

[...] Additionally, the underlying implementation is robust and extensive, supporting FTP/FTPS/TFTP (uploads and downloads),

Otherwise, rethink your problem. Maybe HTTP POST will do as well. It's not 1986 anymore so you're not required to use ftp.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
3

If you can access it from the command line, then you can do:

system("ftp ...") # where ... is the argument list

You could easily wrap this in an R function if you plan on doing it often.

Shane
  • 98,550
  • 35
  • 224
  • 217
3

This probably isn't the answer you're looking for, but I solve my sharing problems by moving the file to my Public dropbox folder and link to that in my R code.

My two pennies.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197