Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
update README; recompile vignette [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Jul 7, 2015
1 parent 2ba1144 commit e1f6c23
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 279 deletions.
8 changes: 5 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ What other ideas do you have?

### Install googlesheets

The released version is available on CRAN

```{r eval = FALSE}
devtools::install_github("jennybc/googlesheets")
install.packages("googlesheets")
```

*It's not on CRAN yet but that is hopefully imminent. So then this will work.*
Or you can get the development version from GitHub:

```{r eval = FALSE}
install.packages("googlesheets")
devtools::install_github("jennybc/googlesheets")
```

### Take a look at the vignette
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ What other ideas do you have?

### Install googlesheets

The released version is available on CRAN

``` r
devtools::install_github("jennybc/googlesheets")
install.packages("googlesheets")
```

*It's not on CRAN yet but that is hopefully imminent. So then this will work.*
Or you can get the development version from GitHub:

``` r
install.packages("googlesheets")
devtools::install_github("jennybc/googlesheets")
```

### Take a look at the vignette
Expand Down Expand Up @@ -96,7 +98,7 @@ Here's a registered `googlesheet` object:
gap
#> Spreadsheet title: Gapminder
#> Spreadsheet author: gspreadr
#> Date of googlesheets registration: 2015-07-02 10:46:41 GMT
#> Date of googlesheets registration: 2015-07-07 08:33:29 GMT
#> Date of last spreadsheet update: 2015-03-23 20:34:08 GMT
#> visibility: private
#> permissions: rw
Expand Down
202 changes: 6 additions & 196 deletions vignettes/basic-usage.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,202 +2,12 @@
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
fig.path = "README-",
purl = FALSE
)

## ----auth, include = FALSE-----------------------------------------------
## I grab the token from the testing directory because that's where it is to be
## found on Travis
token_path <- file.path("..", "tests", "testthat", "googlesheets_token.rds")
suppressMessages(googlesheets::gs_auth(token = token_path, verbose = FALSE))

## ----pre-clean, include = FALSE------------------------------------------
## if a previous compilation of this document leaves anything behind, i.e. if it
## aborts, clean up Google Drive first
googlesheets::gs_vecdel(c("foo", "iris"), verbose = FALSE)
file.remove(c("gapminder.xlsx", "gapminder-africa.csv", "iris"))

## ----load package--------------------------------------------------------
library(googlesheets)
suppressMessages(library(dplyr))

## ----list-sheets---------------------------------------------------------
(my_sheets <- gs_ls())
# (expect a prompt to authenticate with Google interactively HERE)
my_sheets %>% glimpse()

## ----copy-gapminder, eval = FALSE----------------------------------------
# gs_gap() %>%
# gs_copy(to = "Gapminder")

## ----register-sheet------------------------------------------------------
gap <- gs_title("Gapminder")
gap

# Need to access a sheet you do not own?
# Access it by key if you know it!
(GAP_KEY <- gs_gap_key())
third_party_gap <- GAP_KEY %>%
gs_key()

# Need to access a sheet you do not own but you have a sharing link?
# Access it by URL!
(GAP_URL <- gs_gap_url())
third_party_gap <- GAP_URL %>%
gs_url()
# note: registration via URL may not work for "old" sheets

# Worried that a spreadsheet's registration is out-of-date?
# Re-register it!
gap <- gap %>% gs_gs()

## ------------------------------------------------------------------------
oceania <- gap %>% gs_read(ws = "Oceania")
oceania
str(oceania)
glimpse(oceania)

## ------------------------------------------------------------------------
gap %>% gs_read(ws = 2, range = "A1:D8")
gap %>% gs_read(ws = "Europe", range = cell_rows(1:4))
gap %>% gs_read(ws = "Europe", range = cell_rows(100:103), col_names = FALSE)
gap %>% gs_read(ws = "Africa", range = cell_cols(1:4))
gap %>% gs_read(ws = "Asia", range = cell_limits(c(1, 4), c(5, NA)))

## ----csv-list-and-cell-feed----------------------------------------------
# Get the data for worksheet "Oceania": the super-fast csv way
oceania_csv <- gap %>% gs_read_csv(ws = "Oceania")
str(oceania_csv)
oceania_csv

# Get the data for worksheet "Oceania": the less-fast tabular way ("list feed")
oceania_list_feed <- gap %>% gs_read_listfeed(ws = "Oceania")
str(oceania_list_feed)
oceania_list_feed

# Get the data for worksheet "Oceania": the slow cell-by-cell way ("cell feed")
oceania_cell_feed <- gap %>% gs_read_cellfeed(ws = "Oceania")
str(oceania_cell_feed)
oceania_cell_feed

