How to build an R Shiny version of a sweetalert2
message with a dropdown menu in it.
I've build quite the number and types of sweetalert messages in R Shiny, but this is a new type for me and I'm stuck on the right way to get the choice from a selectinput inside a message out as text rather than a number.
Got it working to some extend, but output is number of nth element in list rather than the text strings.....
The original pure javascript example: example under select (posted at bottom of this message.
After the demo app I made, which spits out numbers instead of text, I tried this: (following the working solution I build in the end based on this other SO question on sweetalerts
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value === 'Select a batchname') {
resolve('You need to select a batchname')
} else {
resolve()
}
})
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
Shiny.setInputValue('SweetDropChoice', result.value, {priority: 'event'});
}
});
};"
I think my problem is, is that I have no clue how to use the resolve from the example in my own version properly.
Here is the app to test it with. You will need to change the directory and download the two files to make sweetalert2
work
here: https://www.jsdelivr.com/package/npm/sweetalert2 ,
download button is on the right of the title: sweetalert2
and the 2 files needed are in the dist folder named:
sweetalert2.min.js & sweetalert2.min.css
setwd(PASTE LOCATION WHERE YOU SAVED THE SWEETALERT SCRIPTS)
library(shiny)
library(shinyjs)
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true})
.then(function(result){
if (result.value === 'Select a batchname') {
resolve('You need to select a batchname:)')
} else {
var batchname = result.value
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});}
});
};"
ui <- fluidPage(
actionButton(inputId = 'messagebutton', label = 'click me'),
verbatimTextOutput('Choice', placeholder = T),
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = myjava),
tags$head(includeScript("sweetalert2.min.js"),
includeCSS("sweetalert2.min.css")
)
)
server <- function(input, output, session) {
values <- reactiveValues(Choice = '?',
Choices = rownames(mtcars)[1:6] ## dummy input to use in the sweetalert with dropdown
)
observeEvent(input$messagebutton, {
shinyjs::js$swalFromButton( title = paste('<span style ="color:#339FFF;">An alert with a choice'),
html = paste('Pick a name'),
inputOptions = values$Choices)
})
output$Choice <- renderPrint({input$SweetDropChoice}) ## print output of new sweetalert
}
shinyApp(ui = ui, server = server)
Here is my version. But that's the same as yours, as far as I can see. Except that I have not included the if(result.dismiss === swal.DismissReason.cancel)
(I'm not sure this is correct BTW, there's no dismiss
field in result
).
myjava <- "
shinyjs.swalFromButton = function(params) {
var defaultParams = {
title: null,
html: null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({
title: params.title,
html: params.html,
inputOptions: params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value !== '') {
resolve();
} else {
resolve('You need to select a batch :)');
}
});
}
})
.then(function(result){
var batchname = params.inputOptions[result.value];
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
});
};"