####################### ## RGoogleDocs Usage ## ####################### ## Author: Thomas Girke ## Last update: Feb 3, 2012 ## Utility: Short overview of RGoogleDocs package from Duncan Temple Lang ## Package install ## Download latest RGoogleDocs version: ## Linux ## install.packages("RGoogleDocs", repos = "http://www.omegahat.org/R") ## OS X ## download package source from here: http://www.omegahat.org/R/src/contrib/ ## install.packages("RGoogleDocs_0.6-0.tar.gz", repos=NULL, type="source") ########################################### ## (1) Upload spreadsheet to Google Docs ## ########################################### library(RGoogleDocs) ## (1.1) Authenticate for write access auth <- getGoogleAuth("googledocs_email", "googledocs_password", "writely") # Replace "googledocs_email" and "googledocs_password" with your Google Docs account name and password, respectively. con <- getGoogleDocsConnection(auth) ## (1.2) Upload a data frame export <- iris[1:4,] export <- paste(paste(colnames(export), collapse=", "), "\n", paste(apply(t(export), 2, paste, collapse=", "), collapse="\n"), "\n", sep="", collapse="") # Converts to csv vector. Alternatively, one can write to local csv file and then point uploadDoc() function to this file. tmp <- uploadDoc(export, con, name = "iris", type = "csv") # Check in your Google Docs account if you see a new spreadsheet with name "iris". ## Open question: I am not sure if there are any options to update exisiting documents or to write to specific sheets? ## (2.3) Delete document in Google Docs deleteDoc("iris", con) #################################### ## (2) Downloads from Google Docs ## #################################### ## (2.1) Authenticate for read access auth <- getGoogleAuth("googledocs_email", "googledocs_password", "wise") # Replace "googledocs_email" and "googledocs_password" with your Google Docs account name and password, respectively. con <- getGoogleDocsConnection(auth) ## (2.2) List available docs docs <- getDocs(con) names(docs) as(docs, "data.frame") ## (2.3) Worksheet information of Google Spreadsheets sheets <- getWorksheets(docs[["RGoogleDocsTest"]], con) names(sheets) ## (2.4) Import specific worksheet into data frame dtf <- getWorksheets("RGoogleDocsTest", con) sheetAsMatrix(dtf[["Sheet1"]], header=TRUE) sheetAsMatrix(dtf[["Sheet2"]], header=TRUE)