From 16f34b3831ce921a360c7c306a067c28fcce7e9a Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal Date: Fri, 6 May 2022 13:38:01 +0200 Subject: [PATCH 1/8] resetModule --- NAMESPACE | 2 + R/DownloadModule.R | 28 ---------- R/ResetModule.R | 60 ++++++++++++++++++++++ R/SimpleReporter.R | 9 ++-- man/reset_report_button_srv.Rd | 21 ++++++++ man/reset_report_button_ui.Rd | 19 +++++++ man/simple_reporter_srv.Rd | 5 +- man/simple_reporter_ui.Rd | 2 +- tests/testthat/test-DownloadReportModule.R | 13 ----- tests/testthat/test-ResetModule.R | 26 ++++++++++ tests/testthat/test-SimpleReporter.R | 29 ----------- 11 files changed, 138 insertions(+), 76 deletions(-) create mode 100644 R/ResetModule.R create mode 100644 man/reset_report_button_srv.Rd create mode 100644 man/reset_report_button_ui.Rd create mode 100644 tests/testthat/test-ResetModule.R diff --git a/NAMESPACE b/NAMESPACE index a77bef6b..917af506 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,8 @@ export(add_card_button_ui) export(as_yaml_auto) export(download_report_button_srv) export(download_report_button_ui) +export(reset_report_button_srv) +export(reset_report_button_ui) export(rmd_output_arguments) export(rmd_outputs) export(simple_reporter_srv) diff --git a/R/DownloadModule.R b/R/DownloadModule.R index 458a60f4..6d70e576 100644 --- a/R/DownloadModule.R +++ b/R/DownloadModule.R @@ -100,15 +100,6 @@ download_report_button_srv <- function(id, shiny::tags$div(shiny::tags$b("Invalid", style = "color: red;")) }, footer = shiny::tagList( - shiny::tags$button( - id = ns("reset_reporter"), - type = "button", - style = "float: left;", - class = "btn btn-danger action-button", - `data-val` = shiny::restoreInput(id = ns("reset_reporter"), default = NULL), - NULL, - "Reset Reporter" - ), shiny::tags$button( type = "button", class = "btn btn-primary", @@ -126,25 +117,6 @@ download_report_button_srv <- function(id, shiny::showModal(download_modal()) }) - shiny::observeEvent(input$reset_reporter, { - shiny::showModal( - shiny::modalDialog( - shiny::tags$h3("Reset the Report"), - shiny::tags$hr(), - shiny::tags$strong(shiny::tags$p("Are you sure you want to reset the report?")), - footer = shiny::tagList( - shiny::modalButton("Cancel"), - shiny::actionButton(ns("reset_reporter_ok"), "Reset") - ) - ) - ) - }) - - shiny::observeEvent(input$reset_reporter_ok, { - reporter$reset() - shiny::removeModal() - }) - output$download_data <- shiny::downloadHandler( filename = function() { paste("report_", format(Sys.time(), "%y%m%d%H%M%S"), ".zip", sep = "") diff --git a/R/ResetModule.R b/R/ResetModule.R new file mode 100644 index 00000000..1728ab4b --- /dev/null +++ b/R/ResetModule.R @@ -0,0 +1,60 @@ +#' Reset Button Reporter User Interface +#' @description button for reseting the report content. +#' +#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. +#' @param id `character` +#' @return `shiny::tagList` +#' @export +reset_report_button_ui <- function(id) { + ns <- shiny::NS(id) + shiny::tagList( + shiny::tags$button( + id = ns("reset_reporter"), + type = "button", + class = "btn btn-warning action-button", + `data-val` = shiny::restoreInput(id = ns("reset_reporter"), default = NULL), + NULL, + "Reset Reporter" + ) + ) +} + +#' Reset Button Server +#' @description server for resetting the Report content. +#' +#' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. +#' @param id `character` +#' @param reporter `Reporter` instance. +#' @return `shiny::moduleServer` +#' @export +reset_report_button_srv <- function(id, reporter) { + checkmate::assert_class(reporter, "Reporter") + + shiny::moduleServer( + id, + function(input, output, session) { + ns <- session$ns + nr_cards <- length(reporter$get_cards()) + + + shiny::observeEvent(input$reset_reporter, { + shiny::showModal( + shiny::modalDialog( + shiny::tags$h3("Reset the Report"), + shiny::tags$hr(), + shiny::tags$strong(shiny::tags$p("Are you sure you want to reset the report?")), + footer = shiny::tagList( + shiny::modalButton("Cancel"), + shiny::actionButton(ns("reset_reporter_ok"), "Reset") + ) + ) + ) + }) + + shiny::observeEvent(input$reset_reporter_ok, { + reporter$reset() + shiny::removeModal() + }) + } + ) +} diff --git a/R/SimpleReporter.R b/R/SimpleReporter.R index 824a0f3f..388084bf 100644 --- a/R/SimpleReporter.R +++ b/R/SimpleReporter.R @@ -1,5 +1,5 @@ #' Simple Reporter User Interface -#' @description two buttons for adding views and downloading the Report. +#' @description three buttons for adding views, downloading and resetting the Report. #' #' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. #' @param id `character` @@ -10,12 +10,14 @@ simple_reporter_ui <- function(id) { shiny::tagList( add_card_button_ui(ns("add_report_card_simple")), download_report_button_ui(ns("download_button_simple")), + reset_report_button_ui(ns("reset_button_simple")) ) } #' Simple Reporter Server -#' @description two buttons for adding views and downloading the Report. -#' The add module has `add_report_card_simple` id and download module the `download_button_simple` id. +#' @description three buttons for adding views, downloading and resetting the Report. +#' The add module has `add_report_card_simple` id, the download module the `download_button_simple` id +#' and the reset module the `reset_button_simple` id. #' #' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. #' @param id `character` @@ -31,6 +33,7 @@ simple_reporter_srv <- function(id, reporter, card_fun, notification = TRUE) { function(input, output, session) { add_card_button_srv("add_report_card_simple", reporter = reporter, card_fun = card_fun) download_report_button_srv("download_button_simple", reporter = reporter, notification = notification) + reset_report_button_srv("reset_button_simple", reporter = reporter) } ) } diff --git a/man/reset_report_button_srv.Rd b/man/reset_report_button_srv.Rd new file mode 100644 index 00000000..17106f51 --- /dev/null +++ b/man/reset_report_button_srv.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ResetModule.R +\name{reset_report_button_srv} +\alias{reset_report_button_srv} +\title{Reset Button Server} +\usage{ +reset_report_button_srv(id, reporter) +} +\arguments{ +\item{id}{\code{character}} + +\item{reporter}{\code{Reporter} instance.} +} +\value{ +\code{shiny::moduleServer} +} +\description{ +server for resetting the Report content. + +For more details see the vignette: \code{vignette("simpleReporter", "teal.reporter")}. +} diff --git a/man/reset_report_button_ui.Rd b/man/reset_report_button_ui.Rd new file mode 100644 index 00000000..6d7d38c6 --- /dev/null +++ b/man/reset_report_button_ui.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ResetModule.R +\name{reset_report_button_ui} +\alias{reset_report_button_ui} +\title{Reset Button Reporter User Interface} +\usage{ +reset_report_button_ui(id) +} +\arguments{ +\item{id}{\code{character}} +} +\value{ +\code{shiny::tagList} +} +\description{ +button for reseting the report content. + +For more details see the vignette: \code{vignette("simpleReporter", "teal.reporter")}. +} diff --git a/man/simple_reporter_srv.Rd b/man/simple_reporter_srv.Rd index 4bf73e97..f9ba7174 100644 --- a/man/simple_reporter_srv.Rd +++ b/man/simple_reporter_srv.Rd @@ -20,8 +20,9 @@ the function have at\code{card}argument and optional \code{comment}.} \code{shiny::moduleServer} } \description{ -two buttons for adding views and downloading the Report. -The add module has \code{add_report_card_simple} id and download module the \code{download_button_simple} id. +three buttons for adding views, downloading and resetting the Report. +The add module has \code{add_report_card_simple} id, the download module the \code{download_button_simple} id +and the reset module the \code{reset_button_simple} id. For more details see the vignette: \code{vignette("simpleReporter", "teal.reporter")}. } diff --git a/man/simple_reporter_ui.Rd b/man/simple_reporter_ui.Rd index 0f268fe1..90c8e9d6 100644 --- a/man/simple_reporter_ui.Rd +++ b/man/simple_reporter_ui.Rd @@ -13,7 +13,7 @@ simple_reporter_ui(id) \code{shiny::tagList} } \description{ -two buttons for adding views and downloading the Report. +three buttons for adding views, downloading and resetting the Report. For more details see the vignette: \code{vignette("simpleReporter", "teal.reporter")}. } diff --git a/tests/testthat/test-DownloadReportModule.R b/tests/testthat/test-DownloadReportModule.R index 25c7c625..76fddd41 100644 --- a/tests/testthat/test-DownloadReportModule.R +++ b/tests/testthat/test-DownloadReportModule.R @@ -44,19 +44,6 @@ card1$append_plot( reporter <- Reporter$new() reporter$append_cards(list(card1)) -testthat::test_that("download_report_button_srv - reset a report", { - shiny::testServer( - download_report_button_srv, - args = list(reporter = reporter, notification = FALSE), - expr = { - testthat::expect_identical(reporter$get_cards(), list(card1)) - session$setInputs(`reset_reporter` = 0) - session$setInputs(`reset_reporter_ok` = 0) - testthat::expect_identical(reporter$get_blocks(), list()) - } - ) -}) - testthat::test_that("download_report_button_ui - returns a tagList", { testthat::expect_true( inherits(download_report_button_ui("sth"), c("shiny.tag.list", "list")) diff --git a/tests/testthat/test-ResetModule.R b/tests/testthat/test-ResetModule.R new file mode 100644 index 00000000..0d7ed9f1 --- /dev/null +++ b/tests/testthat/test-ResetModule.R @@ -0,0 +1,26 @@ +card_fun <- function(card = ReportCard$new(), comment = NULL) { + card$append_text("Header 2 text", "header2") + card$append_text("A paragraph of default text", "header2") + card$append_plot( + ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + + ggplot2::geom_histogram() + ) + card +} + +card1 <- card_fun() +reporter <- Reporter$new() +reporter$append_cards(list(card1)) + +testthat::test_that("simple_reporter_srv - reset a reporter", { + shiny::testServer( + simple_reporter_srv, + args = list(reporter = reporter, notification = FALSE, card_fun = card_fun), + expr = { + testthat::expect_identical(reporter$get_cards(), list(card1)) + session$setInputs(`reset_button_simple-reset_reporter` = 0) + session$setInputs(`reset_button_simple-reset_reporter_ok` = 0) + testthat::expect_identical(reporter$get_blocks(), list()) + } + ) +}) diff --git a/tests/testthat/test-SimpleReporter.R b/tests/testthat/test-SimpleReporter.R index a1485c7f..525f7dd8 100644 --- a/tests/testthat/test-SimpleReporter.R +++ b/tests/testthat/test-SimpleReporter.R @@ -37,35 +37,6 @@ testthat::test_that("simple_reporter_srv - render and downlaod a document", { ) }) -card_fun <- function(card = ReportCard$new(), comment = NULL) { - card$append_text("Header 2 text", "header2") - card$append_text("A paragraph of default text", "header2") - card$append_plot( - ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + - ggplot2::geom_histogram() - ) - card -} - -card1 <- card_fun() - -reporter <- Reporter$new() -reporter$append_cards(list(card1)) - -testthat::test_that("simple_reporter_srv - reset a reporter", { - shiny::testServer( - simple_reporter_srv, - args = list(reporter = reporter, notification = FALSE, card_fun = card_fun), - expr = { - testthat::expect_identical(reporter$get_cards(), list(card1)) - session$setInputs(`download_button_simple-reset_reporter` = 0) - session$setInputs(`download_button_simple-reset_reporter_ok` = 0) - testthat::expect_identical(reporter$get_blocks(), list()) - } - ) -}) - - reporter <- Reporter$new() testthat::test_that("simple_reporter_srv - add a Card to Reporter", { From 67b40773ad8d59124b5809834ef959def5d3d87a Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal Date: Fri, 6 May 2022 13:38:10 +0200 Subject: [PATCH 2/8] error notification --- R/DownloadModule.R | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/R/DownloadModule.R b/R/DownloadModule.R index 6d70e576..666502cf 100644 --- a/R/DownloadModule.R +++ b/R/DownloadModule.R @@ -151,10 +151,39 @@ report_render_and_compress <- function(reporter, input_list, file = tempdir()) { renderer <- Renderer$new() renderer$render(reporter$get_blocks(), yaml_header) - temp_zip_file <- tempfile(fileext = ".zip") - zip::zipr(temp_zip_file, renderer$get_output_dir()) - file.copy(temp_zip_file, file) + + tryCatch( + expr = zip::zipr(temp_zip_file, renderer$get_output_dir()), + warning = function(cond) + shiny::showNotification( + ui = sprintf("Zipping folder warning!"), + action = "Please contact app developer", + type = "warning" + ), + error = function(cond) + shiny::showNotification( + ui = sprintf("Zipping folder error!"), + action = "Please contact app developer", + type = "error" + ) + ) + + tryCatch( + expr = file.copy(temp_zip_file, file), + warning = function(cond) + shiny::showNotification( + ui = sprintf("Copying file warning!"), + action = "Please contact app developer", + type = "warning" + ), + error = function(cond) + shiny::showNotification( + ui = sprintf("Copying file error!"), + action = "Please contact app developer", + type = "error" + ) + ) rm(renderer) invisible(file) From 104536f984aee2d237d61e9c6443faec4b7523cf Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal Date: Fri, 6 May 2022 13:38:16 +0200 Subject: [PATCH 3/8] no comment --- vignettes/simpleReporter.Rmd | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vignettes/simpleReporter.Rmd b/vignettes/simpleReporter.Rmd index 24ab375b..f5f297d6 100644 --- a/vignettes/simpleReporter.Rmd +++ b/vignettes/simpleReporter.Rmd @@ -207,8 +207,10 @@ server <- function(input, output, session) { card$append_text("My Table", "header2") card$append_table(table()) } - card$append_text("Comment", "header3") - card$append_text(comment) + if (!is.null(comment)) { + card$append_text("Comment", "header3") + card$append_text(comment) + } card } From 7477e8f990d2018e5c93827eabd5ee0ddaa15fb7 Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal Date: Fri, 6 May 2022 13:48:45 +0200 Subject: [PATCH 4/8] spelling --- R/DownloadModule.R | 18 +++++++++++------- R/ResetModule.R | 2 +- man/reset_report_button_ui.Rd | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/R/DownloadModule.R b/R/DownloadModule.R index 666502cf..4432aa55 100644 --- a/R/DownloadModule.R +++ b/R/DownloadModule.R @@ -155,34 +155,38 @@ report_render_and_compress <- function(reporter, input_list, file = tempdir()) { tryCatch( expr = zip::zipr(temp_zip_file, renderer$get_output_dir()), - warning = function(cond) + warning = function(cond) { shiny::showNotification( ui = sprintf("Zipping folder warning!"), action = "Please contact app developer", type = "warning" - ), - error = function(cond) + ) + }, + error = function(cond) { shiny::showNotification( ui = sprintf("Zipping folder error!"), action = "Please contact app developer", type = "error" ) + } ) tryCatch( - expr = file.copy(temp_zip_file, file), - warning = function(cond) + expr = file.copy(temp_zip_file, file), + warning = function(cond) { shiny::showNotification( ui = sprintf("Copying file warning!"), action = "Please contact app developer", type = "warning" - ), - error = function(cond) + ) + }, + error = function(cond) { shiny::showNotification( ui = sprintf("Copying file error!"), action = "Please contact app developer", type = "error" ) + } ) rm(renderer) diff --git a/R/ResetModule.R b/R/ResetModule.R index 1728ab4b..cbd4828d 100644 --- a/R/ResetModule.R +++ b/R/ResetModule.R @@ -1,5 +1,5 @@ #' Reset Button Reporter User Interface -#' @description button for reseting the report content. +#' @description button for resetting the report content. #' #' For more details see the vignette: `vignette("simpleReporter", "teal.reporter")`. #' @param id `character` diff --git a/man/reset_report_button_ui.Rd b/man/reset_report_button_ui.Rd index 6d7d38c6..7afb7713 100644 --- a/man/reset_report_button_ui.Rd +++ b/man/reset_report_button_ui.Rd @@ -13,7 +13,7 @@ reset_report_button_ui(id) \code{shiny::tagList} } \description{ -button for reseting the report content. +button for resetting the report content. For more details see the vignette: \code{vignette("simpleReporter", "teal.reporter")}. } From 27afde5c28b4a0b911b4824968201fbad874ff87 Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal Date: Fri, 6 May 2022 14:00:27 +0200 Subject: [PATCH 5/8] add card notification --- R/AddCardModule.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/AddCardModule.R b/R/AddCardModule.R index d1452020..bf2295e1 100644 --- a/R/AddCardModule.R +++ b/R/AddCardModule.R @@ -93,6 +93,7 @@ add_card_button_srv <- function(id, reporter, card_fun) { } checkmate::assert_class(card, "ReportCard") reporter$append_cards(list(card)) + shiny::showNotification(sprintf("Card added successfully.")) shiny::removeModal() }) } From d4efd9f3979df3dadb221f16cedc17d39b0e690e Mon Sep 17 00:00:00 2001 From: kpagacz Date: Mon, 9 May 2022 10:41:39 +0200 Subject: [PATCH 6/8] * changed the inputText comment area in add_card_button_srv to textAreaInput * replaced the default value of the said input with a placeholder value and changed its wording * changed the colors of cancel button in SimpleReporter to red --- R/AddCardModule.R | 11 ++++++----- R/DownloadModule.R | 2 +- R/ResetModule.R | 11 +++++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/R/AddCardModule.R b/R/AddCardModule.R index bf2295e1..d9311d0b 100644 --- a/R/AddCardModule.R +++ b/R/AddCardModule.R @@ -41,12 +41,13 @@ add_card_button_srv <- function(id, reporter, card_fun) { add_modal <- function(failed = FALSE) { shiny::modalDialog( easyClose = TRUE, - shiny::tags$h3("Add the Card to the Report"), + shiny::tags$h3("Add a card to the Report"), shiny::tags$hr(), - shiny::textInput( + shiny::textAreaInput( ns("comment"), "Comment", - value = "The idea behind", + value = "", + placeholder = "Add a comment here...", width = "100%" ), if (failed) { @@ -57,7 +58,7 @@ add_card_button_srv <- function(id, reporter, card_fun) { footer = shiny::tagList( shiny::tags$button( type = "button", - class = "btn btn-primary", + class = "btn btn-danger", `data-dismiss` = "modal", `data-bs-dismiss` = "modal", NULL, @@ -93,7 +94,7 @@ add_card_button_srv <- function(id, reporter, card_fun) { } checkmate::assert_class(card, "ReportCard") reporter$append_cards(list(card)) - shiny::showNotification(sprintf("Card added successfully.")) + shiny::showNotification(sprintf("The card added successfully.")) shiny::removeModal() }) } diff --git a/R/DownloadModule.R b/R/DownloadModule.R index 4432aa55..7e407ce7 100644 --- a/R/DownloadModule.R +++ b/R/DownloadModule.R @@ -102,7 +102,7 @@ download_report_button_srv <- function(id, footer = shiny::tagList( shiny::tags$button( type = "button", - class = "btn btn-primary", + class = "btn btn-danger", `data-dismiss` = "modal", `data-bs-dismiss` = "modal", NULL, diff --git a/R/ResetModule.R b/R/ResetModule.R index cbd4828d..79d454d4 100644 --- a/R/ResetModule.R +++ b/R/ResetModule.R @@ -44,8 +44,15 @@ reset_report_button_srv <- function(id, reporter) { shiny::tags$hr(), shiny::tags$strong(shiny::tags$p("Are you sure you want to reset the report?")), footer = shiny::tagList( - shiny::modalButton("Cancel"), - shiny::actionButton(ns("reset_reporter_ok"), "Reset") + shiny::tags$button( + type = "button", + class = "btn btn-danger", + `data-dismiss` = "modal", + `data-bs-dismiss` = "modal", + NULL, + "Cancel" + ), + shiny::actionButton(ns("reset_reporter_ok"), "Reset", class = "btn-warning") ) ) ) From 1b4adaff5a90ecbe6aa4eabe657e713cdd3cd142 Mon Sep 17 00:00:00 2001 From: Mahmoud Hallal <86970066+mhallal1@users.noreply.github.com> Date: Mon, 9 May 2022 11:27:47 +0200 Subject: [PATCH 7/8] Update R/ResetModule.R Co-authored-by: Konrad Pagacz --- R/ResetModule.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/ResetModule.R b/R/ResetModule.R index 79d454d4..8eb5b9a4 100644 --- a/R/ResetModule.R +++ b/R/ResetModule.R @@ -42,7 +42,9 @@ reset_report_button_srv <- function(id, reporter) { shiny::modalDialog( shiny::tags$h3("Reset the Report"), shiny::tags$hr(), - shiny::tags$strong(shiny::tags$p("Are you sure you want to reset the report?")), + shiny::tags$strong(shiny::tags$p( + "Are you sure you want to reset the report? (This will remove ALL previously added cards)." + )), footer = shiny::tagList( shiny::tags$button( type = "button", From cd7606cac703858e2df63830eb14b2c64508fbec Mon Sep 17 00:00:00 2001 From: hallalm Date: Mon, 9 May 2022 11:39:23 +0200 Subject: [PATCH 8/8] update comment check --- vignettes/simpleReporter.Rmd | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/vignettes/simpleReporter.Rmd b/vignettes/simpleReporter.Rmd index f5f297d6..5945f471 100644 --- a/vignettes/simpleReporter.Rmd +++ b/vignettes/simpleReporter.Rmd @@ -116,8 +116,10 @@ server <- function(input, output, session) { card$append_text("My Table", "header2") card$append_table(table()) } - card$append_text("Comment", "header3") - card$append_text(comment) + if (!comment == "") { + card$append_text("Comment", "header3") + card$append_text(comment) + } card } @@ -207,7 +209,7 @@ server <- function(input, output, session) { card$append_text("My Table", "header2") card$append_table(table()) } - if (!is.null(comment)) { + if (!comment == "") { card$append_text("Comment", "header3") card$append_text(comment) }