I have switched to sweetalert2 since the old version is more limited than the new version which is actively developped.
I am running into a problem however, the code I used to observe confirm or cancel in the old version is not working for me anymore.
In the old version I used to add a function in the 'myjava
' code after
closeOnConfirm: true}
namely:
,
evalFunction = function(isConfirm){
if (isConfirm === true) {
var val1= 1;
Shiny.onInputChange('option1', [val1, Math.random()]);
}
else {
var val2= 2;
Shiny.onInputChange('option2'', [val2, Math.random()]);
}
}
but that doesn't work with sweetalert2 it seems.
I tried to try and make the examples on the site work but no luck. https://sweetalert2.github.io/ They use a structure like :
.then((result) => {
if (result.value === true) {
swal('Processing');
}
});
but it keeps resulting in a Warning: Error in : shinyjs: Error parsing the JavaScript file: SyntaxError: Unexpected token >.
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('FOLDER WHERE THE sweetalert2files are ')
library(shiny)
library(shinyjs)
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html,
showConfirmButton : true,
confirmButtonText : 'Left',
confirmButtonColor: '#00cc00',
showCancelButton : true,
cancelButtonText : 'Right',
cancelButtonColor : '#339fff',
closeOnCancel : true,
allowOutsideClick: true,
allowEscapeKey: true,
closeOnConfirm: true});
};"
ui <- fluidPage(
actionButton(inputId = 'messagebutton', label = 'click me'),
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = myjava),
tags$head(includeScript("sweetalert2.min.js"),
includeCSS("sweetalert2.min.css")
)
)
server <- function(input, output, session) {
observeEvent(input$messagebutton, {
shinyjs::js$swalFromButton( title = paste('<span style ="color:#339FFF;">An alert with a choice'),
html = paste('Pick left or right'))
})
observeEvent(input$option1, { print('confirm choosen')})
observeEvent(input$option2, { print('cancel choosen')})
}
shinyApp(ui = ui, server = server)
UPDATE I tried endless variations of this javascript, removing the problematic > symbol as was suggested, but R keeps throwing 'error parsing the javascript code provided.
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html,
showConfirmButton : true,
confirmButtonText : 'Left',
confirmButtonColor: '#00cc00',
showCancelButton : true,
cancelButtonText : 'Right',
cancelButtonColor : '#339fff',
closeOnCancel : true,
allowOutsideClick: true,
allowEscapeKey: true,
closeOnConfirm: true}).then((result){
if (result.value === true) {
swal('Processing');
}
});
};"
Thanks to Stéphane Laurents comments, this is the solution: Including the means to send a variable back to R shiny.