Build Status Coverage Status DOI CRAN version

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)
## if previous compilation errored out, intended clean up may be incomplete
suppressWarnings(
  file.remove(c("~/tmp/gapminder-africa.csv", "~/tmp/gapminder.xlsx")))
googlesheets::gs_vecdel(c("foo", "mini-gap", "iris"), verbose = FALSE)

Google Sheets R API

Access and manage Google spreadsheets from R with googlesheets.

Features:

googlesheets is inspired by gspread, a Google Spreadsheets Python API

The exuberant prose in this README is inspired by Tabletop.js: If you've ever wanted to get data in or out of a Google Spreadsheet from R without jumping through a thousand hoops, welcome home!

Install googlesheets

The released version is available on CRAN

install.packages("googlesheets")

Or you can get the development version from GitHub:

devtools::install_github("jennybc/googlesheets")

Vignettes

GitHub versions:

Talks

Load googlesheets

googlesheets is designed for use with the %>% pipe operator and, to a lesser extent, the data-wrangling mentality of dplyr. This README uses both, but the examples in the help files emphasize usage with plain vanilla R, if that's how you roll. googlesheets uses dplyr internally but does not require the user to do so. You can make the %>% pipe operator available in your own work by loading dplyr or magrittr.

library("googlesheets")
suppressPackageStartupMessages(library("dplyr"))

Function naming convention

To play nicely with tab completion, we use consistent prefixes:

Quick demo

Here's how to get a copy of a Gapminder-based Sheet we publish for practicing and follow along. You'll be sent to the browser to authenticate yourself with Google at this point.

gs_gap() %>% 
  gs_copy(to = "Gapminder")
## or, if you don't use pipes
gs_copy(gs_gap(), to = "Gapminder")

Register a Sheet (in this case, by title):

gap <- gs_title("Gapminder")

Here's a registered googlesheet object:

gap

Visit a registered googlesheet in the browser:

gap %>% gs_browse()
gap %>% gs_browse(ws = "Europe")

Read all the data in a worksheet:

africa <- gs_read(gap)
glimpse(africa)
africa

Some of the many ways to target specific cells:

gap %>% gs_read(ws = 2, range = "A1:D8")
gap %>% gs_read(ws = "Europe", range = cell_rows(1:4))
gap %>% gs_read(ws = "Africa", range = cell_cols(1:4))

Full readr-style control of data ingest -- highly artificial example!

gap %>%
  gs_read(ws = "Oceania", col_names = paste0("Z", 1:6),
          na = c("1962", "1977"), col_types = "cccccc", skip = 1, n_max = 7)

Create a new Sheet from an R object:

iris_ss <- gs_new("iris", input = head(iris, 3), trim = TRUE)

Edit some arbitrary cells and append a row:

iris_ss <- iris_ss %>% 
  gs_edit_cells(input = c("what", "is", "a", "sepal", "anyway?"),
                anchor = "A2", byrow = TRUE)
iris_ss <- iris_ss %>% 
  gs_add_row(input = c("sepals", "support", "the", "petals", "!!"))

Look at what we have wrought:

iris_ss %>% 
  gs_read()

Download this precious thing as csv (other formats are possible):

iris_ss %>% 
  gs_download(to = "iris-ish-stuff.csv", overwrite = TRUE)

Download this precious thing as an Excel workbook (other formats are possible):

iris_ss %>% 
  gs_download(to = "iris-ish-stuff.xlsx", overwrite = TRUE)

Upload a Excel workbook into a new Sheet:

gap_xlsx <- gs_upload(system.file("mini-gap", "mini-gap.xlsx",
                                  package = "googlesheets"))

Clean up our mess locally and on Google Drive:

gs_vecdel(c("iris", "Gapminder"))
file.remove(c("iris-ish-stuff.csv", "iris-ish-stuff.xlsx"))
gs_delete(iris_ss)
file.remove(c("iris-ish-stuff.csv", "iris-ish-stuff.xlsx"))

Remember, the vignette shows a lot more usage.

Overview of functions

fxn_table <-
"fxn,description
gs_ls(), List Sheets
gs_title(), Register a Sheet by title
gs_key(), Register a Sheet by key
gs_url(), Register a Sheet by URL
gs_gs(), Re-register a `googlesheet`
gs_browse(), Visit a registered `googlesheet` in the browser
gs_read(), Read data and let `googlesheets` figure out how
gs_read_csv(), Read explicitly via the fast exportcsv link
gs_read_listfeed(), Read explicitly via the list feed
gs_read_cellfeed(), Read explicitly via the cell feed
gs_reshape_cellfeed(), Reshape cell feed data into a 2D thing
gs_simplify_cellfeed(), Simplify cell feed data into a 1D thing
gs_edit_cells(), Edit specific cells
gs_add_row(), Append a row to pre-existing data table
gs_new(), Create a new Sheet and optionally populate
gs_copy(), Copy a Sheet into a new Sheet
gs_rename(), Rename an existing Sheet
gs_ws_ls(), List the worksheets in a Sheet
gs_ws_new(), Create a new worksheet and optionally populate
gs_ws_rename(), Rename a worksheet
gs_ws_delete(), Delete a worksheet
gs_delete(), Delete a Sheet
gs_grepdel(), Delete Sheets with matching titles
gs_vecdel(), Delete the named Sheets 
gs_upload(), Upload local file into a new Sheet
gs_download(), Download a Sheet into a local file
gs_auth(), Authorize the package
gs_deauth(), De-authorize the package
gs_user(), Get info about current user and auth status
gs_webapp_auth_url(), Facilitates auth by user of a Shiny app
gs_webapp_get_token(), Facilitates auth by user of a Shiny app
gs_gap(), Registers a public Gapminder-based Sheet (for practicing)
gs_gap_key(), Key of the Gapminder practice Sheet
gs_gap_url(), Browser URL for the Gapminder practice Sheet
"
knitr::kable(read.csv(text = fxn_table))

What the hell do I do with this?

Think of googlesheets as a read/write CMS that you (or your less R-obsessed friends) can edit through Google Docs, as well via R. It's like Christmas up in here.

Use a Google Form to conduct a survey, which populates a Google Sheet.

Gather data while you're in the field in a Google Sheet, maybe with an iPhone or an Android device. Take advantage of data validation to limit the crazy on the way in. You do not have to be online to edit a Google Sheet! Work offline via the Chrome browser, the Sheets app for Android, or the Sheets app for iOS.

There are various ways to harvest web data directly into a Google Sheet. For example:

Use googlesheets to get all that data into R.

Use it in a Shiny app! Several example apps come with the package.

What other ideas do you have?



dakota1063/dddd documentation built on May 25, 2019, 4:21 p.m.