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

refine Shiny Gadget #29

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
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
Loading