14 Nov 2011

How to Download and Run Google Docs Script in the R Console

...There is not much to it:
upload a txt file with your script, share it for anyone with the link, then simply run something like the below code. 

ps: When using the code for your own purpose mind to change "https" to "http" and to insert your individual document id.
pss: You could use download.file() in this way for downloading any file from Google Docs..


# Example 1:
setwd(tempdir())
download.file("http://docs.google.com/uc?export=download&id=0B2wAunwURQNsMDViYzllMTMtNjllZS00ZTc4LTgzMzEtNDFjMWQ3MTUzYTRk",
              destfile = "test_google_docs.txt", mode = "wb")
# the file contains: x <- sample(100); plot(x)
source(paste(tempdir(), "/test_google_docs.txt", sep = ""))
# remove files from tempdir:
unlink(dir())

# Example 2:
setwd(tempdir())
download.file("http://docs.google.com/uc?export=download&id=0B2wAunwURQNsY2MwMzNhNGMtYmU4Yi00N2FlLWEwYTctYWU3MDhjNTkzOTdi",
              destfile = "google_docs_script.txt", mode = "wb")
# the downloaded script is the GScholarScraper-Function,
# read it and run an example:
source(paste(tempdir(), "/google_docs_script.txt", sep = ""))
# remove files from tempdir:
unlink(dir())

EDIT, MARCH 2013:
Method is outdated, use Tony's from below!

8 comments :

  1. An alternative method if you are using https in this way (i.e. not giving any password type information):

    # RSTART
    library(RCurl)
    setwd(tempdir())
    destfile = "test_google_docs.txt"
    x = getBinaryURL("https://docs.google.com/uc?export=download&id=0B2wAunwURQNsMDViYzllMTMtNjllZS00ZTc4LTgzMzEtNDFjMWQ3MTUzYTRk", followlocation = TRUE, ssl.verifypeer = FALSE)
    writeBin(x, destfile, useBytes = TRUE)
    source(paste(tempdir(), "/test_google_docs.txt", sep = ""))
    ls()
    # remove files from tempdir:
    unlink(dir())
    # REND

    ReplyDelete
    Replies
    1. Tony,
      thanks for pointing out this approach!

      Delete
  2. This is a very interesting article, thank you for sharing this. Would you know if this would work with programs like Microsoft Sharepoint or dropbox as well?

    ReplyDelete
    Replies
    1. Sorry Tom,
      I don't know. But I guess a similar scheme may also apply for other file-sharing sites..
      You could try to find the URL for downloading the file and paste it to the appropriate place in the above code..

      Delete
  3. Tom, with dropbox you canmake it for sure: Just copy your R file to the 'Public' directory; with a right-click you can acquire its public url. Should work the very same way from that point on.

    ReplyDelete
    Replies
    1. see http://thebiobucket.blogspot.com/2012/05/source-r-script-from-dropbox.html !

      Delete
  4. Hey it's no working ,
    I'm getting downloaded file as 'complete html page' which in non executable in turn.

    ReplyDelete
    Replies
    1. Did you notice the edit in the OP? With Tony's method everything works perfectly well for me!

      Delete