This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
README.Rmd
225 lines (164 loc) · 8.46 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
---
output:
# html_document
md_document:
variant: markdown_github
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
[![Build Status](https://travis-ci.org/jennybc/googlesheets.svg?branch=master)](https://travis-ci.org/jennybc/googlesheets) [![Coverage Status](https://coveralls.io/repos/jennybc/googlesheets/badge.svg)](https://coveralls.io/r/jennybc/googlesheets)
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
```{r make-clean, echo = FALSE, include = FALSE}
## 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:
- Access a spreadsheet by its title, key or URL.
- Extract data or edit data.
- Create | delete | rename | copy | upload | download spreadsheets and worksheets.
`googlesheets` is inspired by [gspread](https://github.com/burnash/gspread), a Google Spreadsheets Python API
The exuberant prose in this README is inspired by [Tabletop.js](https://github.com/jsoma/tabletop): 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!
#### 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](http://www.google.com/forms/about/) 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](https://itunes.apple.com/us/app/google-sheets/id842849113?mt=8) or [an Android device](https://play.google.com/store/apps/details?id=com.google.android.apps.docs.editors.sheets&hl=en). Take advantage of [data validation](https://support.google.com/docs/answer/139705?hl=en) to limit the crazy on the way in.
There are various ways to harvest web data directly into a Google Sheet. For example:
* [This blog post](http://blog.aylien.com/post/114757623598/sentiment-analysis-of-restaurant-reviews) from Aylien.com has a simple example that uses the `=IMPORTXML()` formula to populate a Google Sheet with restaurant reviews and ratings from TripAdvisor.
* Martin Hawksey offers [TAGS](https://tags.hawksey.info), a free Google Sheet template to setup and run automated collection of search results from Twitter.
* Martin Hawksey also has a great blog post, [Feeding Google Spreadsheets](https://mashe.hawksey.info/2012/10/feeding-google-spreadsheets-exercises-in-import/), that demonstrates how functions like `importHTML`, `importFeed`, and `importXML` help you get data from the web into a Google Sheet with no programming.
* Martin Hawksey has another blog post about [feeding a Google Sheet from IFTTT](https://mashe.hawksey.info/2012/09/ifttt-if-i-do-that-on-insert-social-networkrss-feedother-then-add-row-to-google-spreadsheet/). [IFTTT](https://ifttt.com) stands for "if this, then that" and it's "a web-based service that allows users to create chains of simple conditional statements, called 'recipes', which are triggered based on changes to other web services such as Gmail, Facebook, Instagram, and Craigslist" (from [Wikipedia](http://en.wikipedia.org/wiki/IFTTT)).
Use `googlesheets` to get all that data into R.
Use it in a Shiny app! *[Several example apps](inst/shiny-examples) come with the package.*
What other ideas do you have?
### Install googlesheets
The released version is available on CRAN
```{r eval = FALSE}
install.packages("googlesheets")
```
Or you can get the development version from GitHub:
```{r eval = FALSE}
devtools::install_github("jennybc/googlesheets")
```
### Take a look at the vignette
Read [the vignette](http://htmlpreview.github.io/?https://raw.githubusercontent.com/jennybc/googlesheets/master/vignettes/basic-usage.html) on GitHub.
### Slides from UseR2015
[Slides](https://speakerdeck.com/jennybc/googlesheets-talk-at-user2015) for a talk in July 2015 at the [UseR2015 conference](http://user2015.math.aau.dk)
### Load googlesheets
`googlesheets` is designed for use with the `%>%` pipe operator and, to a lesser extent, the data-wrangling mentality of [`dplyr`](http://cran.r-project.org/web/packages/dplyr/index.html). 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`](http://cran.r-project.org/web/packages/dplyr/index.html) or [`magrittr`](http://cran.r-project.org/web/packages/magrittr/index.html).
```{r load-package}
library("googlesheets")
suppressPackageStartupMessages(library("dplyr"))
```
### Function naming convention
All functions start with `gs_`, which plays nicely with tab completion. If the function has something to do with worksheets or tabs within a spreadsheet, then it will start with `gs_ws_`.
### Quick demo
First, 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.
```{r copy-gapminder, eval = FALSE}
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):
```{r}
gap <- gs_title("Gapminder")
```
Here's a registered `googlesheet` object:
```{r}
gap
```
Read all the data in a worksheet:
```{r}
africa <- gs_read(gap)
str(africa)
head(africa)
```
Some of the many ways to target specific cells:
```{r eval = FALSE}
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))
```
Create a new Sheet:
```{r}
iris_ss <- gs_new("iris", input = head(iris, 3), trim = TRUE)
```
Edit some arbitrary cells and append a row:
```{r}
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:
```{r}
iris_ss %>%
gs_read()
```
Download this precious thing (other formats are possible):
```{r}
iris_ss %>%
gs_download(to = "iris-ish-stuff.csv", overwrite = TRUE)
```
Clean up our mess:
```{r eval = FALSE}
gs_vecdel("iris", "Gapminder")
file.remove("iris-ish-stuff.csv")
```
```{r include = FALSE}
gs_vecdel("iris")
file.remove("iris-ish-stuff.csv")
```
Remember, [the vignette](http://htmlpreview.github.io/?https://raw.githubusercontent.com/jennybc/googlesheets/master/vignettes/basic-usage.html) shows a lot more usage.
### Overview of functions
```{r include = FALSE}
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_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_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_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
"
```
```{r as.is = TRUE, echo = FALSE}
knitr::kable(read.csv(text = fxn_table))
```