## ------------------------------------------------------------------------
jfun <- function(readfun)
system.time(do.call(readfun, list(gs_gap(), ws = "Africa", verbose = FALSE)))
readfuns <- c("gs_read_csv", "gs_read_listfeed", "gs_read_cellfeed")
readfuns <- sapply(readfuns, get, USE.NAMES = TRUE)
sapply(readfuns, jfun)

## ----post-processing-----------------------------------------------------
# Reshape: instead of one row per cell, make a nice rectangular data.frame
australia_cell_feed <- gap %>%
gs_read_cellfeed(ws = "Oceania", range = "A1:F13")
str(australia_cell_feed)
oceania_cell_feed
australia_reshaped <- australia_cell_feed %>% gs_reshape_cellfeed()
str(australia_reshaped)
australia_reshaped

# Example: first 3 rows
gap_3rows <- gap %>% gs_read_cellfeed("Europe", range = cell_rows(1:3))
gap_3rows %>% head()

# convert to a data.frame (by default, column names found in first row)
gap_3rows %>% gs_reshape_cellfeed()

# arbitrary cell range, column names no longer available in first row
gap %>%
gs_read_cellfeed("Oceania", range = "D12:F15") %>%
gs_reshape_cellfeed(col_names = FALSE)

# arbitrary cell range, direct specification of column names
gap %>%
gs_read_cellfeed("Oceania", range = cell_limits(c(2, 1), c(5, 3))) %>%
gs_reshape_cellfeed(col_names = paste("thing", c("one", "two", "three"),
sep = "_"))

## ------------------------------------------------------------------------
# Example: first row only
gap_1row <- gap %>% gs_read_cellfeed("Europe", range = cell_rows(1))
gap_1row

# convert to a named character vector
gap_1row %>% gs_simplify_cellfeed()

# Example: single column
gap_1col <- gap %>% gs_read_cellfeed("Europe", range = cell_cols(3))
gap_1col

# convert to a un-named character vector and drop the variable name
gap_1col %>% gs_simplify_cellfeed(notation = "none", col_names = TRUE)

## ----new-sheet-----------------------------------------------------------
foo <- gs_new("foo")
foo

## ----edit-cells----------------------------------------------------------
## foo <- gs_new("foo")
## initialize the worksheets
foo <- foo %>% gs_ws_new("edit_cells")
foo <- foo %>% gs_ws_new("add_row")

## add first six rows of iris data (and var names) into a blank sheet
foo <- foo %>%
gs_edit_cells(ws = "edit_cells", input = head(iris), trim = TRUE)

## initialize sheet with column headers and one row of data
## the list feed is picky about this
foo <- foo %>%
gs_edit_cells(ws = "add_row", input = head(iris, 1), trim = TRUE)
## add the next 5 rows of data
for(i in 2:6) {
foo <- foo %>% gs_add_row(ws = "add_row", input = iris[i, ])
if(identical(tolower(Sys.getenv("NOT_CRAN")), "true")) {
NOT_CRAN <- TRUE
} else {
NOT_CRAN <- FALSE
}

## let's inspect out work
foo %>% gs_read(ws = "edit_cells")
foo %>% gs_read(ws = "add_row")

## ----delete-sheet--------------------------------------------------------
gs_delete(foo)

## ----new-sheet-from-file-------------------------------------------------
iris %>%
head(5) %>%
write.csv("iris.csv", row.names = FALSE)
iris_ss <- gs_upload("iris.csv")
iris_ss
iris_ss %>% gs_read()
file.remove("iris.csv")

## ----new-sheet-from-xlsx-------------------------------------------------
gap_xlsx <- gs_upload(system.file("mini-gap.xlsx", package = "googlesheets"))
gap_xlsx
gap_xlsx %>% gs_read(ws = "Asia")

## ----delete-moar-sheets--------------------------------------------------
gs_vecdel(c("iris", "mini-gap"))
## achieves same as:
## gs_delete(iris_ss)
## gs_delete(gap_xlsx)

## ----export-sheet-as-csv-------------------------------------------------
gs_title("Gapminder") %>%
gs_download(ws = "Africa", to = "gapminder-africa.csv")
## is it there? yes!
read.csv("gapminder-africa.csv") %>% head()

## ----export-sheet-as-xlsx------------------------------------------------
gs_title("Gapminder") %>%
gs_download(to = "gapminder.xlsx")

## ----clean-exported-files------------------------------------------------
file.remove(c("gapminder.xlsx", "gapminder-africa.csv"))

## ----gs_auth, eval = FALSE-----------------------------------------------
# # Give googlesheets permission to access your spreadsheets and google drive
# gs_auth()

## ----gs_user-------------------------------------------------------------
user_session_info <- gs_user()
user_session_info

Loading

0 comments on commit e1f6c23

Please sign in to comment.