Install prereqs

# R
install.packages(c("googlesheets", "lubridate"))
# python
source activate class
pip install gspread

Pull data in R

Authenticate

When you run gs_auth() for the first time (easiest from within RStudio rather than in the middle of a knitted doc!), it asks you for your google credentials and stores them in an .httr-oauth file. Delete this file to remove your credentials and NEVER commit an .httr-oauth file to github - best to always include the following lines in your .gitignore file:

# exclude authentication tokens
.httr-oauth
gs_auth()

Identify spreadsheet

# identify spreadsheet by name
spreadsheet_title <- "class log test"
gs <- gs_title(spreadsheet_title)
## Sheet successfully identified: "class log test"

Read data from worksheet (tab)

data <- gs %>% gs_read_csv(ws = "light")
## Accessing worksheet titled 'light'.
## Parsed with column specification:
## cols(
##   published_at = col_datetime(format = ""),
##   location = col_character(),
##   cds = col_integer()
## )

Process data

The key is to adjust the timezone from the UTC based timestamp. Notice a couple of data outages.

data %>% 
  mutate(published_at = published_at %>% with_tz("America/Denver")) %>% 
  ggplot() +
  aes(x = published_at, y = cds, color = location) +
  geom_line() +
  scale_x_datetime() +
  theme_bw()

Overplot individual days. Notice the shift in daylight savings that suddenly moved everything by an hour!

data %>% 
  mutate(
    published_at = published_at %>% with_tz("America/Denver"),
    day = paste(month(published_at, label = TRUE), day(published_at)),
    time = published_at %>% { hour(.) + minute(.)/60 + second(.)/3600 }
  ) %>% 
  ggplot() +
  aes(x = time, y = cds, color = day) +
  geom_line() +
  scale_x_continuous(breaks = 0:24, expand = c(0,.01)) +
  theme_bw()
## Warning: Removed 2 rows containing missing values (geom_path).

Interactive plot.

ggplotly()

Pull data in Python

Use the gspread library.

import gspread