From 7b38a147504d4416a01f84ea01116e9262b05cd1 Mon Sep 17 00:00:00 2001 From: jennybc Date: Mon, 30 Mar 2015 11:18:57 -0700 Subject: [PATCH] admit that I must deal with visibility [covr] --- R/consume-data.R | 7 +++- R/googlesheet.R | 4 +- R/print.R | 7 ++-- R/register.R | 5 +-- .../02_create-published-to-the-web-sheet.R | 36 ++++++++++++++++++ man/googlesheet.Rd | 1 + tests/testthat/helper01_setup-sheets.R | 4 ++ tests/testthat/pts_identify_ss.rds | Bin 355 -> 364 bytes tests/testthat/test-download-spreadsheet.R | 2 +- tests/testthat/test-edit-data.R | 2 +- tests/testthat/test-edit-spreadsheets.R | 2 +- tests/testthat/test-register.R | 2 +- tests/testthat/test-zz-consume-public-data.R | 33 ++++++++++++++++ 13 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 data-for-demo/02_create-published-to-the-web-sheet.R create mode 100644 tests/testthat/test-zz-consume-public-data.R diff --git a/R/consume-data.R b/R/consume-data.R index 64258c6..b867c38 100644 --- a/R/consume-data.R +++ b/R/consume-data.R @@ -37,8 +37,11 @@ get_via_csv <- function(ss, ws = 1, ...) { this_ws <- get_ws(ss, ws) ## since gsheets_GET expects xml back, just using GET for now - req <- - httr::GET(this_ws$exportcsv, get_google_token()) + if(ss$is_public) { + req <- httr::GET(this_ws$exportcsv) + } else { + req <- httr::GET(this_ws$exportcsv, get_google_token()) + } if(is.null(httr::content(req))) { stop("Worksheet is empty. There are no cells that contain data.") diff --git a/R/googlesheet.R b/R/googlesheet.R index 52f8a31..b41902d 100644 --- a/R/googlesheet.R +++ b/R/googlesheet.R @@ -16,9 +16,10 @@ #' \item \code{sheet_id} the id of the spreadsheet #' \item \code{updated} the time of last update (at time of registration) #' \item \code{get_date} the time of registration -#' \item \code{visibility} visibility of spreadsheet (Google's confusing +#' \item \code{visibility} visibility of spreadsheet (Google's confusing #' vocabulary); actually, does not describe a property of spreadsheet itself but #' rather whether requests will be made with or without authentication +#' \item \code{is_public} logical indicating visibility is "public", as opposed to "private" #' \item \code{author_name} the name of the owner #' \item \code{author_email} the email of the owner #' \item \code{links} data.frame of links specific to the spreadsheet @@ -40,6 +41,7 @@ googlesheet <- function() { updated = character() %>% as.POSIXct(), get_date = character() %>% as.POSIXct(), visibility = character(), + is_public = logical(), author_name = character(), author_email = character(), links = character(), # initialize as data.frame? diff --git a/R/print.R b/R/print.R index 307006b..b0fb4fb 100644 --- a/R/print.R +++ b/R/print.R @@ -19,11 +19,12 @@ #' @export print.googlesheet <- function(x, ...) { - sprintf(" Spreadsheet title: %s\n", x$sheet_title) %>% cat + sprintf(" Spreadsheet title: %s\n", x$sheet_title) %>% cat() sprintf(" Date of googlesheets::register_ss: %s\n", - x$get_date %>% format.POSIXct(usetz = TRUE)) %>% cat + x$get_date %>% format.POSIXct(usetz = TRUE)) %>% cat() sprintf(" Date of last spreadsheet update: %s\n", - x$updated %>% format.POSIXct(usetz = TRUE)) %>% cat + x$updated %>% format.POSIXct(usetz = TRUE)) %>% cat() + sprintf(" visibility: %s\n", x$visibility) %>% cat() cat("\n") ws_output <- diff --git a/R/register.R b/R/register.R index 698276e..ca90f52 100644 --- a/R/register.R +++ b/R/register.R @@ -323,9 +323,8 @@ register_ss <- function(x, key = NULL, ws_feed = NULL, as.POSIXct(format = "%Y-%m-%dT%H:%M:%S", tz = "UTC") ss$get_date <- req$date - ## I'm ambivalent about even storing this; it's baked into the links - ## holdover from an earlier stage of pkg development ... omit? - ss$visibility <- req$url %>% dirname %>% basename + ss$visibility <- req$url %>% dirname() %>% basename() + ss$is_public <- ss$visibility == "public" ss$author_name <- req$content[["author"]][["name"]] ss$author_email <- req$content[["author"]][["email"]] diff --git a/data-for-demo/02_create-published-to-the-web-sheet.R b/data-for-demo/02_create-published-to-the-web-sheet.R new file mode 100644 index 0000000..dda9012 --- /dev/null +++ b/data-for-demo/02_create-published-to-the-web-sheet.R @@ -0,0 +1,36 @@ +#' --- +#' output: md_document +#' --- + +library("googlesheets") +suppressPackageStartupMessages(library("dplyr")) + +## damn you render and your hard-wiring of wd = dir where file lives! +## if I don't commit this abomination, existing .httr-oauth cannot be found :( +if (getwd() %>% basename == "data-for-demo") { + setwd("..") +} + +## "make clean" +delete_ss(regex = "^iris_public$") + +iris_ss <- new_ss("iris_public") +iris_ss + +iris_ss <- iris_ss %>% + edit_cells(input = head(iris), header = TRUE, trim = TRUE) +iris_ss + +iris_ss %>% get_via_lf() +iris_ss %>% get_via_csv() + +## via browser, publish this sheet to the web +## in sheets, File > Publish to the web ... +## in future, do programmatically w/ googlesheets or driver +## https://github.com/jennybc/googlesheets/issues/62 +## https://docs.google.com/spreadsheets/d/1cAYN-a089TSw8GF0RadQNdZiWo2RzekT-8swZeYME4A/pubhtml + +iris_key <- "1cAYN-a089TSw8GF0RadQNdZiWo2RzekT-8swZeYME4A" +iris_public <- register_ss(key = iris_key, visibility = "public") +iris_public +iris_public %>% get_via_csv() diff --git a/man/googlesheet.Rd b/man/googlesheet.Rd index 810c320..36840cf 100644 --- a/man/googlesheet.Rd +++ b/man/googlesheet.Rd @@ -27,6 +27,7 @@ includes the fields: \item \code{visibility} visibility of spreadsheet (Google's confusing vocabulary); actually, does not describe a property of spreadsheet itself but rather whether requests will be made with or without authentication +\item \code{is_public} logical indicating visibility is "public", as opposed to "private" \item \code{author_name} the name of the owner \item \code{author_email} the email of the owner \item \code{links} data.frame of links specific to the spreadsheet diff --git a/tests/testthat/helper01_setup-sheets.R b/tests/testthat/helper01_setup-sheets.R index 1bc5b02..ede6db8 100644 --- a/tests/testthat/helper01_setup-sheets.R +++ b/tests/testthat/helper01_setup-sheets.R @@ -29,3 +29,7 @@ old_title <- "Old Style Sheet" ## WARNING! this is not the key provided for this sheet by list_sheets() ## so it seems we cannot obtain a key from old style Sheet URLs # old_key <- "0Audw-qi1jh3fdG9qZFVLR0NvZGRyNHFNUFBtN2tudUE" + +## "Published to the web" sheet +## For accessing via visibility = "public" w/o ever having authenticated +ptw_key <- "1cAYN-a089TSw8GF0RadQNdZiWo2RzekT-8swZeYME4A" diff --git a/tests/testthat/pts_identify_ss.rds b/tests/testthat/pts_identify_ss.rds index 2e3d611e9dd33579f42bba5eda9b5a617713d47d..96258893aa8dc7e6663f3a09a421c0e242c3cbd2 100644 GIT binary patch literal 364 zcmV-y0h9h8iwFP!000001C>%uQ^GJ14FN?!9B1$c@B@^{_;K*Y1;>*e%K^D`N}IN^ zp{Y#@!jA`&HgT}i3zW67sCo-Vfb&&+y^Xy$Rtjz8}G*N5-8a`(o{my~$J8Rmi{;n0pS7PdH#-RcTxB zh{>5pUCwn9)L`3sx8LTT(C&HRit0_VV+pPZQ)~afRO4{M>+n$jpwR%jN8PNmgF20g zyTV3eUxS^Zu}+M)8t9dPAcA5IwGwB;4eJee)L+r=MVU>E-_BECw)`&~%Af+Y-(3?< zmV^>v%5?~L(qdvveK!{o%N#{kLzf!DE0<6shLR*Uq@bi%-{f97Oa)7$h7zs@a{mi1 Krf0hg0{{S1ShW8D literal 355 zcmV-p0i6CHiwFP!000001C>(CQo=A44FN?SI?mt+@BvC>d@S6t!Et5BvOqSSKGHTe zG_^@V_;@gB69ySvkeTG>o_mvf&P}Fm000E_8U(N|xAmS}x^iC2SpyBwlJ<5OhvD;R zJs!=Xao|1OvGmg)j%O?TXJS3ZH21k>Cm;90>%(_)aQ4P3JWBlj1am>ssBgy@3yEKX zYV7BJ#X@BK1tFg0*bTT!<%5W^h~k0I<|xEC;AqM6q>7~AX&EHbB?FqNT?ePnZhJS7En6}iN zW23FB!B*K&C&4QXbSgj)L9vD!sk7vU?UFm{HMDzCWfS8!i!6|>-i0F>RDkx|3&M#< zC=t7IZXsfpJBpizx*EcBmr%o% head(6) +## because our data consumption m.o. is stringsAsFactors = FALSE +iris_ish$Species <- iris_ish$Species %>% as.character() + +test_that("We can get data 'published to the web' sheet w/o authenticating, via csv", { + + tmp <- iris_public %>% get_via_csv() + expect_equivalent(tmp, iris_ish) + +}) + +test_that("We can get data 'published to the web' w/o authenticating, via list feed", { + + iris_ish2 <- iris_ish + ## because the list feed will lowercase the names + names(iris_ish2) <- names(iris_ish2) %>% tolower() + + tmp <- iris_public %>% get_via_lf() + expect_equivalent(tmp, iris_ish) + + +}) + +setwd(old_wd)