Skip to content

Commit

Permalink
refine Shiny Gadget (simonpcouch#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch authored Oct 11, 2024
1 parent 30c7a4f commit 3f2fb69
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions R/gadget.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
.pal <- function() {
pal_fn <- .pal_app()
if (is.null(pal_fn) || identical(pal_fn, ".pal_rs__")) {
return(NULL)
# suppress "Listening on..." message and rethrow errors with new context
try_fetch(
suppressMessages(pal_fn <- .pal_app()),
error = function(cnd) {cli::cli_abort(conditionMessage(cnd), call = NULL)}
)

if (is.null(pal_fn) || identical(pal_fn, ".pal_rs_")) {
return(invisible())
}

# call the binding associated with the chosen pal
try_fetch(
do.call(pal_fn, args = list()),
error = function(e) {
cli::cli_abort("Unable to locate the requested pal.")
}
)

invisible()
}

.pal_app <- function() {
Expand All @@ -17,9 +26,14 @@
ui <- miniUI::miniPage(
miniUI::miniContentPanel(
shiny::selectizeInput("pal", "Select a pal:",
choices = NULL,
selected = NULL,
multiple = FALSE
choices = pal_choices,
multiple = FALSE,
options = list(
create = FALSE,
placeholder = 'Type to filter or select a pal',
onDropdownOpen = I("function($dropdown) {this.clear();}"),
onBlur = I("function() {this.clear();}")
)
),
shiny::verbatimTextOutput("result"),
shiny::tags$script(shiny::HTML("
Expand All @@ -28,16 +42,16 @@
Shiny.setInputValue('done', true, {priority: 'event'});
}
});
$(document).ready(function() {
setTimeout(function() {
$('.selectize-input input').focus();
}, 100);
});
"))
)
)

server <- function(input, output, session) {
shiny::updateSelectizeInput(
session, 'pal',
choices = pal_choices,
server = TRUE
)
shiny::observeEvent(input$done, {
shiny::stopApp(returnValue = paste0(".pal_rs__", input$pal))
})
Expand Down

0 comments on commit 3f2fb69

Please sign in to comment.