Skip to content

Commit

Permalink
898 save app state version 3 (#268)
Browse files Browse the repository at this point in the history
Companion to insightsengineering/teal#1011
Introduces bookmarking of report cards.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Dawid Kałędkowski <[email protected]>
  • Loading branch information
3 people authored Mar 28, 2024
1 parent 02e1773 commit 3f301ca
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 320 deletions.
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

0 comments on commit 3f301ca

Please sign in to comment.