diff --git a/R/AddCardModule.R b/R/AddCardModule.R index 5adf32c1..b5d86774 100644 --- a/R/AddCardModule.R +++ b/R/AddCardModule.R @@ -88,126 +88,131 @@ add_card_button_srv <- function(id, reporter, card_fun) { checkmate::assert_class(reporter, "Reporter") checkmate::assert_subset(names(formals(card_fun)), c("card", "comment", "label"), empty.ok = TRUE) - shiny::moduleServer( - id, - function(input, output, session) { - setBookmarkExclude(c("add_report_card_button", "add_card_ok")) - ns <- session$ns - add_modal <- function() { - shiny::modalDialog( - easyClose = TRUE, - shiny::tags$h3("Add a Card to the Report"), - shiny::tags$hr(), - shiny::textInput( - ns("label"), - "Card Name", - value = "", - placeholder = "Add the card title here", - width = "100%" - ), - shiny::textAreaInput( - ns("comment"), - "Comment", - value = "", - placeholder = "Add a comment here...", - width = "100%" - ), - shiny::tags$script( - shiny::HTML( - sprintf( - " + shiny::moduleServer(id, function(input, output, session) { + + shiny::setBookmarkExclude(c( + "add_report_card_button", "download_button", "reset_reporter", + "add_card_ok", "download_data", "reset_reporter_ok", + "label", "comment" + )) + + ns <- session$ns + + add_modal <- function() { + shiny::modalDialog( + easyClose = TRUE, + shiny::tags$h3("Add a Card to the Report"), + shiny::tags$hr(), + shiny::textInput( + ns("label"), + "Card Name", + value = "", + placeholder = "Add the card title here", + width = "100%" + ), + shiny::textAreaInput( + ns("comment"), + "Comment", + value = "", + placeholder = "Add a comment here...", + width = "100%" + ), + shiny::tags$script( + shiny::HTML( + sprintf( + " $('#shiny-modal').on('shown.bs.modal', () => { $('#%s').focus() }) ", - ns("label") - ) + ns("label") ) + ) + ), + footer = shiny::div( + shiny::tags$button( + type = "button", + class = "btn btn-secondary", + `data-dismiss` = "modal", + `data-bs-dismiss` = "modal", + NULL, + "Cancel" ), - footer = shiny::div( - shiny::tags$button( - type = "button", - class = "btn btn-secondary", - `data-dismiss` = "modal", - `data-bs-dismiss` = "modal", - NULL, - "Cancel" - ), - shiny::tags$button( - id = ns("add_card_ok"), - type = "button", - class = "btn btn-primary action-button", - `data-val` = shiny::restoreInput(id = ns("add_card_ok"), default = NULL), - NULL, - "Add Card" - ) + shiny::tags$button( + id = ns("add_card_ok"), + type = "button", + class = "btn btn-primary action-button", + `data-val` = shiny::restoreInput(id = ns("add_card_ok"), default = NULL), + NULL, + "Add Card" ) ) - } + ) + } - shiny::observeEvent(input$add_report_card_button, { - shiny::showModal(add_modal()) - }) + shiny::observeEvent(input$add_report_card_button, { + shiny::showModal(add_modal()) + }) - # the add card button is disabled when clicked to prevent multi-clicks - # please check the ui part for more information - shiny::observeEvent(input$add_card_ok, { - card_fun_args_nams <- names(formals(card_fun)) - has_card_arg <- "card" %in% card_fun_args_nams - has_comment_arg <- "comment" %in% card_fun_args_nams - has_label_arg <- "label" %in% card_fun_args_nams + # the add card button is disabled when clicked to prevent multi-clicks + # please check the ui part for more information + shiny::observeEvent(input$add_card_ok, { + card_fun_args_nams <- names(formals(card_fun)) + has_card_arg <- "card" %in% card_fun_args_nams + has_comment_arg <- "comment" %in% card_fun_args_nams + has_label_arg <- "label" %in% card_fun_args_nams - arg_list <- list() + arg_list <- list() - if (has_comment_arg) { - arg_list <- c(arg_list, list(comment = input$comment)) - } - if (has_label_arg) { - arg_list <- c(arg_list, list(label = input$label)) - } + if (has_comment_arg) { + arg_list <- c(arg_list, list(comment = input$comment)) + } + if (has_label_arg) { + arg_list <- c(arg_list, list(label = input$label)) + } - if (has_card_arg) { - # The default_card is defined here because formals() returns a pairedlist object - # of formal parameter names and their default values. The values are missing - # if not defined and the missing check does not work if supplied formals(card_fun)[[1]] - default_card <- formals(card_fun)$card - card <- `if`( - missing(default_card), - ReportCard$new(), - eval(default_card, envir = environment(card_fun)) - ) - arg_list <- c(arg_list, list(card = card)) - } + if (has_card_arg) { + # The default_card is defined here because formals() returns a pairedlist object + # of formal parameter names and their default values. The values are missing + # if not defined and the missing check does not work if supplied formals(card_fun)[[1]] + default_card <- formals(card_fun)$card + card <- `if`( + missing(default_card), + ReportCard$new(), + eval(default_card, envir = environment(card_fun)) + ) + arg_list <- c(arg_list, list(card = card)) + } - card <- try(do.call(card_fun, arg_list)) + card <- try(do.call(card_fun, arg_list)) - if (inherits(card, "try-error")) { - msg <- paste0( - "The card could not be added to the report. ", - "Have the outputs for the report been created yet? If not please try again when they ", - "are ready. Otherwise contact your application developer" - ) - warning(msg) - shiny::showNotification( - msg, - type = "error" - ) - } else { - checkmate::assert_class(card, "ReportCard") - if (!has_comment_arg && length(input$comment) > 0 && input$comment != "") { - card$append_text("Comment", "header3") - card$append_text(input$comment) - } - - if (!has_label_arg && length(input$label) == 1 && input$label != "") { - card$set_name(input$label) - } - - reporter$append_cards(list(card)) - shiny::showNotification(sprintf("The card added successfully."), type = "message") - shiny::removeModal() + if (inherits(card, "try-error")) { + msg <- paste0( + "The card could not be added to the report. ", + "Have the outputs for the report been created yet? If not please try again when they ", + "are ready. Otherwise contact your application developer" + ) + warning(msg) + shiny::showNotification( + msg, + type = "error" + ) + } else { + checkmate::assert_class(card, "ReportCard") + if (!has_comment_arg && length(input$comment) > 0 && input$comment != "") { + card$append_text("Comment", "header3") + card$append_text(input$comment) } - }) - } + + if (!has_label_arg && length(input$label) == 1 && input$label != "") { + card$set_name(input$label) + } + + reporter$append_cards(list(card)) + shiny::showNotification(sprintf("The card added successfully."), type = "message") + shiny::removeModal() + } + }) + } ) } diff --git a/R/DownloadModule.R b/R/DownloadModule.R index 3a4cbf12..abb583f2 100644 --- a/R/DownloadModule.R +++ b/R/DownloadModule.R @@ -73,84 +73,85 @@ download_report_button_srv <- function(id, ) checkmate::assert_true(rmd_yaml_args[["output"]] %in% rmd_output) - shiny::moduleServer( - id, - function(input, output, session) { - ns <- session$ns - shiny::setBookmarkExclude(c("download_button")) - download_modal <- function() { - nr_cards <- length(reporter$get_cards()) - downb <- shiny::tags$a( - id = ns("download_data"), - class = paste("btn btn-primary shiny-download-link", if (nr_cards) NULL else "disabled"), - style = if (nr_cards) NULL else "pointer-events: none;", - href = "", - target = "_blank", - download = NA, - shiny::icon("download"), - "Download" - ) - shiny::modalDialog( - easyClose = TRUE, - shiny::tags$h3("Download the Report"), - shiny::tags$hr(), - if (length(reporter$get_cards()) == 0) { - shiny::tags$div( - class = "mb-4", - shiny::tags$p( - class = "text-danger", - shiny::tags$strong("No Cards Added") - ) - ) - } else { - shiny::tags$div( - class = "mb-4", - shiny::tags$p( - class = "text-success", - shiny::tags$strong(paste("Number of cards: ", nr_cards)) - ), + shiny::moduleServer(id, function(input, output, session) { + + shiny::setBookmarkExclude(c("download_button")) + + ns <- session$ns + + download_modal <- function() { + nr_cards <- length(reporter$get_cards()) + downb <- shiny::tags$a( + id = ns("download_data"), + class = paste("btn btn-primary shiny-download-link", if (nr_cards) NULL else "disabled"), + style = if (nr_cards) NULL else "pointer-events: none;", + href = "", + target = "_blank", + download = NA, + shiny::icon("download"), + "Download" + ) + shiny::modalDialog( + easyClose = TRUE, + shiny::tags$h3("Download the Report"), + shiny::tags$hr(), + if (length(reporter$get_cards()) == 0) { + shiny::tags$div( + class = "mb-4", + shiny::tags$p( + class = "text-danger", + shiny::tags$strong("No Cards Added") ) - }, - reporter_download_inputs( - rmd_yaml_args = rmd_yaml_args, - rmd_output = rmd_output, - showrcode = any_rcode_block(reporter), - session = session - ), - footer = shiny::tagList( - shiny::tags$button( - type = "button", - class = "btn btn-secondary", - `data-dismiss` = "modal", - `data-bs-dismiss` = "modal", - NULL, - "Cancel" + ) + } else { + shiny::tags$div( + class = "mb-4", + shiny::tags$p( + class = "text-success", + shiny::tags$strong(paste("Number of cards: ", nr_cards)) ), - downb ) - ) - } - - shiny::observeEvent(input$download_button, { - shiny::showModal(download_modal()) - }) - - output$download_data <- shiny::downloadHandler( - filename = function() { - paste("report_", format(Sys.time(), "%y%m%d%H%M%S"), ".zip", sep = "") }, - content = function(file) { - shiny::showNotification("Rendering and Downloading the document.") - shinybusy::block(id = ns("download_data"), text = "", type = "dots") - input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]]) - names(input_list) <- names(rmd_yaml_args) - if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode - report_render_and_compress(reporter, input_list, global_knitr, file) - shinybusy::unblock(id = ns("download_data")) - }, - contentType = "application/zip" + reporter_download_inputs( + rmd_yaml_args = rmd_yaml_args, + rmd_output = rmd_output, + showrcode = any_rcode_block(reporter), + session = session + ), + footer = shiny::tagList( + shiny::tags$button( + type = "button", + class = "btn btn-secondary", + `data-dismiss` = "modal", + `data-bs-dismiss` = "modal", + NULL, + "Cancel" + ), + downb + ) ) } + + shiny::observeEvent(input$download_button, { + shiny::showModal(download_modal()) + }) + + output$download_data <- shiny::downloadHandler( + filename = function() { + paste("report_", format(Sys.time(), "%y%m%d%H%M%S"), ".zip", sep = "") + }, + content = function(file) { + shiny::showNotification("Rendering and Downloading the document.") + shinybusy::block(id = ns("download_data"), text = "", type = "dots") + input_list <- lapply(names(rmd_yaml_args), function(x) input[[x]]) + names(input_list) <- names(rmd_yaml_args) + if (is.logical(input$showrcode)) global_knitr[["echo"]] <- input$showrcode + report_render_and_compress(reporter, input_list, global_knitr, file) + shinybusy::unblock(id = ns("download_data")) + }, + contentType = "application/zip" + ) + } ) } @@ -174,7 +175,7 @@ report_render_and_compress <- function(reporter, input_list, global_knitr, file if ( identical("pdf_document", input_list$output) && - inherits(try(system2("pdflatex", "--version", stdout = TRUE), silent = TRUE), "try-error") + inherits(try(system2("pdflatex", "--version", stdout = TRUE), silent = TRUE), "try-error") ) { shiny::showNotification( ui = "pdflatex is not available so the pdf_document could not be rendered. Please use other output type.", @@ -262,18 +263,18 @@ reporter_download_inputs <- function(rmd_yaml_args, rmd_output, showrcode, sessi shiny::tagList( lapply(names(rmd_yaml_args), function(e) { switch(e, - author = shiny::textInput(session$ns("author"), label = "Author:", value = rmd_yaml_args$author), - title = shiny::textInput(session$ns("title"), label = "Title:", value = rmd_yaml_args$title), - date = shiny::dateInput(session$ns("date"), "Date:", value = rmd_yaml_args$date), - output = shiny::tags$div( - shinyWidgets::pickerInput( - inputId = session$ns("output"), - label = "Choose a document type: ", - choices = rmd_output, - selected = rmd_yaml_args$output - ) - ), - toc = shiny::checkboxInput(session$ns("toc"), label = "Include Table of Contents", value = rmd_yaml_args$toc) + author = shiny::textInput(session$ns("author"), label = "Author:", value = rmd_yaml_args$author), + title = shiny::textInput(session$ns("title"), label = "Title:", value = rmd_yaml_args$title), + date = shiny::dateInput(session$ns("date"), "Date:", value = rmd_yaml_args$date), + output = shiny::tags$div( + shinyWidgets::pickerInput( + inputId = session$ns("output"), + label = "Choose a document type: ", + choices = rmd_output, + selected = rmd_yaml_args$output + ) + ), + toc = shiny::checkboxInput(session$ns("toc"), label = "Include Table of Contents", value = rmd_yaml_args$toc) ) }), if (showrcode) { diff --git a/R/Previewer.R b/R/Previewer.R index 916e2dc6..936c5f2d 100644 --- a/R/Previewer.R +++ b/R/Previewer.R @@ -80,9 +80,10 @@ reporter_previewer_srv <- function(id, checkmate::assert_true(rmd_yaml_args[["output"]] %in% rmd_output) shiny::moduleServer(id, function(input, output, session) { - ns <- session$ns - shiny::setBookmarkExclude(c("resetButtonPreviewer", "download_data_prev")) + shiny::setBookmarkExclude(c( + "card_remove_id", "card_down_id", "card_up_id", "remove_card_ok", "showrcode", "download_data_prev") + ) session$onBookmark(function(state) { state$values$report_cards <- reporter$get_cards() }) @@ -90,6 +91,8 @@ reporter_previewer_srv <- function(id, reporter$append_cards(state$values$report_cards) }) + ns <- session$ns + reset_report_button_srv("resetButtonPreviewer", reporter) output$encoding <- shiny::renderUI({ @@ -219,12 +222,12 @@ block_to_html <- function(b) { b_content <- b$get_content() if (inherits(b, "TextBlock")) { switch(b$get_style(), - header1 = shiny::tags$h1(b_content), - header2 = shiny::tags$h2(b_content), - header3 = shiny::tags$h3(b_content), - header4 = shiny::tags$h4(b_content), - verbatim = shiny::tags$pre(b_content), - shiny::tags$pre(b_content) + header1 = shiny::tags$h1(b_content), + header2 = shiny::tags$h2(b_content), + header3 = shiny::tags$h3(b_content), + header4 = shiny::tags$h4(b_content), + verbatim = shiny::tags$pre(b_content), + shiny::tags$pre(b_content) ) } else if (inherits(b, "RcodeBlock")) { panel_item("R Code", shiny::tags$pre(b_content)) diff --git a/R/ResetModule.R b/R/ResetModule.R index 59a74afc..b2afc7e8 100644 --- a/R/ResetModule.R +++ b/R/ResetModule.R @@ -45,42 +45,43 @@ reset_report_button_ui <- function(id, label = NULL) { 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::moduleServer(id, function(input, output, session) { + shiny::setBookmarkExclude(c("reset_reporter")) - 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-secondary", - `data-dismiss` = "modal", - `data-bs-dismiss` = "modal", - NULL, - "Cancel" - ), - shiny::actionButton(ns("reset_reporter_ok"), "Reset", class = "btn-danger") + 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-secondary", + `data-dismiss` = "modal", + `data-bs-dismiss` = "modal", + NULL, + "Cancel" + ), + shiny::actionButton(ns("reset_reporter_ok"), "Reset", class = "btn-danger") ) ) - }) + ) + }) - shiny::observeEvent(input$reset_reporter_ok, { - reporter$reset() - shiny::removeModal() - }) - } + shiny::observeEvent(input$reset_reporter_ok, { + reporter$reset() + shiny::removeModal() + }) + } ) }