Skip to content

Commit 3711b54

Browse files
committed
Export footer text
1 parent 84c0fe3 commit 3711b54

5 files changed

Lines changed: 65 additions & 20 deletions

File tree

R/export_helpers.R

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pdf_preprocessing <- function(df, ref) {
406406
#' @return Named list containing the data frames which are now ready for download.
407407
#'
408408
#' @keywords internal
409-
prep_export_data <- function(data_selection, current_data, data_selection_name, dataset_list) {
409+
prep_export_data <- function(data_selection, current_data, data_selection_name, dataset_list, footers) {
410410
# check validity of parameters
411411
checkmate::assert(
412412
checkmate::check_string(data_selection),
@@ -424,17 +424,31 @@ prep_export_data <- function(data_selection, current_data, data_selection_name,
424424
data_to_download <- dataset_list
425425
}
426426

427-
names(data_to_download) <- shorten_entries(
427+
shortened_names <- shorten_entries(
428428
paste0(names(data_to_download), " (", get_labels(data_to_download), ")"),
429429
as.integer(31) # name has to be shortened to 31 characters due to Excel's sheet name limit
430430
)
431431

432-
# convert types to character to avoid representation issues in Excel
433-
data_to_download <- lapply(data_to_download, function(df) {
434-
labels <- get_labels(df)
435-
data <- data.frame(sapply(df, as.character))
436-
data <- set_labels(data, labels)
432+
data_to_download <- local({
433+
res <- list()
434+
for (i_dataset in seq_along(data_to_download)){
435+
df <- data_to_download[[i_dataset]]
436+
437+
# convert types to character to avoid representation issues in Excel
438+
labels <- get_labels(df)
439+
data <- data.frame(sapply(df, as.character))
440+
data <- set_labels(data, labels)
441+
442+
# attach footer, if available
443+
dataset_name <- names(data_to_download)[[i_dataset]]
444+
attr(data, "footer") <- footers[[dataset_name]]
445+
446+
res[[length(res) + 1]] <- data
447+
}
448+
return(res)
437449
})
450+
451+
names(data_to_download) <- shortened_names
438452

439453
return(data_to_download)
440454
}
@@ -458,9 +472,16 @@ excel_export <- function(data_to_download, file, intended_use_label) {
458472
combine = "and"
459473
)
460474

461-
# Add column labels
475+
# Add column labels and footers
462476
data_to_download <- lapply(data_to_download, function(x) {
463477
names(x) <- paste0(names(x), " [", get_labels(x), "]")
478+
footer <- attr(x, "footer")
479+
if (!is.null(footer)) {
480+
first_new_row <- nrow(x) + 1
481+
last_new_row <- nrow(x) + length(footer)
482+
x[first_new_row:last_new_row, ] <- NA
483+
x[first_new_row:last_new_row, 1] <- footer
484+
}
464485
return(x)
465486
})
466487

@@ -522,6 +543,7 @@ pdf_export <- function(data_to_download, ref_cols, file, metadata, active_sessio
522543
trial_name = gsub("_", "\\\\_", metadata[1]), # Rmd does not allow underscore
523544
time_stamp = gsub("[^-/A-Za-z0-9!?.,:() ]", "?", metadata[2]), # replace characters that could cause problems
524545
snap_shot_name = gsub("[^-/A-Za-z0-9!?.,:() ]", "?", metadata[3]), # same here
546+
footer = attr(data_to_download[[1]], "footer"),
525547
df_list = res_preprocess,
526548
active_session = active_session
527549
),

R/mod_export_listings.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ mod_export_listings_server <- function(module_id,
9494
data,
9595
data_selection_name,
9696
current_rows,
97-
intended_use_label) {
97+
intended_use_label,
98+
footers) {
9899
# check validity of parameters
99100
checkmate::assert(
100101
checkmate::check_string(module_id, min.chars = 1),
@@ -266,7 +267,7 @@ mod_export_listings_server <- function(module_id,
266267
shiny::removeModal() # close pop up
267268

268269
data_to_download <- prep_export_data(
269-
input[[EXP$DATASEL_ID]], current_data(), data_selection_name(), v_dataset_list()
270+
input[[EXP$DATASEL_ID]], current_data(), data_selection_name(), v_dataset_list(), footers
270271
)
271272

272273
if (input[[EXP$FILETYPE_ID]] == ".xlsx") {

R/mod_listings.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ listings_server <- function(module_id,
408408
data = shiny::reactive(set_data(listings_data(), r_selected_columns_in_dataset()[[input[[TBL$DATASET_ID]]]])),
409409
data_selection_name = shiny::reactive(input[[TBL$DATASET_ID]]),
410410
current_rows = shiny::reactive(input[[paste0(TBL$TABLE_ID, "_rows_all")]]),
411-
intended_use_label = intended_use_label
411+
intended_use_label = intended_use_label,
412+
footers
412413
)
413414

414415
# Proxy reference to dataTable

inst/rmd/create_pdf_export.Rmd

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ params:
1010
snap_shot_name: NULL
1111
df_list: NULL
1212
active_session: NULL
13+
footer: NULL
1314
title: "`r params$set_title`"
1415
subtitle: "`r params$set_subtitle`"
1516
header-includes:
@@ -54,9 +55,21 @@ purrr::iwalk(params$df_list, ~ {
5455
5556
# Print table
5657
print(k)
57-
cat("\\clearpage")
58+
if (.y < num_pages) cat("\\clearpage") # no trailing new page
5859
})
5960
61+
html_to_text <- function(html_var) {
62+
txt <- gsub("<br\\s*/?>", "\n\n", html_var, ignore.case = TRUE) # Rmd double newlines
63+
txt <- gsub("<[^>]+>", "", txt) # strip all other HTML tags
64+
return(txt)
65+
}
66+
67+
if (length(params$footer)) {
68+
html_output <- sprintf("<p>%s</p>", paste(params$footer, collapse = "<br>"))
69+
text_output <- html_to_text(html_output)
70+
cat(text_output)
71+
}
72+
6073
if (params$active_session) {
6174
shiny::setProgress(
6275
message = "Finalizing document.",

tests/testthat/test-export_helpers.R

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,31 +442,36 @@ test_that("prep_export_data() throws an error when argument types mismatch", {
442442
.x,
443443
current_data_valid,
444444
data_selection_name_valid,
445-
dataset_list_valid
445+
dataset_list_valid,
446+
footers = NULL
446447
)))
447448
purrr::walk(current_data_invalid, ~ expect_error(prep_export_data(
448449
data_selection_valid,
449450
.x,
450451
data_selection_name_valid,
451-
dataset_list_valid
452+
dataset_list_valid,
453+
footers = NULL
452454
)))
453455
purrr::walk(data_selection_name_invalid, ~ expect_error(prep_export_data(
454456
data_selection_valid,
455457
current_data_valid,
456458
.x,
457-
dataset_list_valid
459+
dataset_list_valid,
460+
footers = NULL
458461
)))
459462
purrr::walk(dataset_list_invalid, ~ expect_error(prep_export_data(
460463
data_selection_valid,
461464
current_data_valid,
462465
data_selection_name_valid,
463-
.x
466+
.x,
467+
footers = NULL
464468
)))
465469
expect_error(prep_export_data(
466470
data_selection_valid,
467471
current_data_valid,
468472
data_selection_name_valid,
469-
dataset_list_valid
473+
dataset_list_valid,
474+
footers = NULL
470475
), NA) # expect no error
471476
})
472477

@@ -479,7 +484,8 @@ test_that("prep_export_data() performs the correct transformation in the single
479484
data_selection_name_valid <- names(dataset_list_valid)[1]
480485

481486
# result
482-
res <- prep_export_data(data_selection_valid, current_data_valid, data_selection_name_valid, dataset_list_valid)
487+
res <- prep_export_data(data_selection_valid, current_data_valid, data_selection_name_valid, dataset_list_valid,
488+
footers = NULL)
483489

484490
# expected
485491
exp <- list("data1 (My Label)" = set_labels(data.frame(col1 = c("1", "2"), col2 = c("3", "4"))))
@@ -508,7 +514,8 @@ test_that("prep_export_data() performs the correct transformation in the multipl
508514
data_selection_valid,
509515
current_data_valid,
510516
data_selection_name_valid,
511-
dataset_list_valid
517+
dataset_list_valid,
518+
footers = NULL
512519
)
513520

514521
# perform tests
@@ -530,7 +537,8 @@ test_that("prep_export_data() shortens dataset names if they exceed Excel's shee
530537

531538
# result
532539
res <- nchar(
533-
names(prep_export_data(data_selection_valid, current_data_valid, data_selection_name_valid, dataset_list_valid))
540+
names(prep_export_data(data_selection_valid, current_data_valid, data_selection_name_valid, dataset_list_valid,
541+
footers = NULL))
534542
)
535543

536544
# perform tests

0 commit comments

Comments
 (0)