Google Docs R

I know that you can publish spreadsheets in Google docs and then import them into R. However, let me say that I have a function or some kind of code that I want to read in R (as in the original function). How would you, for example, read all of this code stored in google docs?

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

I do not think it was published, but it is just for example.

I basically want:

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

  • Upload it to Google Docs and share it with someone with a link so that I don’t have to register through R or Google Docs, etc.

  • Read it with a command like source () wherever I want

I'm not interested in reading data, but in the reading function. This will greatly speed up the import of my own functions, if I'm not on the same computer / server.

Any ideas?

Many thanks

PS Example 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)
}
+5
source share
4 answers

This is easiest to do with Dropbox - see here http://thebiobucket.blogspot.co.at/2012/05/source-r-script-from-dropbox.html

So:

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

But you can either do it using Googledocs, as 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 = ""))
+8
source

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
  • Cooperation
  • R Github, R, devtools
  • URL, source()

, Github, :

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

, , R devtools.

, R- Github. , "github: gist". "gist" , source_gist() devtools :

source_gist("gist-id-number")

, ...


, - Google , , , , " " , , . , URL- , ....

, "txt" , , source() - R. - , "UTF-8 ( )". - , ?

+8

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

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

auth <- getGoogleAuth("dicko.ahmadou@gmail.com", "*********")

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

+4
source

There is an RGoogleDocs package that supports downloading and uploading files. After downloading the file, you can use sourceit to read it.

+1
source

All Articles