From 96a3f6e460aa2055d46019f7edfbbc0ed7f6863c Mon Sep 17 00:00:00 2001 From: David Mayer Date: Mon, 17 Nov 2025 11:06:09 -0500 Subject: [PATCH 01/25] prep --- DESCRIPTION | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 61d7bc0..62acb26 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,9 +1,11 @@ Package: programets Title: Collect and analyze academic impact metrics from various sources -Version: 0.3.0 +Version: 0.3.1 Authors@R: - c(person("Sean", "Davis", , "seandavi@gmail.com", role = c("aut", "cre"), comment=c(ORCID = "0000-0002-8991-6458")), - person("David", "Mayer", , "david.mayer@cuanschutz.edu", role = c("aut"))) + c( + person("Sean", "Davis", , "seandavi@gmail.com", role = c("aut", "cre"), comment=c(ORCID = "0000-0002-8991-6458")), + person("David", "Mayer", , "david.mayer@cuanschutz.edu", role = c("aut"), comment=c(ORCID = "0000-0002-6056-9771")) + ) Description: Collect and analyze academic impact metrics from various sources. The package provides functions to collect and analyze data from NIH reporter, @@ -13,7 +15,7 @@ Description: License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 Depends: R (>= 4.1.0) Imports: From 6edb88a71b6df215942e2320d00ff7aa8d080960 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Mon, 17 Nov 2025 15:44:49 -0500 Subject: [PATCH 02/25] beginnings of export function --- NAMESPACE | 5 ++++ R/export.R | 62 +++++++++++++++++++++++++++++++++++++++++++++ man/export_excel.Rd | 17 +++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 R/export.R create mode 100644 man/export_excel.Rd diff --git a/NAMESPACE b/NAMESPACE index 1b1fb9e..ed4c3f7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand export(epmc_search) +export(export_excel) export(ga_dataframe) export(ga_meta_simple) export(ga_query_explorer) @@ -35,6 +36,10 @@ importFrom(httr2,request) importFrom(httr2,resp_body_json) importFrom(httr2,resp_status) importFrom(jsonlite,fromJSON) +importFrom(openxlsx,addWorksheet) +importFrom(openxlsx,createWorkbook) +importFrom(openxlsx,saveWorkbook) +importFrom(openxlsx,writeData) importFrom(purrr,discard) importFrom(purrr,map2_dbl) importFrom(purrr,map_chr) diff --git a/R/export.R b/R/export.R new file mode 100644 index 0000000..ef165cc --- /dev/null +++ b/R/export.R @@ -0,0 +1,62 @@ +export_project <- function(core_project_numbers, export = c("csv", "json", "excel")) { + + export <- match.arg(export) + + if(export == "csv") { + export_csv(core_project_numbers) + } + + if(export == "json") { + export_json(core_project_numbers) + } + + if(export == "excel") { + export_excel(core_project_numbers) + } + +} + +export_csv <- function(core_project_numbers) { + +} + +export_js <- function(core_project_numbers) { + +} + +#' Export to Excel +#' +#' @importFrom openxlsx createWorkbook addWorksheet writeData saveWorkbook +#' @export +#' +#' @examples +#' \dontrun{ +#' test_projects <-c("OT2OD030545") +#' } +#' +export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password) { + + ## Create Excel Workbook + wb <- createWorkbook() + + ## Add NIH Project Info + addWorksheet(wb, "project_info") + proj_info <- get_core_project_info(core_project_numbers) + writeData(wb = wb, sheet = "project_info", x = proj_info, na.string = "") + + ## Add Assosciated Publications + addWorksheet(wb, "pub_info") + pmids <- proj_info |> + filter(found_publication) |> + pull(pmid) + pub_info <- icite(pmids) + writeData(wb = wb, sheet = "pub_info", x = pub_info, na.string = "") + + ## Add GitHub + addWorksheet(wb, "github_info") + github_info <- get_github_by_topic_graphql(core_project_numbers, token = token) + writeData(wb = wb, sheet = "github_info", x = github_info, na.string = "") + + ## Save Workbook + saveWorkbook(wb, paste0("programets_", Sys.Date(), ".xlsx", sep = "")) +} \ No newline at end of file diff --git a/man/export_excel.Rd b/man/export_excel.Rd new file mode 100644 index 0000000..e73acc8 --- /dev/null +++ b/man/export_excel.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/export.R +\name{export_excel} +\alias{export_excel} +\title{Export to Excel} +\usage{ +export_excel(core_project_numbers) +} +\description{ +Export to Excel +} +\examples{ +\dontrun{ +test_projects <-c("OT2OD030545") +} + +} From 64288b79bdd9229b5e79fdc159050c6be82029c7 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 19 Nov 2025 10:54:38 -0500 Subject: [PATCH 03/25] sp --- DEVELOPER.md | 6 +++--- ...4-acess-keyfile.json => ga4-access-keyfile.json} | Bin tests/testthat/test-ga_dataframe.R | 2 +- vignettes/googleanalytics.qmd | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename inst/secret/{ga4-acess-keyfile.json => ga4-access-keyfile.json} (100%) diff --git a/DEVELOPER.md b/DEVELOPER.md index e57b807..d26f7d5 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -38,9 +38,9 @@ Step 3: Encrypt the google service account json file ```{r} gargle::secret_encrypt_json( - path = "path/to/ga4-acess-keyfile.json", + path = "path/to/ga4-access-keyfile.json", key = "GARGLE_ENCRYPTION_KEY", - output = "inst/secret/ga4-acess-keyfile.json" + output = "inst/secret/ga4-access-keyfile.json" ) ``` This will create an encrypted version of the json file in the `inst/secret` directory. @@ -51,7 +51,7 @@ Step 4: Use the encrypted file in your code library(gargle) googleAnalyticsR::ga_auth( json_file = gargle::secret_decrypt_json( - path = system.file("secret/ga4-acess-keyfile.json", package = "programets"), + path = system.file("secret/ga4-access-keyfile.json", package = "programets"), key = "GARGLE_ENCRYPTION_KEY" ) ) diff --git a/inst/secret/ga4-acess-keyfile.json b/inst/secret/ga4-access-keyfile.json similarity index 100% rename from inst/secret/ga4-acess-keyfile.json rename to inst/secret/ga4-access-keyfile.json diff --git a/tests/testthat/test-ga_dataframe.R b/tests/testthat/test-ga_dataframe.R index a294431..8f199ac 100644 --- a/tests/testthat/test-ga_dataframe.R +++ b/tests/testthat/test-ga_dataframe.R @@ -6,7 +6,7 @@ test_that("ga_dataframe returns expected tibble", { json_file <- gargle::secret_decrypt_json( path = system.file( "secret", - "ga4-acess-keyfile.json", + "ga4-access-keyfile.json", package = "programets" ), key = "GARGLE_ENCRYPTION_KEY" diff --git a/vignettes/googleanalytics.qmd b/vignettes/googleanalytics.qmd index 3b14c9f..a483f1f 100644 --- a/vignettes/googleanalytics.qmd +++ b/vignettes/googleanalytics.qmd @@ -39,7 +39,7 @@ ga_auth(email = "seandavi@gmail.com") json_file <- gargle::secret_decrypt_json( path = system.file( "secret", - "ga4-acess-keyfile.json", + "ga4-access-keyfile.json", package = "programets" ), key = "GARGLE_ENCRYPTION_KEY" From 88a157f4229ecfe2ef07fb2da2e2afb8607553f1 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Mon, 1 Dec 2025 15:57:49 -0500 Subject: [PATCH 04/25] prep for google analytics inclusion --- R/export.R | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/R/export.R b/R/export.R index ef165cc..b62fd5f 100644 --- a/R/export.R +++ b/R/export.R @@ -57,6 +57,48 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ github_info <- get_github_by_topic_graphql(core_project_numbers, token = token) writeData(wb = wb, sheet = "github_info", x = github_info, na.string = "") + ## Add Google Analytics + addWorksheet(wb, "ga_info") + json_file <- gargle::secret_decrypt_json( + path = system.file( + "secret", + "cfde-access-keyfile.json", + package = "programets" + ), + key = "CFDE_ENCRYPTION_KEY" + ) + googleAnalyticsR::ga_auth( + json_file = json_file + ) + account_list <- ga_account_list("ga4") |> + mutate( + property_meta = map(propertyId, get_ga_meta_by_id), + core_project_num = map_chr( + property_meta, + ~{ + res <- .x |> + filter(str_detect(apiName, core_project_numbers)) |> + tidyr::separate(apiName, into = c("api", "value"), sep = ":", remove = FALSE) |> + pull(value) + + if (length(res) == 0) { + NA_character_ + } else { + res |> + str_remove("^cfde_") |> + unique() |> + paste(collapse = ",") + } + } + ) + ) |> + filter(!is.na(core_project_num)) |> + select(-property_meta) + writeData(wb = wb, sheet = "ga_info", x = account_list, na.string = "") + + ga_info <- get_ga_meta_by_id(core_project_numbers) + writeData(wb = wb, sheet = "ga_info", x = ga_info, na.string = "") + ## Save Workbook saveWorkbook(wb, paste0("programets_", Sys.Date(), ".xlsx", sep = "")) } \ No newline at end of file From 2b86a3831c32c635a78fa44d427ef6b6980efdd2 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Mon, 1 Dec 2025 15:59:55 -0500 Subject: [PATCH 05/25] ga_meta by propertyId helper --- R/ga_meta_simple.R | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/R/ga_meta_simple.R b/R/ga_meta_simple.R index e3882c7..3ac83b4 100644 --- a/R/ga_meta_simple.R +++ b/R/ga_meta_simple.R @@ -26,4 +26,37 @@ #' @export ga_meta_simple <- function() { tibble::as_tibble(googleAnalyticsR::ga_meta(version = "data")) +} + +#' Google Analytics metadata dataframe by property ID +#' +#' This function retrieves Google Analytics metadata by property ID +#' and returns it as a dataframe. The metadata includes +#' information about metrics, dimensions, and other +#' attributes available in Google Analytics. +#' +#' This function is a wrapper around the +#' `googleAnalyticsR::ga_meta()` function. It retrieves +#' metadata for the Google Analytics API version 4. +#' +#' @param property_id The property ID for which to retrieve +#' metadata. +#' +#' @note This function requires first authenticating to +#' Google Analytics using the `ga_auth()` function. +#' +#' @family Google Analytics +#' +#' @examples +#' \dontrun{ +#' res = get_ga_meta_by_id("123456789") +#' head(res) +#' dplyr::glimpse(res) +#' } +#' +#' @return A tibble containing Google Analytics metadata. +#' +#' @export +get_ga_meta_by_id <- function(property_id) { + tibble::as_tibble(googleAnalyticsR::ga_meta(version = "data", propertyId = property_id)) } \ No newline at end of file From fb2348f8deb49f24dabd49864eae9089f2f2b766 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Tue, 2 Dec 2025 13:29:32 -0500 Subject: [PATCH 06/25] misc updates - NAMESPACE - docs - gitignore --- .gitignore | 2 ++ NAMESPACE | 1 + man/export_excel.Rd | 2 +- man/ga_meta_simple.Rd | 3 ++- man/ga_query_explorer.Rd | 3 ++- man/get_ga_meta_by_id.Rd | 44 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 man/get_ga_meta_by_id.Rd diff --git a/.gitignore b/.gitignore index 6f4bb29..9a4aaa0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ docs terraform/ /.quarto/ **/*.quarto_ipynb +inst/secret/cfde-access-keyfile.json +*.xlsx diff --git a/NAMESPACE b/NAMESPACE index ed4c3f7..08e44bc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(ga_dataframe) export(ga_meta_simple) export(ga_query_explorer) export(get_core_project_info) +export(get_ga_meta_by_id) export(get_github_by_topic) export(get_github_by_topic_graphql) export(icite) diff --git a/man/export_excel.Rd b/man/export_excel.Rd index e73acc8..054527f 100644 --- a/man/export_excel.Rd +++ b/man/export_excel.Rd @@ -4,7 +4,7 @@ \alias{export_excel} \title{Export to Excel} \usage{ -export_excel(core_project_numbers) +export_excel(core_project_numbers, token = gitcreds::gitcreds_get()$password) } \description{ Export to Excel diff --git a/man/ga_meta_simple.Rd b/man/ga_meta_simple.Rd index 6d57073..72a94fc 100644 --- a/man/ga_meta_simple.Rd +++ b/man/ga_meta_simple.Rd @@ -34,6 +34,7 @@ dplyr::glimpse(res) } \seealso{ Other Google Analytics: -\code{\link{ga_query_explorer}()} +\code{\link{ga_query_explorer}()}, +\code{\link{get_ga_meta_by_id}()} } \concept{Google Analytics} diff --git a/man/ga_query_explorer.Rd b/man/ga_query_explorer.Rd index d74816e..fc49f77 100644 --- a/man/ga_query_explorer.Rd +++ b/man/ga_query_explorer.Rd @@ -28,6 +28,7 @@ ga_query_explorer() } \seealso{ Other Google Analytics: -\code{\link{ga_meta_simple}()} +\code{\link{ga_meta_simple}()}, +\code{\link{get_ga_meta_by_id}()} } \concept{Google Analytics} diff --git a/man/get_ga_meta_by_id.Rd b/man/get_ga_meta_by_id.Rd new file mode 100644 index 0000000..9885205 --- /dev/null +++ b/man/get_ga_meta_by_id.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ga_meta_simple.R +\name{get_ga_meta_by_id} +\alias{get_ga_meta_by_id} +\title{Google Analytics metadata dataframe by property ID} +\usage{ +get_ga_meta_by_id(property_id) +} +\arguments{ +\item{property_id}{The property ID for which to retrieve +metadata.} +} +\value{ +A tibble containing Google Analytics metadata. +} +\description{ +This function retrieves Google Analytics metadata by property ID +and returns it as a dataframe. The metadata includes +information about metrics, dimensions, and other +attributes available in Google Analytics. +} +\details{ +This function is a wrapper around the +\code{googleAnalyticsR::ga_meta()} function. It retrieves +metadata for the Google Analytics API version 4. +} +\note{ +This function requires first authenticating to +Google Analytics using the \code{ga_auth()} function. +} +\examples{ +\dontrun{ +res = get_ga_meta_by_id("123456789") +head(res) +dplyr::glimpse(res) +} + +} +\seealso{ +Other Google Analytics: +\code{\link{ga_meta_simple}()}, +\code{\link{ga_query_explorer}()} +} +\concept{Google Analytics} From 7680936264ec8953f31c732917aed56caad60dae Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 14:01:59 -0500 Subject: [PATCH 07/25] updates - add missing namespace deps - update docs - refactor auth --- NAMESPACE | 4 ++++ R/export.R | 36 +--------------------------- R/get_ga.R | 58 +++++++++++++++++++++++++++++++++++++++++++++ man/get_ga_basic.Rd | 19 +++++++++++++++ 4 files changed, 82 insertions(+), 35 deletions(-) create mode 100644 R/get_ga.R create mode 100644 man/get_ga_basic.Rd diff --git a/NAMESPACE b/NAMESPACE index 08e44bc..9df0d20 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(ga_dataframe) export(ga_meta_simple) export(ga_query_explorer) export(get_core_project_info) +export(get_ga_basic) export(get_ga_meta_by_id) export(get_github_by_topic) export(get_github_by_topic_graphql) @@ -24,6 +25,7 @@ importFrom(dplyr,tibble) importFrom(ghql,GraphqlClient) importFrom(glue,glue) importFrom(glue,glue_collapse) +importFrom(googleAnalyticsR,ga_account_list) importFrom(httr2,req_auth_bearer_token) importFrom(httr2,req_body_json) importFrom(httr2,req_error) @@ -42,6 +44,7 @@ importFrom(openxlsx,createWorkbook) importFrom(openxlsx,saveWorkbook) importFrom(openxlsx,writeData) importFrom(purrr,discard) +importFrom(purrr,map) importFrom(purrr,map2_dbl) importFrom(purrr,map_chr) importFrom(purrr,map_dbl) @@ -55,4 +58,5 @@ importFrom(rlang,inform) importFrom(stats,setNames) importFrom(stringr,regex) importFrom(stringr,str_detect) +importFrom(stringr,str_remove) importFrom(tibble,tibble) diff --git a/R/export.R b/R/export.R index b62fd5f..f2c5743 100644 --- a/R/export.R +++ b/R/export.R @@ -59,41 +59,7 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ ## Add Google Analytics addWorksheet(wb, "ga_info") - json_file <- gargle::secret_decrypt_json( - path = system.file( - "secret", - "cfde-access-keyfile.json", - package = "programets" - ), - key = "CFDE_ENCRYPTION_KEY" - ) - googleAnalyticsR::ga_auth( - json_file = json_file - ) - account_list <- ga_account_list("ga4") |> - mutate( - property_meta = map(propertyId, get_ga_meta_by_id), - core_project_num = map_chr( - property_meta, - ~{ - res <- .x |> - filter(str_detect(apiName, core_project_numbers)) |> - tidyr::separate(apiName, into = c("api", "value"), sep = ":", remove = FALSE) |> - pull(value) - - if (length(res) == 0) { - NA_character_ - } else { - res |> - str_remove("^cfde_") |> - unique() |> - paste(collapse = ",") - } - } - ) - ) |> - filter(!is.na(core_project_num)) |> - select(-property_meta) + writeData(wb = wb, sheet = "ga_info", x = account_list, na.string = "") ga_info <- get_ga_meta_by_id(core_project_numbers) diff --git a/R/get_ga.R b/R/get_ga.R new file mode 100644 index 0000000..8fd60c6 --- /dev/null +++ b/R/get_ga.R @@ -0,0 +1,58 @@ +#' Get Basic Google Analytics Data +#' +#' This function takes a character vector of NIH Core Project Numbers and +#' returns a data frame containing the any Google Analytics properties associated +#' with the Core Project Numbers. +#' +#' @param core_project_numbers A character vector of NIH Core Project Numbers +#' +#' @importFrom googleAnalyticsR ga_account_list +#' @importFrom purrr map map_chr +#' @importFrom stringr str_remove +#' +#' @return A data frame containing the associated Google Analytics data +#' @export +get_ga_basic <- function(core_project_numbers, token = 'cfde-access-keyfile.json') { + ## This function requires authentication, check for existing creds + if(file.exists(system.file("secret", token, package = "programets"))){ + json_file <- gargle::secret_decrypt_json( + path = system.file( + "secret", + "cfde-access-keyfile.json", + package = "programets" + ), + key = "CFDE_ENCRYPTION_KEY" + ) + googleAnalyticsR::ga_auth( + json_file = json_file + ) + } else { + ga_auth() + } + + ## Get All Analytics Properties + account_list <- ga_account_list("ga4") |> + mutate( + property_meta = suppressMessages(map(propertyId, get_ga_meta_by_id)), + core_project_num = map_chr( + property_meta, + ~{ + res <- .x |> + filter(str_detect(apiName, core_project_numbers)) |> + tidyr::separate(apiName, into = c("api", "value"), sep = ":", remove = FALSE) |> + pull(value) + if (length(res) == 0) { + NA_character_ + } else { + res |> + str_remove("^cfde_") |> + unique() |> + paste(collapse = ",") + } + } + ) + ) |> + ## Filter to those with the requested Core Project Numbers + filter(!is.na(core_project_num)) |> + select(-property_meta) + } diff --git a/man/get_ga_basic.Rd b/man/get_ga_basic.Rd new file mode 100644 index 0000000..92dde29 --- /dev/null +++ b/man/get_ga_basic.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_ga.R +\name{get_ga_basic} +\alias{get_ga_basic} +\title{Get Basic Google Analytics Data} +\usage{ +get_ga_basic(core_project_numbers, token = "cfde-access-keyfile.json") +} +\arguments{ +\item{core_project_numbers}{A character vector of NIH Core Project Numbers} +} +\value{ +A data frame containing the associated Google Analytics data +} +\description{ +This function takes a character vector of NIH Core Project Numbers and +returns a data frame containing the any Google Analytics properties associated +with the Core Project Numbers. +} From 1e06f2d0a248054952a4e4f7468a548c14dafd35 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 14:03:41 -0500 Subject: [PATCH 08/25] rename --- R/{get_ga.R => get_ga_basic.R} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename R/{get_ga.R => get_ga_basic.R} (98%) diff --git a/R/get_ga.R b/R/get_ga_basic.R similarity index 98% rename from R/get_ga.R rename to R/get_ga_basic.R index 8fd60c6..a8ca3fd 100644 --- a/R/get_ga.R +++ b/R/get_ga_basic.R @@ -1,4 +1,4 @@ -#' Get Basic Google Analytics Data +#' Get Basic Google Analytics Info #' #' This function takes a character vector of NIH Core Project Numbers and #' returns a data frame containing the any Google Analytics properties associated From 456e1eb7701a940da300376dddf7c22ddd181ddc Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 14:06:31 -0500 Subject: [PATCH 09/25] update param, disambiguate from github tokens --- R/get_ga_basic.R | 6 +++--- man/get_ga_basic.Rd | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index a8ca3fd..d8ec270 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -12,13 +12,13 @@ #' #' @return A data frame containing the associated Google Analytics data #' @export -get_ga_basic <- function(core_project_numbers, token = 'cfde-access-keyfile.json') { +get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-access-keyfile.json') { ## This function requires authentication, check for existing creds - if(file.exists(system.file("secret", token, package = "programets"))){ + if(file.exists(system.file("secret", service_account_json, package = "programets"))){ json_file <- gargle::secret_decrypt_json( path = system.file( "secret", - "cfde-access-keyfile.json", + service_account_json, package = "programets" ), key = "CFDE_ENCRYPTION_KEY" diff --git a/man/get_ga_basic.Rd b/man/get_ga_basic.Rd index 92dde29..bd5747c 100644 --- a/man/get_ga_basic.Rd +++ b/man/get_ga_basic.Rd @@ -1,10 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get_ga.R +% Please edit documentation in R/get_ga_basic.R \name{get_ga_basic} \alias{get_ga_basic} -\title{Get Basic Google Analytics Data} +\title{Get Basic Google Analytics Info} \usage{ -get_ga_basic(core_project_numbers, token = "cfde-access-keyfile.json") +get_ga_basic( + core_project_numbers, + service_account_json = "cfde-access-keyfile.json" +) } \arguments{ \item{core_project_numbers}{A character vector of NIH Core Project Numbers} From 0cddefdefe3e62fe895a6b592d8db9ecc622d0e4 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 14:31:31 -0500 Subject: [PATCH 10/25] make case insensitive --- R/get_ga_basic.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index d8ec270..4e7f0db 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -38,7 +38,7 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce property_meta, ~{ res <- .x |> - filter(str_detect(apiName, core_project_numbers)) |> + filter(str_detect(apiName, regex(core_project_numbers, ignore_case = TRUE))) |> tidyr::separate(apiName, into = c("api", "value"), sep = ":", remove = FALSE) |> pull(value) if (length(res) == 0) { @@ -55,4 +55,6 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce ## Filter to those with the requested Core Project Numbers filter(!is.na(core_project_num)) |> select(-property_meta) + + return(account_list) } From 15675ca6a7ed94475a19496533c6773ae6ecb41a Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 14:31:37 -0500 Subject: [PATCH 11/25] cleanup --- R/export.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/export.R b/R/export.R index f2c5743..4cff908 100644 --- a/R/export.R +++ b/R/export.R @@ -1,4 +1,7 @@ -export_project <- function(core_project_numbers, export = c("csv", "json", "excel")) { +export_project <- function( + core_project_numbers, export = c("csv", "json", "excel"), + token = gitcreds::gitcreds_get()$password, + service_account_json = 'cfde-access-keyfile.json', file) { export <- match.arg(export) @@ -34,7 +37,7 @@ export_js <- function(core_project_numbers) { #' test_projects <-c("OT2OD030545") #' } #' -export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password) { +export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', file) { ## Create Excel Workbook wb <- createWorkbook() @@ -59,10 +62,7 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ ## Add Google Analytics addWorksheet(wb, "ga_info") - - writeData(wb = wb, sheet = "ga_info", x = account_list, na.string = "") - - ga_info <- get_ga_meta_by_id(core_project_numbers) + ga_info <- get_ga_basic(core_project_numbers = core_project_numbers, service_account_json = service_account_json) writeData(wb = wb, sheet = "ga_info", x = ga_info, na.string = "") ## Save Workbook From 436868d4db2e2bdeecd398d9968988ac318d8797 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 14:56:23 -0500 Subject: [PATCH 12/25] utilize file param for workbook export --- R/export.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/export.R b/R/export.R index 4cff908..a54e5dd 100644 --- a/R/export.R +++ b/R/export.R @@ -66,5 +66,5 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ writeData(wb = wb, sheet = "ga_info", x = ga_info, na.string = "") ## Save Workbook - saveWorkbook(wb, paste0("programets_", Sys.Date(), ".xlsx", sep = "")) + saveWorkbook(wb, paste0("programets_", Sys.Date(), ".xlsx", sep = ""), file = file) } \ No newline at end of file From e74a428f196f1d9fc4769db807fca99521d8577a Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 15:30:47 -0500 Subject: [PATCH 13/25] add messaging, if empty return --- R/get_ga_basic.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index 4e7f0db..9f3b672 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -55,6 +55,8 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce ## Filter to those with the requested Core Project Numbers filter(!is.na(core_project_num)) |> select(-property_meta) - + if(nrow(account_list) == 0) { + rlang::inform(rlang::format_error_bullets(c(i = "No Google Analytics properties found for the requested Core Project Numbers"))) + } return(account_list) } From 8a65736cbffdc7058c96acbdf91421280909a87d Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 15:48:09 -0500 Subject: [PATCH 14/25] fix regex filter, when multiple core projects are specified --- R/get_ga_basic.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index 9f3b672..99c2f24 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -31,6 +31,7 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce } ## Get All Analytics Properties + core_project_regex <- paste0(core_project_numbers, collapse = "|") account_list <- ga_account_list("ga4") |> mutate( property_meta = suppressMessages(map(propertyId, get_ga_meta_by_id)), @@ -38,7 +39,7 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce property_meta, ~{ res <- .x |> - filter(str_detect(apiName, regex(core_project_numbers, ignore_case = TRUE))) |> + filter(str_detect(apiName, regex(core_project_regex, ignore_case = TRUE))) |> tidyr::separate(apiName, into = c("api", "value"), sep = ":", remove = FALSE) |> pull(value) if (length(res) == 0) { From 761bbf52cd88e2069b606dd18d61e191d4220282 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 15:48:26 -0500 Subject: [PATCH 15/25] add test for get_ga_basic() --- tests/testthat/test-get_ga_basic.R | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/testthat/test-get_ga_basic.R diff --git a/tests/testthat/test-get_ga_basic.R b/tests/testthat/test-get_ga_basic.R new file mode 100644 index 0000000..1f1ebe8 --- /dev/null +++ b/tests/testthat/test-get_ga_basic.R @@ -0,0 +1,11 @@ +test_that("get_ga_basic returns expected values", { + core_project_numbers <- c("u54od036472", "99999999") + result <- get_ga_basic(core_project_numbers = core_project_numbers) + expect_s3_class(result, "tbl_df") + expect_true("account_name" %in% names(result)) + expect_true("accountId" %in% names(result)) + expect_true("propertyId" %in% names(result)) + expect_true("property_name" %in% names(result)) + expect_true("core_project_num" %in% names(result)) + } +) From 8d054649d931a5018ecd04e2315f01ef4403080e Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 15:51:28 -0500 Subject: [PATCH 16/25] update docs --- man/export_excel.Rd | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/man/export_excel.Rd b/man/export_excel.Rd index 054527f..8a30e08 100644 --- a/man/export_excel.Rd +++ b/man/export_excel.Rd @@ -4,7 +4,12 @@ \alias{export_excel} \title{Export to Excel} \usage{ -export_excel(core_project_numbers, token = gitcreds::gitcreds_get()$password) +export_excel( + core_project_numbers, + token = gitcreds::gitcreds_get()$password, + service_account_json = "cfde-access-keyfile.json", + file +) } \description{ Export to Excel From d3a4cf76d2f6f8b0b93e513be430503966f8795d Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 15:53:52 -0500 Subject: [PATCH 17/25] basic validation (ensure project numbers are unique) --- R/get_ga_basic.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index 99c2f24..56e205b 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -31,7 +31,7 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce } ## Get All Analytics Properties - core_project_regex <- paste0(core_project_numbers, collapse = "|") + core_project_regex <- paste0(unique(tolower(core_project_numbers)), collapse = "|") account_list <- ga_account_list("ga4") |> mutate( property_meta = suppressMessages(map(propertyId, get_ga_meta_by_id)), From f0f98515df93d2486d79003f28dc2c8dd7f6478e Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 15:54:41 -0500 Subject: [PATCH 18/25] add openxlsx to DESCRIPTION --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 62acb26..61ad147 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,8 @@ Imports: stringr, tibble, glue, - ghql + ghql, + openxlsx Suggests: gargle, gitcreds, From 2c2858739c7b24c133b554852619323e72ef7f43 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 16:17:52 -0500 Subject: [PATCH 19/25] misc R CMD check fixes --- .Rbuildignore | 1 + DESCRIPTION | 3 ++- NAMESPACE | 2 ++ R/export.R | 39 ++++++++------------------------------- R/get_ga_basic.R | 14 +++++++++----- man/export_excel.Rd | 9 +++++++++ man/get_ga_basic.Rd | 3 +++ 7 files changed, 34 insertions(+), 37 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 8bee5ea..1d32942 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -11,4 +11,5 @@ terraform/ DEVELOPER.md ^README\.Rmd$ ^CODE_OF_CONDUCT\.md$ +.xlsx$ diff --git a/DESCRIPTION b/DESCRIPTION index 61ad147..71f8790 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,7 +30,8 @@ Imports: tibble, glue, ghql, - openxlsx + openxlsx, + tidyr Suggests: gargle, gitcreds, diff --git a/NAMESPACE b/NAMESPACE index 9df0d20..615d472 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -26,6 +26,7 @@ importFrom(ghql,GraphqlClient) importFrom(glue,glue) importFrom(glue,glue_collapse) importFrom(googleAnalyticsR,ga_account_list) +importFrom(googleAnalyticsR,ga_auth) importFrom(httr2,req_auth_bearer_token) importFrom(httr2,req_body_json) importFrom(httr2,req_error) @@ -60,3 +61,4 @@ importFrom(stringr,regex) importFrom(stringr,str_detect) importFrom(stringr,str_remove) importFrom(tibble,tibble) +importFrom(tidyr,separate) diff --git a/R/export.R b/R/export.R index a54e5dd..8da207e 100644 --- a/R/export.R +++ b/R/export.R @@ -1,35 +1,12 @@ -export_project <- function( - core_project_numbers, export = c("csv", "json", "excel"), - token = gitcreds::gitcreds_get()$password, - service_account_json = 'cfde-access-keyfile.json', file) { - - export <- match.arg(export) - - if(export == "csv") { - export_csv(core_project_numbers) - } - - if(export == "json") { - export_json(core_project_numbers) - } - - if(export == "excel") { - export_excel(core_project_numbers) - } - -} - -export_csv <- function(core_project_numbers) { - -} - -export_js <- function(core_project_numbers) { - -} - #' Export to Excel #' +#' @param core_project_numbers A character vector of NIH Core Project Numbers +#' @param token The token required for authentication with the GitHub API +#' @param service_account_json A character string containing the path to a JSON file containing a Google service account +#' @param file A character string containing the path to the Excel file to be written +#' #' @importFrom openxlsx createWorkbook addWorksheet writeData saveWorkbook +#' @importFrom rlang .data #' @export #' #' @examples @@ -50,8 +27,8 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ ## Add Assosciated Publications addWorksheet(wb, "pub_info") pmids <- proj_info |> - filter(found_publication) |> - pull(pmid) + filter(.data$found_publication) |> + pull('pmid') pub_info <- icite(pmids) writeData(wb = wb, sheet = "pub_info", x = pub_info, na.string = "") diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index 56e205b..f5ea967 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -5,10 +5,14 @@ #' with the Core Project Numbers. #' #' @param core_project_numbers A character vector of NIH Core Project Numbers +#' @param service_account_json A character string containing the path to a JSON file containing the +#' Google service account credentials. Defaults to "cfde-access-keyfile.json" #' -#' @importFrom googleAnalyticsR ga_account_list +#' @importFrom googleAnalyticsR ga_account_list ga_auth #' @importFrom purrr map map_chr #' @importFrom stringr str_remove +#' @importFrom tidyr separate +#' @importFrom rlang .data #' #' @return A data frame containing the associated Google Analytics data #' @export @@ -34,9 +38,9 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce core_project_regex <- paste0(unique(tolower(core_project_numbers)), collapse = "|") account_list <- ga_account_list("ga4") |> mutate( - property_meta = suppressMessages(map(propertyId, get_ga_meta_by_id)), + property_meta = suppressMessages(map(.data$propertyId, get_ga_meta_by_id)), core_project_num = map_chr( - property_meta, + .data$property_meta, ~{ res <- .x |> filter(str_detect(apiName, regex(core_project_regex, ignore_case = TRUE))) |> @@ -54,8 +58,8 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce ) ) |> ## Filter to those with the requested Core Project Numbers - filter(!is.na(core_project_num)) |> - select(-property_meta) + filter(!is.na(.data$core_project_num)) |> + select(-'property_meta') if(nrow(account_list) == 0) { rlang::inform(rlang::format_error_bullets(c(i = "No Google Analytics properties found for the requested Core Project Numbers"))) } diff --git a/man/export_excel.Rd b/man/export_excel.Rd index 8a30e08..6b99f7c 100644 --- a/man/export_excel.Rd +++ b/man/export_excel.Rd @@ -11,6 +11,15 @@ export_excel( file ) } +\arguments{ +\item{core_project_numbers}{A character vector of NIH Core Project Numbers} + +\item{token}{The token required for authentication with the GitHub API} + +\item{service_account_json}{A character string containing the path to a JSON file containing a Google service account} + +\item{file}{A character string containing the path to the Excel file to be written} +} \description{ Export to Excel } diff --git a/man/get_ga_basic.Rd b/man/get_ga_basic.Rd index bd5747c..12c318f 100644 --- a/man/get_ga_basic.Rd +++ b/man/get_ga_basic.Rd @@ -11,6 +11,9 @@ get_ga_basic( } \arguments{ \item{core_project_numbers}{A character vector of NIH Core Project Numbers} + +\item{service_account_json}{A character string containing the path to a JSON file containing the +Google service account credentials. Defaults to "cfde-access-keyfile.json"} } \value{ A data frame containing the associated Google Analytics data From 847d5607750476846c4703fd4f76f7b81d73135e Mon Sep 17 00:00:00 2001 From: David Mayer Date: Wed, 3 Dec 2025 16:32:38 -0500 Subject: [PATCH 20/25] update auth progression --- R/get_ga_basic.R | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index f5ea967..2116127 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -18,8 +18,10 @@ #' @export get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-access-keyfile.json') { ## This function requires authentication, check for existing creds - if(file.exists(system.file("secret", service_account_json, package = "programets"))){ - json_file <- gargle::secret_decrypt_json( + ## Package Credentials + if(file.exists(system.file("secret", service_account_json, package = "programets")) && + !is.null(Sys.getenv("CFDE_ENCRYPTION_KEY"))){ + programets_service_account <- gargle::secret_decrypt_json( path = system.file( "secret", service_account_json, @@ -28,8 +30,12 @@ get_ga_basic <- function(core_project_numbers, service_account_json = 'cfde-acce key = "CFDE_ENCRYPTION_KEY" ) googleAnalyticsR::ga_auth( - json_file = json_file + json_file = programets_service_account ) + ## User SA Credentials + } else if (file.exists(service_account_json)) { + ga_auth(json_file = service_account_json) + ## Interactive Auth } else { ga_auth() } From 0d5fd5bfa29b9e3bd5b796c988ae3f71d1c09542 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Thu, 4 Dec 2025 08:23:25 -0500 Subject: [PATCH 21/25] additional description about auth --- R/get_ga_basic.R | 3 ++- man/get_ga_basic.Rd | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/R/get_ga_basic.R b/R/get_ga_basic.R index 2116127..219d14a 100644 --- a/R/get_ga_basic.R +++ b/R/get_ga_basic.R @@ -6,7 +6,8 @@ #' #' @param core_project_numbers A character vector of NIH Core Project Numbers #' @param service_account_json A character string containing the path to a JSON file containing the -#' Google service account credentials. Defaults to "cfde-access-keyfile.json" +#' Google service account credentials. If no file is provided, interactive authentication is used. +#' Defaults to "cfde-access-keyfile.json" #' #' @importFrom googleAnalyticsR ga_account_list ga_auth #' @importFrom purrr map map_chr diff --git a/man/get_ga_basic.Rd b/man/get_ga_basic.Rd index 12c318f..13fb9e5 100644 --- a/man/get_ga_basic.Rd +++ b/man/get_ga_basic.Rd @@ -13,7 +13,8 @@ get_ga_basic( \item{core_project_numbers}{A character vector of NIH Core Project Numbers} \item{service_account_json}{A character string containing the path to a JSON file containing the -Google service account credentials. Defaults to "cfde-access-keyfile.json"} +Google service account credentials. If no file is provided, interactive authentication is used. +Defaults to "cfde-access-keyfile.json"} } \value{ A data frame containing the associated Google Analytics data From 010157c02fabe03d526a27d97dcbd75c366571fc Mon Sep 17 00:00:00 2001 From: David Mayer Date: Thu, 4 Dec 2025 08:35:22 -0500 Subject: [PATCH 22/25] add test for export excel --- tests/testthat/test-export_excel.R | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/testthat/test-export_excel.R diff --git a/tests/testthat/test-export_excel.R b/tests/testthat/test-export_excel.R new file mode 100644 index 0000000..ff4bef8 --- /dev/null +++ b/tests/testthat/test-export_excel.R @@ -0,0 +1,20 @@ +library(gitcreds) + +test_that("export_excel writes an Excel file", { + temp_file <- tempfile(fileext = ".xlsx") + on.exit(unlink(temp_file)) + core_project_numbers <- c("OT2OD030545") + token <- gitcreds_get()$password + export_excel(core_project_numbers = core_project_numbers, token = token, file = temp_file) + expect_true(file.exists(temp_file)) +}) + +test_that("export_excel throws an error if the file already exists", { + temp_file <- tempfile(fileext = ".xlsx") + on.exit(unlink(temp_file)) + core_project_numbers <- c("OT2OD030545") + token <- gitcreds_get()$password + file.create(temp_file) + expect_error(export_excel(core_project_numbers = core_project_numbers, token = token, file = temp_file), "File already exists") +}) + From 939022cd426315cb5cca58e90be493574d2e5c96 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Thu, 4 Dec 2025 09:25:34 -0500 Subject: [PATCH 23/25] update param, and test --- R/export.R | 6 +++--- man/export_excel.Rd | 4 ++-- tests/testthat/test-export_excel.R | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/R/export.R b/R/export.R index 8da207e..df38c31 100644 --- a/R/export.R +++ b/R/export.R @@ -3,7 +3,7 @@ #' @param core_project_numbers A character vector of NIH Core Project Numbers #' @param token The token required for authentication with the GitHub API #' @param service_account_json A character string containing the path to a JSON file containing a Google service account -#' @param file A character string containing the path to the Excel file to be written +#' @param dir A character string containing the path to directory where the Excel file will be written #' #' @importFrom openxlsx createWorkbook addWorksheet writeData saveWorkbook #' @importFrom rlang .data @@ -14,7 +14,7 @@ #' test_projects <-c("OT2OD030545") #' } #' -export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', file) { +export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', dir) { ## Create Excel Workbook wb <- createWorkbook() @@ -43,5 +43,5 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ writeData(wb = wb, sheet = "ga_info", x = ga_info, na.string = "") ## Save Workbook - saveWorkbook(wb, paste0("programets_", Sys.Date(), ".xlsx", sep = ""), file = file) + saveWorkbook(wb, file.path(dir, paste0("programets_", Sys.Date(), ".xlsx", sep = ""))) } \ No newline at end of file diff --git a/man/export_excel.Rd b/man/export_excel.Rd index 6b99f7c..30b1e47 100644 --- a/man/export_excel.Rd +++ b/man/export_excel.Rd @@ -8,7 +8,7 @@ export_excel( core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = "cfde-access-keyfile.json", - file + dir ) } \arguments{ @@ -18,7 +18,7 @@ export_excel( \item{service_account_json}{A character string containing the path to a JSON file containing a Google service account} -\item{file}{A character string containing the path to the Excel file to be written} +\item{dir}{A character string containing the path to directory where the Excel file will be written} } \description{ Export to Excel diff --git a/tests/testthat/test-export_excel.R b/tests/testthat/test-export_excel.R index ff4bef8..9e5aac7 100644 --- a/tests/testthat/test-export_excel.R +++ b/tests/testthat/test-export_excel.R @@ -1,20 +1,20 @@ library(gitcreds) test_that("export_excel writes an Excel file", { - temp_file <- tempfile(fileext = ".xlsx") + temp_file <- file.path(tempdir(), paste0("programets_", Sys.Date(), ".xlsx", sep = "")) on.exit(unlink(temp_file)) core_project_numbers <- c("OT2OD030545") token <- gitcreds_get()$password - export_excel(core_project_numbers = core_project_numbers, token = token, file = temp_file) + export_excel(core_project_numbers = core_project_numbers, token = token, dir = tempdir()) expect_true(file.exists(temp_file)) }) test_that("export_excel throws an error if the file already exists", { - temp_file <- tempfile(fileext = ".xlsx") + temp_file <- file.path(tempdir(), paste0("programets_", Sys.Date(), ".xlsx", sep = "")) on.exit(unlink(temp_file)) core_project_numbers <- c("OT2OD030545") token <- gitcreds_get()$password file.create(temp_file) - expect_error(export_excel(core_project_numbers = core_project_numbers, token = token, file = temp_file), "File already exists") + expect_error(export_excel(core_project_numbers = core_project_numbers, token = token, dir = tempdir()), "File already exists") }) From a95c835e8842a51a0e81e6391e73fe8c2e676336 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Thu, 4 Dec 2025 09:25:46 -0500 Subject: [PATCH 24/25] tidyselect fix --- R/nih_reporter.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/nih_reporter.R b/R/nih_reporter.R index 48b0312..5616d8e 100644 --- a/R/nih_reporter.R +++ b/R/nih_reporter.R @@ -215,8 +215,8 @@ get_core_project_info <- function(core_project_numbers) { proj_results_tbl |> full_join(all_results, by = c('core_project_num' = 'core_project_number', 'appl_id' = 'applid')) |> filter(.data$core_project_num != "") |> - relocate(.data$core_project_num, .before = .data$appl_id) |> - relocate(.data$found_publication, .after = .data$core_project_num) + relocate('core_project_num', .before = 'appl_id') |> + relocate('found_publication', .after = 'core_project_num') return(all_results_combined) } From cd5b128456d32067c208caae2d8d90e4c2a76023 Mon Sep 17 00:00:00 2001 From: David Mayer Date: Thu, 4 Dec 2025 10:07:57 -0500 Subject: [PATCH 25/25] add optional csv export for tabs, change function name to reflect --- DESCRIPTION | 3 ++- NAMESPACE | 3 ++- R/export.R | 18 ++++++++++++++++-- man/{export_excel.Rd => export_tabular.Rd} | 15 +++++++++------ ...st-export_excel.R => test-export_tabular.R} | 8 ++++---- 5 files changed, 33 insertions(+), 14 deletions(-) rename man/{export_excel.Rd => export_tabular.Rd} (78%) rename tests/testthat/{test-export_excel.R => test-export_tabular.R} (58%) diff --git a/DESCRIPTION b/DESCRIPTION index 521f1e0..b436e46 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,7 +32,8 @@ Imports: ghql, openxlsx, tidyr, - rvest + rvest, + readr Suggests: gargle, gitcreds, diff --git a/NAMESPACE b/NAMESPACE index 170a00b..e44315d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,7 +2,7 @@ export(cfde_opportunity_numbers) export(epmc_search) -export(export_excel) +export(export_tabular) export(ga_dataframe) export(ga_meta_simple) export(ga_query_explorer) @@ -52,6 +52,7 @@ importFrom(purrr,map_chr) importFrom(purrr,map_dbl) importFrom(purrr,map_dfr) importFrom(purrr,pmap) +importFrom(readr,write_csv) importFrom(rlang,"%||%") importFrom(rlang,.data) importFrom(rlang,abort) diff --git a/R/export.R b/R/export.R index df38c31..84fbb11 100644 --- a/R/export.R +++ b/R/export.R @@ -1,11 +1,13 @@ -#' Export to Excel +#' Export to Tabular #' #' @param core_project_numbers A character vector of NIH Core Project Numbers #' @param token The token required for authentication with the GitHub API #' @param service_account_json A character string containing the path to a JSON file containing a Google service account #' @param dir A character string containing the path to directory where the Excel file will be written +#' @param csv A logical indicating whether to write a CSV file #' #' @importFrom openxlsx createWorkbook addWorksheet writeData saveWorkbook +#' @importFrom readr write_csv #' @importFrom rlang .data #' @export #' @@ -14,7 +16,7 @@ #' test_projects <-c("OT2OD030545") #' } #' -export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', dir) { +export_tabular <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', dir, csv = FALSE) { ## Create Excel Workbook wb <- createWorkbook() @@ -23,6 +25,9 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ addWorksheet(wb, "project_info") proj_info <- get_core_project_info(core_project_numbers) writeData(wb = wb, sheet = "project_info", x = proj_info, na.string = "") + if (csv) { + write_csv(proj_info, file.path(dir, paste0("programets_proj_info_", Sys.Date(), ".csv", sep = ""))) + } ## Add Assosciated Publications addWorksheet(wb, "pub_info") @@ -31,16 +36,25 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$ pull('pmid') pub_info <- icite(pmids) writeData(wb = wb, sheet = "pub_info", x = pub_info, na.string = "") + if (csv) { + write_csv(pub_info, file.path(dir, paste0("programets_pub_info_", Sys.Date(), ".csv", sep = ""))) + } ## Add GitHub addWorksheet(wb, "github_info") github_info <- get_github_by_topic_graphql(core_project_numbers, token = token) writeData(wb = wb, sheet = "github_info", x = github_info, na.string = "") + if (csv) { + write_csv(github_info, file.path(dir, paste0("programets_github_info_", Sys.Date(), ".csv", sep = ""))) + } ## Add Google Analytics addWorksheet(wb, "ga_info") ga_info <- get_ga_basic(core_project_numbers = core_project_numbers, service_account_json = service_account_json) writeData(wb = wb, sheet = "ga_info", x = ga_info, na.string = "") + if (csv) { + write_csv(ga_info, file.path(dir, paste0("programets_ga_info_", Sys.Date(), ".csv", sep = ""))) + } ## Save Workbook saveWorkbook(wb, file.path(dir, paste0("programets_", Sys.Date(), ".xlsx", sep = ""))) diff --git a/man/export_excel.Rd b/man/export_tabular.Rd similarity index 78% rename from man/export_excel.Rd rename to man/export_tabular.Rd index 30b1e47..095f012 100644 --- a/man/export_excel.Rd +++ b/man/export_tabular.Rd @@ -1,14 +1,15 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/export.R -\name{export_excel} -\alias{export_excel} -\title{Export to Excel} +\name{export_tabular} +\alias{export_tabular} +\title{Export to Tabular} \usage{ -export_excel( +export_tabular( core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = "cfde-access-keyfile.json", - dir + dir, + csv = FALSE ) } \arguments{ @@ -19,9 +20,11 @@ export_excel( \item{service_account_json}{A character string containing the path to a JSON file containing a Google service account} \item{dir}{A character string containing the path to directory where the Excel file will be written} + +\item{csv}{A logical indicating whether to write a CSV file} } \description{ -Export to Excel +Export to Tabular } \examples{ \dontrun{ diff --git a/tests/testthat/test-export_excel.R b/tests/testthat/test-export_tabular.R similarity index 58% rename from tests/testthat/test-export_excel.R rename to tests/testthat/test-export_tabular.R index 9e5aac7..e9a8c8b 100644 --- a/tests/testthat/test-export_excel.R +++ b/tests/testthat/test-export_tabular.R @@ -1,20 +1,20 @@ library(gitcreds) -test_that("export_excel writes an Excel file", { +test_that("export_tabular writes an Excel file", { temp_file <- file.path(tempdir(), paste0("programets_", Sys.Date(), ".xlsx", sep = "")) on.exit(unlink(temp_file)) core_project_numbers <- c("OT2OD030545") token <- gitcreds_get()$password - export_excel(core_project_numbers = core_project_numbers, token = token, dir = tempdir()) + export_tabular(core_project_numbers = core_project_numbers, token = token, dir = tempdir()) expect_true(file.exists(temp_file)) }) -test_that("export_excel throws an error if the file already exists", { +test_that("export_tabular throws an error if the file already exists", { temp_file <- file.path(tempdir(), paste0("programets_", Sys.Date(), ".xlsx", sep = "")) on.exit(unlink(temp_file)) core_project_numbers <- c("OT2OD030545") token <- gitcreds_get()$password file.create(temp_file) - expect_error(export_excel(core_project_numbers = core_project_numbers, token = token, dir = tempdir()), "File already exists") + expect_error(export_tabular(core_project_numbers = core_project_numbers, token = token, dir = tempdir()), "File already exists") })