How to upload files to Dropbox and Google Docs from R

[This article was first published on Fellgernon Bit - rstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Have you ever wondered whether you can upload files from R to Dropbox and/or Google Docs? I recently asked myself this question while making my most recent Shiny app (more later).

The answer is yes, you can upload files from R to these cloud services!

Dropbox

As far as I know, the best R package for uploading files to Dropbox is rDrop (Ram & Temple Lang, 2012). The whole setup is very well explained in it’s GitHub repository (Karthik). Basically you have to:

  • Install the package and it’s dependencies (some are only on GitHub).
  • Create a Dropbox app in your account.
  • Get the credentials info.
  • Authorize your access to the Dropbox app from R.
  • Save that info for later use.

dropbox_save()

Then uploading any type of R object to Dropbox becomes as easy as using the dropbox_save() function.

For example, lets save a simple vector with random data.

suppressMessages(library("rDrop"))

## Define credentials or load them if you defined them already
## dropbox_credentials <- dropbox_auth('Your app key', 'Your app secret')
load("dropbox_credentials.Rdata")

## Lets create some random data
set.seed(20140205)
x <- rnorm(1000)

## Lets check the args of the uploading function
args(dropbox_save)

## function (cred, ..., list = character(), file = stop("'file' must be specified"), 
##     envir = parent.frame(), precheck = TRUE, verbose = FALSE, 
##     curl = getCurlHandle(), ext = ".rda", .opts = list()) 
## NULL

## Then lets upload it to dropbox on the public folder
done <- dropbox_save(dropbox_credentials, x, file = "public/x")

## The result has some information, like the path of where you upload the
## file
names(done)

##  [1] "revision"     "rev"          "thumb_exists" "bytes"       
##  [5] "modified"     "client_mtime" "path"         "is_dir"      
##  [9] "icon"         "root"         "mime_type"    "size"

done$path

## [1] "/public/x.rda"

You can now actually download the "x.rda" file from here. That's in case that you also wanted to share the file, and is obviously optional.

Note that you can get the link from withing R and don't even need to use the Dropbox site.

dropbox_share(dropbox_credentials, file = "public/x.rda")

## $url
## [1] "https://db.tt/xzf3huXf"
## 
## $expires
## [1] "Tue, 01 Jan 2030 00:00:00 +0000"

dropbox_put()

What if you want to upload an actual file and not only R objects? That's where dropbox_put() shines. Below is an example where we create an image, save it as a pdf file, and upload it to Dropbox.

## Lets create a sample file, in this case a pdf file
pdf("dropboxFig.pdf")
hist(x, freq = FALSE, col = "light blue")
tmp <- dev.off()

## Lets check the args for the uploading function
args(dropbox_put)

## function (cred, what, filename = what, curl = getCurlHandle(), 
##     ..., verbose = FALSE, contentType = "application/octet-stream") 
## NULL

## Now, lets upload the file
done <- dropbox_put(dropbox_credentials, what = "dropboxFig.pdf", filename = "public/dropboxFig.pdf")

## Again, the result contains some information about the file
names(done)

##  [1] "revision"     "rev"          "thumb_exists" "bytes"       
##  [5] "modified"     "client_mtime" "path"         "is_dir"      
##  [9] "icon"         "root"         "mime_type"    "size"

You can view the result here.

Google Docs

From what I found, it seems to me that RGoogleDocs (Temple Lang) is the package you'll want to use for interacting with Google Docs from R. The manual (Temple Lang) explains all what you pretty much need to know. You should know though that you can only upload certain types of files.

For example, you can upload a text file as shown below.

suppressMessages(library("RGoogleDocs"))

## Load password
load("gpasswd.Rdata")

## Something to write
text <- "Hello world!\n"

## Authentificate
auth <- getGoogleAuth("[email protected]", gpasswd)

## Connect to Google
con <- getGoogleDocsConnection(auth)

## Check the args for the uploading function
args(uploadDoc)

## function (content, con, name, type = as.character(findType(content)), 
##     binary = FALSE, asText = FALSE, folder = NULL, replace = TRUE, 
##     ...) 
## NULL

## Upload the file
done <- uploadDoc(content = text, con = con, name = "testFile.txt", type = "txt")

You can view the file here.

Conclusions

rDrop (Ram & Temple Lang, 2012) is very cool and easy to use. Compared to Google Docs, you have much greater flexibility on the type of files you can upload. I guess that will change in the future if there is a Google Drive from R package.

References

Citations made with knitcitations (Boettiger, 2014).

Reproducibility

sessionInfo()

## R version 3.0.2 (2013-09-25)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] RGoogleDocs_0.7-0   rDrop_0.3-0         stringr_0.6.2      
##  [4] plyr_1.8            ROAuth_0.92.0       digest_0.6.4       
##  [7] RCurl_1.95-4.1      bitops_1.0-6        RJSONIO_1.0-3      
## [10] knitcitations_0.5-0 bibtex_0.3-6        knitr_1.5          
## 
## loaded via a namespace (and not attached):
## [1] evaluate_0.5.1 formatR_0.10   httr_0.2       tools_3.0.2   
## [5] XML_3.95-0.2   xtable_1.7-1

Check other topics on #rstats.

To leave a comment for the author, please follow the link and comment on their blog: Fellgernon Bit - rstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)