Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R code on Google docs

I'm aware that you can publish spreadsheets on Google docs and then import them in R. However, let's say I have a function or some code I want to read in R (like in the source function). How would you for example read in all this code stored on google docs?

https://docs.google.com/document/edit?id=1f11rcVs9AtVcgOyiP0lV04yq9hshVDIPx93lA0ItFvQ

I don't think this has been published but it's just to give an example.

Basically I want to:

  1. Create a function in R (for comparability see example below)

  2. Upload it on Google Docs and share it with anyone with the link so that I don't have to log in through R or Google Docs etc...

  3. Read it in through a command like source() wherever I want

My interest is not in reading data but in reading functions. It would make importing my own functions much quicker when I'm not on the same computer/server.

Any ideas?

Many thanks

P.S. Example of a function from statsmethods.net

mysummary <- function(x,npar=TRUE,print=TRUE) {
  if (!npar) {
    center <- mean(x); spread <- sd(x) 
  } else {
    center <- median(x); spread <- mad(x) 
  }
  if (print & !npar) {
    cat("Mean=", center, "\n", "SD=", spread, "\n")
  } else if (print & npar) {
    cat("Median=", center, "\n", "MAD=", spread, "\n")
  }
  result <- list(center=center,spread=spread)
  return(result)
}
like image 242
Marco M Avatar asked Aug 11 '12 08:08

Marco M


3 Answers

It is most easily done with Dropbox - see here http://thebiobucket.blogspot.co.at/2012/05/source-r-script-from-dropbox.html

like so:

source("http://dl.dropbox.com/s/c18lcwnnrodsevt/test_dropbox_source.R")

But you can either do it with Googledocs, like here http://thebiobucket.blogspot.co.at/2011/11/how-to-run-r-scripts-directly-out-of.html#more

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 = ""))
like image 161
Kay Avatar answered Oct 11 '22 16:10

Kay


I would suggest Github or something similar for this type of situation.

Just a few of the reasons for recommending Github:

  • Version control
  • Code highlighting
  • Collaboration
  • Ability to develop and host an R package on Github that others can install in R using the devtools package
  • Simple URL scheme that makes it easy to use source() to load functions

Once you have posted your function to Github, you can easily load it as follows:

require(RCurl)
baseURL = c("https://raw.github.com/--username--/--repo-name--/master/")
source(textConnection(getURL(paste0(baseURL, "path-to-scripts/script-name.R"))))

Of course, you can easily wrap that into a function instead of having to type it out in each R session, or try some of the options in the devtools package.

If you do this a lot, you might want to learn to make an R package and host it on Github. If you only need to do this once in a while, you might consider using "github:gist" instead. Each "gist" is assigned a numeric ID that can then easily be sourced using source_gist() from the devtools package as follows:

source_gist("gist-id-number")

Pretty hard to beat that, but you do have to make note of the ID numbers for your functions, or put all of your functions in one file and memorize one ID number...


The reason I wouldn't recommend something like Google Docs to write your code is that it is generally a terrible idea to use "word processor" type software for such purposes as they might introduce hidden characters that would make your file difficult to source. Furthermore, the URL to the plain text version is a very obscure one that you won't be able to remember....

Incidentally, it is possible to get the "txt" version of the file you've linked to in your post, but I'm not able to source() it--I have to open it in a text editor and copy and paste it into R. This is because of the file encoding which appears to be "UTF-8 (with BOM)". Anyone out there have tips on how to deal with that?

like image 35
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 11 '22 16:10

A5C1D2H2I1M1N2O1R2T1


I also think that the best way is to use github or dropbox. But using the RGoogleDocs and XML packages we can parse the code (I'm not very experienced) with XML parsing may be someone will have better code.

### require(devtools);dev_mode(TRUE, .libPaths()[1]);install_github("RGoogleDocs", "duncantl")
require(RGoogleDocs) 
require(XML)

auth <- getGoogleAuth("[email protected]", "*********")

con <- getGoogleDocsConnection(auth)

mydoc <- getDocs(con)

## I put star for confidentiality
## Your doc is in 10th position
names(mydoc)

##  [1] "*********"                                 
##  [2] "*********"                             
##  [3] "panel_tp_transferts"                                      
##  [4] "txint"                                                    
##  [5] "avortementsuivisen"                                       
##  [6] "Untitled Document"                                        
##  [7] "copie de villages_emprise10km"
##  [8] "AéroportBlaiseDiagne_AFDB.pdf"                            
##  [9] "strassen_eng.pdf"                                         
## [10] "R_script_CO2_emissions_airborne"  


rcode <- mydoc[[10]]
rcode <- getDocContent(rcode, con)
## remove Non break space in the document (there are plenty of them...)
rcode <- gsub("&nbsp;", " ", rcode)
rcode <- htmlParse(rcode, asText = TRUE)
rcodecontent <- xpathApply(rcode, "/html//body//p//span")
rcodecontent <- sapply(rcodecontent, function(x) unname(xmlSApply(x, xmlValue))

Now we can save the code in a file

### save the script in my dropbox folder (dropbox is very easy to use...)
cat(sapply(rcodecontent, function(x) paste(x, "\n")), 
       file = "/home/ahmadou/Dropbox/Public/code.R")

### retrieve the public link
oldwd <- getwd()
setwd("/home/ahmadou/Dropbox/Public")
system('dropbox puburl code.R', intern = TRUE)
[1] "https://dl.dropbox.com/u/8750577/code.R"

setwd(oldw)

Here is the link for the code

like image 39
dickoa Avatar answered Oct 11 '22 17:10

dickoa