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

898 save app state version 3 #268

Merged
merged 16 commits into from
Mar 28, 2024
3 changes: 2 additions & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linters: linters_with_defaults(
line_length_linter = line_length_linter(120),
cyclocomp_linter = NULL,
object_usage_linter = NULL
object_usage_linter = NULL,
indentation_linter = NULL
)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# teal.reporter 0.3.1.9001

### Enhancements

* Report cards are now included in bookmarks. When using the `shiny` bookmarking mechanism, present report cards will be available in the restored application.

# teal.reporter 0.3.1

### Enhancements
Expand Down
212 changes: 108 additions & 104 deletions R/AddCardModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,125 +88,129 @@ 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) {
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())
})
)
}

# 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
shiny::observeEvent(input$add_report_card_button, {
shiny::showModal(add_modal())
})

arg_list <- list()
# 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

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))
}
arg_list <- list()

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_comment_arg) {
arg_list <- c(arg_list, list(comment = input$comment))
}
if (has_label_arg) {
arg_list <- c(arg_list, list(label = input$label))
}

card <- try(do.call(card_fun, arg_list))
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 (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)
}
card <- try(do.call(card_fun, arg_list))

if (!has_label_arg && length(input$label) == 1 && input$label != "") {
card$set_name(input$label)
}
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)
}

reporter$append_cards(list(card))
shiny::showNotification(sprintf("The card added successfully."), type = "message")
shiny::removeModal()
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()
}
})
})
}
Loading
Loading