Skip to content

Commit cd5b128

Browse files
committed
add optional csv export for tabs, change function name to reflect
1 parent c790023 commit cd5b128

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ Imports:
3232
ghql,
3333
openxlsx,
3434
tidyr,
35-
rvest
35+
rvest,
36+
readr
3637
Suggests:
3738
gargle,
3839
gitcreds,

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
export(cfde_opportunity_numbers)
44
export(epmc_search)
5-
export(export_excel)
5+
export(export_tabular)
66
export(ga_dataframe)
77
export(ga_meta_simple)
88
export(ga_query_explorer)
@@ -52,6 +52,7 @@ importFrom(purrr,map_chr)
5252
importFrom(purrr,map_dbl)
5353
importFrom(purrr,map_dfr)
5454
importFrom(purrr,pmap)
55+
importFrom(readr,write_csv)
5556
importFrom(rlang,"%||%")
5657
importFrom(rlang,.data)
5758
importFrom(rlang,abort)

R/export.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
#' Export to Excel
1+
#' Export to Tabular
22
#'
33
#' @param core_project_numbers A character vector of NIH Core Project Numbers
44
#' @param token The token required for authentication with the GitHub API
55
#' @param service_account_json A character string containing the path to a JSON file containing a Google service account
66
#' @param dir A character string containing the path to directory where the Excel file will be written
7+
#' @param csv A logical indicating whether to write a CSV file
78
#'
89
#' @importFrom openxlsx createWorkbook addWorksheet writeData saveWorkbook
10+
#' @importFrom readr write_csv
911
#' @importFrom rlang .data
1012
#' @export
1113
#'
@@ -14,7 +16,7 @@
1416
#' test_projects <-c("OT2OD030545")
1517
#' }
1618
#'
17-
export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', dir) {
19+
export_tabular <- function(core_project_numbers, token = gitcreds::gitcreds_get()$password, service_account_json = 'cfde-access-keyfile.json', dir, csv = FALSE) {
1820

1921
## Create Excel Workbook
2022
wb <- createWorkbook()
@@ -23,6 +25,9 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$
2325
addWorksheet(wb, "project_info")
2426
proj_info <- get_core_project_info(core_project_numbers)
2527
writeData(wb = wb, sheet = "project_info", x = proj_info, na.string = "")
28+
if (csv) {
29+
write_csv(proj_info, file.path(dir, paste0("programets_proj_info_", Sys.Date(), ".csv", sep = "")))
30+
}
2631

2732
## Add Assosciated Publications
2833
addWorksheet(wb, "pub_info")
@@ -31,16 +36,25 @@ export_excel <- function(core_project_numbers, token = gitcreds::gitcreds_get()$
3136
pull('pmid')
3237
pub_info <- icite(pmids)
3338
writeData(wb = wb, sheet = "pub_info", x = pub_info, na.string = "")
39+
if (csv) {
40+
write_csv(pub_info, file.path(dir, paste0("programets_pub_info_", Sys.Date(), ".csv", sep = "")))
41+
}
3442

3543
## Add GitHub
3644
addWorksheet(wb, "github_info")
3745
github_info <- get_github_by_topic_graphql(core_project_numbers, token = token)
3846
writeData(wb = wb, sheet = "github_info", x = github_info, na.string = "")
47+
if (csv) {
48+
write_csv(github_info, file.path(dir, paste0("programets_github_info_", Sys.Date(), ".csv", sep = "")))
49+
}
3950

4051
## Add Google Analytics
4152
addWorksheet(wb, "ga_info")
4253
ga_info <- get_ga_basic(core_project_numbers = core_project_numbers, service_account_json = service_account_json)
4354
writeData(wb = wb, sheet = "ga_info", x = ga_info, na.string = "")
55+
if (csv) {
56+
write_csv(ga_info, file.path(dir, paste0("programets_ga_info_", Sys.Date(), ".csv", sep = "")))
57+
}
4458

4559
## Save Workbook
4660
saveWorkbook(wb, file.path(dir, paste0("programets_", Sys.Date(), ".xlsx", sep = "")))
Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
library(gitcreds)
22

3-
test_that("export_excel writes an Excel file", {
3+
test_that("export_tabular writes an Excel file", {
44
temp_file <- file.path(tempdir(), paste0("programets_", Sys.Date(), ".xlsx", sep = ""))
55
on.exit(unlink(temp_file))
66
core_project_numbers <- c("OT2OD030545")
77
token <- gitcreds_get()$password
8-
export_excel(core_project_numbers = core_project_numbers, token = token, dir = tempdir())
8+
export_tabular(core_project_numbers = core_project_numbers, token = token, dir = tempdir())
99
expect_true(file.exists(temp_file))
1010
})
1111

12-
test_that("export_excel throws an error if the file already exists", {
12+
test_that("export_tabular throws an error if the file already exists", {
1313
temp_file <- file.path(tempdir(), paste0("programets_", Sys.Date(), ".xlsx", sep = ""))
1414
on.exit(unlink(temp_file))
1515
core_project_numbers <- c("OT2OD030545")
1616
token <- gitcreds_get()$password
1717
file.create(temp_file)
18-
expect_error(export_excel(core_project_numbers = core_project_numbers, token = token, dir = tempdir()), "File already exists")
18+
expect_error(export_tabular(core_project_numbers = core_project_numbers, token = token, dir = tempdir()), "File already exists")
1919
})
2020

0 commit comments

Comments
 (0)