Skip to content

Commit db15f1a

Browse files
committed
factor out spreadsheet generation
prepares #329
1 parent f8cd888 commit db15f1a

File tree

14 files changed

+179
-110
lines changed

14 files changed

+179
-110
lines changed

NAMESPACE

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

33
export("%>%")
44
export(accumulate_pred_trans)
5+
export(add_attachment_mc)
56
export(assert_metacheckable)
67
export(auth_cr)
78
export(auth_mailjet)
@@ -11,6 +12,8 @@ export(cr_compliance_overview)
1112
export(cr_funder_df)
1213
export(cr_has_orcid)
1314
export(cr_tdm_df)
15+
export(create_and_attach_ss)
16+
export(create_ss)
1417
export(doi_examples)
1518
export(draft_report)
1619
export(emailReport)
@@ -38,7 +41,6 @@ export(mc_long_docs)
3841
export(mc_long_docs_string)
3942
export(mc_render_email)
4043
export(mc_translator)
41-
export(md_data_attachment)
4244
export(metrics_overview)
4345
export(pretests)
4446
export(render_report)
@@ -48,6 +50,7 @@ export(smtp_send_mc)
4850
export(tabulate_metacheckable)
4951
export(tdm_metrics)
5052
export(vor_issue)
53+
export(write_xlsx_mc)
5154
import(dplyr)
5255
import(purrr)
5356
import(tidyr)

R/email.R

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ mc_compose_email <- function(dois = doi_examples$good[1:10],
1212
...) {
1313
mc_body_block(dois = dois, translator = translator, ...) %>%
1414
mc_compose_email_outer(translator = translator) %>%
15-
blastula::add_attachment(
16-
md_data_attachment(dois = dois),
17-
filename = translator$translate("mc_individual_results.xlsx")
18-
)
15+
create_and_attach_ss(email = ., dois = dois, translator = translator)
1916
}
2017

2118
mc_body_block <- function(dois, translator = mc_translator(), ...) {
@@ -431,43 +428,3 @@ email_async <- function(to, translator = mc_translator(), ...) {
431428
# both are needed upstream
432429
list(done = promise_done, id_notifi = id_notifi_done)
433430
}
434-
435-
# excel attachment ====
436-
437-
#' Make Spreadsheet attachment
438-
#' Creates an excel spreadsheet with individual-level results.
439-
#'
440-
#' @details `r metacheck::mc_long_docs_string("spreadsheet.md")`
441-
#'
442-
#' @param dois character, *all* submitted dois
443-
#' @param df compliance data from [cr_compliance_overview()]
444-
#' @inheritParams writexl::write_xlsx
445-
#'
446-
#' @return path to the created file
447-
#'
448-
#' @export
449-
#' @family communicate
450-
md_data_attachment <- function(dois,
451-
df = cr_compliance_overview(get_cr_md(
452-
dois[is_metacheckable(dois)]
453-
)),
454-
path = fs::file_temp(ext = "xlsx")) {
455-
is_compliance_overview_list(df)
456-
df[["pretest"]] <- tibble::tibble(
457-
# writexl does not know vctrs records
458-
doi = as.character(biblids::as_doi(dois)),
459-
tabulate_metacheckable(dois)
460-
)
461-
writexl::write_xlsx(
462-
x = df,
463-
path = path
464-
)
465-
}
466-
467-
#' Data is available
468-
#' @noRd
469-
is_compliance_overview_list <- function(x) {
470-
assertthat::assert_that(x %has_name% c("cr_overview", "cc_license_check"),
471-
msg = "No Compliance Data to attach, compliance data from [cr_compliance_overview()]"
472-
)
473-
}

R/spreadsheet.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' Store individual results in a spreadsheet
2+
#' @details `r metacheck::mc_long_docs_string("spreadsheet.md")`
3+
#' @family communicate
4+
#' @name spreadsheet
5+
NULL
6+
7+
#' @describeIn spreadsheet Create individual results
8+
#' @return A list of tibbles
9+
#' @inheritParams report
10+
#' @export
11+
create_ss <- function(dois = doi_examples$good[1:10]) {
12+
df <- cr_compliance_overview(get_cr_md(dois[is_metacheckable(dois)]))
13+
df[["pretest"]] <- tibble::tibble(
14+
# writexl does not know vctrs records
15+
doi = as.character(biblids::as_doi(dois)),
16+
tabulate_metacheckable(dois)
17+
)
18+
df
19+
}
20+
21+
#' @describeIn spreadsheet Write out file
22+
#' @inheritParams writexl::write_xlsx
23+
#' @inheritDotParams writexl::write_xlsx
24+
#' @export
25+
write_xlsx_mc <- function(x, path = fs::file_temp(ext = "xlsx"), ...) {
26+
writexl::write_xlsx(
27+
x = x,
28+
path = path,
29+
...
30+
)
31+
}
32+
33+
#' @describeIn spreadsheet Attach file to email
34+
#' @inheritParams blastula::add_attachment
35+
#' @export
36+
add_attachment_mc <- function(email = blastula::prepare_test_message(),
37+
file,
38+
translator = mc_translator()) {
39+
blastula::add_attachment(
40+
email = email,
41+
file = file,
42+
filename = translator$translate("mc_individual_results.xlsx")
43+
)
44+
}
45+
46+
#' @describeIn spreadsheet Create and attach individual results to email
47+
#' @export
48+
create_and_attach_ss <- function(dois = doi_examples$good[1:10], ...) {
49+
ellipsis::check_dots_used()
50+
df <- create_ss(dois = dois)
51+
add_attachment_mc(file = write_xlsx_mc(df), ...)
52+
}

man/email.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/emailReport.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mcApp.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/mcControls.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/md_data_attachment.Rd

Lines changed: 0 additions & 47 deletions
This file was deleted.

man/report.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/runMetacheck.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/spreadsheet.Rd

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/spreadsheet.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# individual results are assembled
2+
3+
Code
4+
names(create_ss())
5+
Output
6+
[1] "cr_overview" "cc_license_check" "tdm" "funder_info"
7+
[5] "pretest"
8+

tests/testthat/test-email.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,3 @@ test_that("email can be send", {
3030
smtp_send_mc()
3131
})
3232
})
33-
34-
test_that("attachment spreadsheet can be written out", {
35-
checkmate::expect_file_exists(md_data_attachment(
36-
dois = c(doi_examples$weird, doi_examples$good)
37-
))
38-
})

tests/testthat/test-spreadsheet.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test_that("individual results are assembled", {
2+
expect_snapshot(names(create_ss()))
3+
})
4+
5+
test_that("spreadsheet is written to file", {
6+
checkmate::expect_file_exists(write_xlsx_mc(create_ss()))
7+
})
8+
9+
test_that("individual results can be created and attached", {
10+
expect_true(length(create_and_attach_ss()$attachments) == 1L)
11+
})

0 commit comments

Comments
 (0)