## ----include = FALSE----------------------------------------------------------
# Writing to Google Drive needs a signed-in account, so no chunk is run here.
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----setup--------------------------------------------------------------------
# library(tsg)

## -----------------------------------------------------------------------------
# install.packages("googledrive")

## -----------------------------------------------------------------------------
# googledrive::drive_auth()

## -----------------------------------------------------------------------------
# googledrive::drive_auth(email = "you@example.com")

## -----------------------------------------------------------------------------
# sheet <- person_record |>
#   generate_frequency(sex) |>
#   add_table_title("Population by sex") |>
#   add_source_note("Source: person_record") |>
#   write_gsheet("Population by sex")

## -----------------------------------------------------------------------------
# googledrive::drive_browse(sheet) # open it in the browser
# googledrive::drive_link(sheet)   # its URL

## -----------------------------------------------------------------------------
# googledrive::drive_mkdir("reports")
# 
# person_record |>
#   generate_frequency(sex) |>
#   write_gsheet("reports/Population by sex")

## -----------------------------------------------------------------------------
# person_record |>
#   generate_frequency(marital_status) |>
#   write_gsheet(sheet)

## -----------------------------------------------------------------------------
# person_record |>
#   generate_frequency(marital_status) |>
#   write_gsheet(googledrive::as_id("1AbC...xyz"))

## -----------------------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   write_gsheet("Marital status by sex", facade = "striped")

## -----------------------------------------------------------------------------
# person_record |>
#   generate_frequency(sex) |>
#   add_facade(table.offsetRow = 1, table.offsetCol = 1) |>
#   write_gsheet(
#     "Population by sex",
#     title = "Population by sex",
#     footnotes = "Excludes persons with no reported sex."
#   )

## -----------------------------------------------------------------------------
# person_record |>
#   generate_frequency(sex, marital_status, employed) |>
#   write_gsheet("Population profile")

## -----------------------------------------------------------------------------
# person_record |>
#   dplyr::group_by(sex) |>
#   generate_summary(age, group_as_list = TRUE) |>
#   write_gsheet("Age by sex")

## -----------------------------------------------------------------------------
# person_record |>
#   generate_frequency(sex, marital_status, employed) |>
#   write_gsheet("Population profile", include_table_list = TRUE)

## -----------------------------------------------------------------------------
# person_record |>
#   generate_crosstab(marital_status, sex) |>
#   generate_output("Marital status by sex", format = "gsheet")

## -----------------------------------------------------------------------------
# # anyone with the link can view
# googledrive::drive_share_anyone(sheet)
# 
# # a specific person can edit
# googledrive::drive_share(sheet, role = "writer", type = "user", emailAddress = "colleague@example.com")

## -----------------------------------------------------------------------------
# googledrive::drive_auth(path = "service-account-key.json")

## -----------------------------------------------------------------------------
# googledrive::drive_deauth()
# googledrive::drive_auth(
#   scopes = "https://www.googleapis.com/auth/drive",
#   cache = FALSE
# )

## -----------------------------------------------------------------------------
# googledrive::drive_has_token() # FALSE: run googledrive::drive_auth()
# googledrive::drive_user()      # the signed-in account
# gargle::gargle_oauth_sitrep()  # tokens cached on disk

## -----------------------------------------------------------------------------
# googledrive::drive_find(n_max = 1)

