Site icon R-bloggers

R-Function to Read Data from Google Docs Spreadsheets

[This article was first published on theBioBucket*, 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.
I used this idea posted on Stack Overflow to plug together a function for reading data from Google Docs spreadsheets into R.










google_ss <- function(gid = NA, key = NA)
{
if (is.na(gid)) {stop(“\nWorksheetnumber (gid) is missing\n”)}
if (is.na(key)) {stop(“\nDocumentkey (key) is missing\n”)}
require(RCurl)
url <- getURL(paste(“https://docs.google.com/spreadsheet/pub?key=”, key,
“&single=true&gid=”, gid, “&output=csv”, sep = “”),
cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
read.csv(textConnection(url), header = T, sep = “,”)
}

## Example:
## Mind that the worksheets are numbered consecutively from 0 to n,
## irrespective of the actual worksheet-name.
## The key should be put in apostrophes.
## And, the URL works only for published spreadsheets!

(data <- google_ss(gid = 0,
key = “0AmwAunwURQNsdDNpZzJqTU90cmpTU0sza2xLTW9fenc”))

To leave a comment for the author, please follow the link and comment on their blog: theBioBucket*.

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.