Skip to content

Commit

Permalink
factor out spreadsheet generation
Browse files Browse the repository at this point in the history
prepares #329
  • Loading branch information
maxheld83 committed Dec 8, 2021
1 parent f8cd888 commit db15f1a
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 110 deletions.
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export("%>%")
export(accumulate_pred_trans)
export(add_attachment_mc)
export(assert_metacheckable)
export(auth_cr)
export(auth_mailjet)
Expand All @@ -11,6 +12,8 @@ export(cr_compliance_overview)
export(cr_funder_df)
export(cr_has_orcid)
export(cr_tdm_df)
export(create_and_attach_ss)
export(create_ss)
export(doi_examples)
export(draft_report)
export(emailReport)
Expand Down Expand Up @@ -38,7 +41,6 @@ export(mc_long_docs)
export(mc_long_docs_string)
export(mc_render_email)
export(mc_translator)
export(md_data_attachment)
export(metrics_overview)
export(pretests)
export(render_report)
Expand All @@ -48,6 +50,7 @@ export(smtp_send_mc)
export(tabulate_metacheckable)
export(tdm_metrics)
export(vor_issue)
export(write_xlsx_mc)
import(dplyr)
import(purrr)
import(tidyr)
Expand Down
45 changes: 1 addition & 44 deletions R/email.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ mc_compose_email <- function(dois = doi_examples$good[1:10],
...) {
mc_body_block(dois = dois, translator = translator, ...) %>%
mc_compose_email_outer(translator = translator) %>%
blastula::add_attachment(
md_data_attachment(dois = dois),
filename = translator$translate("mc_individual_results.xlsx")
)
create_and_attach_ss(email = ., dois = dois, translator = translator)
}

mc_body_block <- function(dois, translator = mc_translator(), ...) {
Expand Down Expand Up @@ -431,43 +428,3 @@ email_async <- function(to, translator = mc_translator(), ...) {
# both are needed upstream
list(done = promise_done, id_notifi = id_notifi_done)
}

# excel attachment ====

#' Make Spreadsheet attachment
#' Creates an excel spreadsheet with individual-level results.
#'
#' @details `r metacheck::mc_long_docs_string("spreadsheet.md")`
#'
#' @param dois character, *all* submitted dois
#' @param df compliance data from [cr_compliance_overview()]
#' @inheritParams writexl::write_xlsx
#'
#' @return path to the created file
#'
#' @export
#' @family communicate
md_data_attachment <- function(dois,
df = cr_compliance_overview(get_cr_md(
dois[is_metacheckable(dois)]
)),
path = fs::file_temp(ext = "xlsx")) {
is_compliance_overview_list(df)
df[["pretest"]] <- tibble::tibble(
# writexl does not know vctrs records
doi = as.character(biblids::as_doi(dois)),
tabulate_metacheckable(dois)
)
writexl::write_xlsx(
x = df,
path = path
)
}

#' Data is available
#' @noRd
is_compliance_overview_list <- function(x) {
assertthat::assert_that(x %has_name% c("cr_overview", "cc_license_check"),
msg = "No Compliance Data to attach, compliance data from [cr_compliance_overview()]"
)
}
52 changes: 52 additions & 0 deletions R/spreadsheet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' Store individual results in a spreadsheet
#' @details `r metacheck::mc_long_docs_string("spreadsheet.md")`
#' @family communicate
#' @name spreadsheet
NULL

#' @describeIn spreadsheet Create individual results
#' @return A list of tibbles
#' @inheritParams report
#' @export
create_ss <- function(dois = doi_examples$good[1:10]) {
df <- cr_compliance_overview(get_cr_md(dois[is_metacheckable(dois)]))
df[["pretest"]] <- tibble::tibble(
# writexl does not know vctrs records
doi = as.character(biblids::as_doi(dois)),
tabulate_metacheckable(dois)
)
df
}

#' @describeIn spreadsheet Write out file
#' @inheritParams writexl::write_xlsx
#' @inheritDotParams writexl::write_xlsx
#' @export
write_xlsx_mc <- function(x, path = fs::file_temp(ext = "xlsx"), ...) {
writexl::write_xlsx(
x = x,
path = path,
...
)
}

#' @describeIn spreadsheet Attach file to email
#' @inheritParams blastula::add_attachment
#' @export
add_attachment_mc <- function(email = blastula::prepare_test_message(),
file,
translator = mc_translator()) {
blastula::add_attachment(
email = email,
file = file,
filename = translator$translate("mc_individual_results.xlsx")
)
}

#' @describeIn spreadsheet Create and attach individual results to email
#' @export
create_and_attach_ss <- function(dois = doi_examples$good[1:10], ...) {
ellipsis::check_dots_used()
df <- create_ss(dois = dois)
add_attachment_mc(file = write_xlsx_mc(df), ...)
}
4 changes: 2 additions & 2 deletions man/email.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/emailReport.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/mcApp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/mcControls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 0 additions & 47 deletions man/md_data_attachment.Rd

This file was deleted.

4 changes: 2 additions & 2 deletions man/report.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/runMetacheck.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions man/spreadsheet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/spreadsheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# individual results are assembled

Code
names(create_ss())
Output
[1] "cr_overview" "cc_license_check" "tdm" "funder_info"
[5] "pretest"

6 changes: 0 additions & 6 deletions tests/testthat/test-email.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,3 @@ test_that("email can be send", {
smtp_send_mc()
})
})

test_that("attachment spreadsheet can be written out", {
checkmate::expect_file_exists(md_data_attachment(
dois = c(doi_examples$weird, doi_examples$good)
))
})
11 changes: 11 additions & 0 deletions tests/testthat/test-spreadsheet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("individual results are assembled", {
expect_snapshot(names(create_ss()))
})

test_that("spreadsheet is written to file", {
checkmate::expect_file_exists(write_xlsx_mc(create_ss()))
})

test_that("individual results can be created and attached", {
expect_true(length(create_and_attach_ss()$attachments) == 1L)
})

0 comments on commit db15f1a

Please sign in to comment.