Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple reporter updates #32

Merged
merged 9 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions R/AddCardModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -93,6 +94,7 @@ add_card_button_srv <- function(id, reporter, card_fun) {
}
checkmate::assert_class(card, "ReportCard")
reporter$append_cards(list(card))
shiny::showNotification(sprintf("The card added successfully."))
shiny::removeModal()
})
}
Expand Down
69 changes: 37 additions & 32 deletions R/DownloadModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,8 @@ download_report_button_srv <- function(id,
},
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",
class = "btn btn-danger",
`data-dismiss` = "modal",
`data-bs-dismiss` = "modal",
NULL,
Expand All @@ -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 = "")
Expand Down Expand Up @@ -179,10 +151,43 @@ 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)
Expand Down
69 changes: 69 additions & 0 deletions R/ResetModule.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' Reset Button Reporter User Interface
#' @description button for resetting 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? (This will remove ALL previously added cards)."
)),
footer = shiny::tagList(
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")
)
)
)
})

shiny::observeEvent(input$reset_reporter_ok, {
reporter$reset()
shiny::removeModal()
})
}
)
}
9 changes: 6 additions & 3 deletions R/SimpleReporter.R
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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`
Expand All @@ -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)
}
)
}
21 changes: 21 additions & 0 deletions man/reset_report_button_srv.Rd

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

19 changes: 19 additions & 0 deletions man/reset_report_button_ui.Rd

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

5 changes: 3 additions & 2 deletions man/simple_reporter_srv.Rd

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

2 changes: 1 addition & 1 deletion man/simple_reporter_ui.Rd

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

13 changes: 0 additions & 13 deletions tests/testthat/test-DownloadReportModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-ResetModule.R
Original file line number Diff line number Diff line change
@@ -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())
}
)
})
29 changes: 0 additions & 29 deletions tests/testthat/test-SimpleReporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
Loading