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<span class="p">(</span>library<span class="p">(</span><span class="s">"rDrop"</span><span class="p">))</span>

<span class="c1">## Define credentials or load them if you defined them already</span>
<span class="c1">## dropbox_credentials <- dropbox_auth('Your app key', 'Your app secret')</span>
load<span class="p">(</span><span class="s">"dropbox_credentials.Rdata"</span><span class="p">)</span>

<span class="c1">## Lets create some random data</span>
set.seed<span class="p">(</span><span class="m">20140205</span><span class="p">)</span>
x <span class="o"><-</span> rnorm<span class="p">(</span><span class="m">1000</span><span class="p">)</span>

<span class="c1">## Lets check the args of the uploading function</span>
args<span class="p">(</span>dropbox_save<span class="p">)</span>
## function (cred, ..., list = character(), file = stop("'file' must be specified"), 
##     envir = parent.frame(), precheck = TRUE, verbose = FALSE, 
##     curl = getCurlHandle(), ext = ".rda", .opts = list()) 
## NULL
<span class="c1">## Then lets upload it to dropbox on the public folder</span>
done <span class="o"><-</span> dropbox_save<span class="p">(</span>dropbox_credentials<span class="p">,</span> x<span class="p">,</span> file <span class="o">=</span> <span class="s">"public/x"</span><span class="p">)</span>

<span class="c1">## The result has some information, like the path of where you upload the</span>
<span class="c1">## file</span>
names<span class="p">(</span>done<span class="p">)</span>
##  [1] "revision"     "rev"          "thumb_exists" "bytes"       
##  [5] "modified"     "client_mtime" "path"         "is_dir"      
##  [9] "icon"         "root"         "mime_type"    "size"
done<span class="o">$</span>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<span class="p">(</span>dropbox_credentials<span class="p">,</span> file <span class="o">=</span> <span class="s">"public/x.rda"</span><span class="p">)</span>
## $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.

<span class="c1">## Lets create a sample file, in this case a pdf file</span>
pdf<span class="p">(</span><span class="s">"dropboxFig.pdf"</span><span class="p">)</span>
hist<span class="p">(</span>x<span class="p">,</span> freq <span class="o">=</span> <span class="kc">FALSE</span><span class="p">,</span> col <span class="o">=</span> <span class="s">"light blue"</span><span class="p">)</span>
tmp <span class="o"><-</span> dev.off<span class="p">()</span>

<span class="c1">## Lets check the args for the uploading function</span>
args<span class="p">(</span>dropbox_put<span class="p">)</span>
## function (cred, what, filename = what, curl = getCurlHandle(), 
##     ..., verbose = FALSE, contentType = "application/octet-stream") 
## NULL
<span class="c1">## Now, lets upload the file</span>
done <span class="o"><-</span> dropbox_put<span class="p">(</span>dropbox_credentials<span class="p">,</span> what <span class="o">=</span> <span class="s">"dropboxFig.pdf"</span><span class="p">,</span> filename <span class="o">=</span> <span class="s">"public/dropboxFig.pdf"</span><span class="p">)</span>

<span class="c1">## Again, the result contains some information about the file</span>
names<span class="p">(</span>done<span class="p">)</span>
##  [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<span class="p">(</span>library<span class="p">(</span><span class="s">"RGoogleDocs"</span><span class="p">))</span>

<span class="c1">## Load password</span>
load<span class="p">(</span><span class="s">"gpasswd.Rdata"</span><span class="p">)</span>

<span class="c1">## Something to write</span>
text <span class="o"><-</span> <span class="s">"Hello world!\n"</span>

<span class="c1">## Authentificate</span>
auth <span class="o"><-</span> getGoogleAuth<span class="p">(</span><span class="s">"[email protected]"</span><span class="p">,</span> gpasswd<span class="p">)</span>

<span class="c1">## Connect to Google</span>
con <span class="o"><-</span> getGoogleDocsConnection<span class="p">(</span>auth<span class="p">)</span>

<span class="c1">## Check the args for the uploading function</span>
args<span class="p">(</span>uploadDoc<span class="p">)</span>
## function (content, con, name, type = as.character(findType(content)), 
##     binary = FALSE, asText = FALSE, folder = NULL, replace = TRUE, 
##     ...) 
## NULL
<span class="c1">## Upload the file</span>
done <span class="o"><-</span> uploadDoc<span class="p">(</span>content <span class="o">=</span> text<span class="p">,</span> con <span class="o">=</span> con<span class="p">,</span> name <span class="o">=</span> <span class="s">"testFile.txt"</span><span class="p">,</span> type <span class="o">=</span> <span class="s">"txt"</span><span class="p">)</span>

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<span class="p">()</span>
## 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)