Observe sweetalert2 confirm with javascript in R Shiny
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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');
}
});
};"
javascript r shiny sweetalert2 shinyjs
|
show 2 more comments
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');
}
});
};"
javascript r shiny sweetalert2 shinyjs
Instead of writing.then((result) => ...
can't you write.then(function(result) ...
? It looks like shinyjs does not like the>
.
– Stéphane Laurent
Nov 24 '18 at 7:55
Tried, and updated the answer, can't get the correct syntax working Stephane. javascript is still a huge struggle for me compared to R coding
– Mark
Nov 24 '18 at 10:42
No.then(function(result){
, notthen((result){
.
– Stéphane Laurent
Nov 24 '18 at 10:52
Thanks! I managed to get the rest of the code to send variables back to R shiny server working as well now.
– Mark
Nov 24 '18 at 11:01
Nice. FYI I already did a Shiny app using sweetalert2 but without using shinyjs. Unfortunately I don't currently have the computer in which it is saved.
– Stéphane Laurent
Nov 24 '18 at 11:02
|
show 2 more comments
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');
}
});
};"
javascript r shiny sweetalert2 shinyjs
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');
}
});
};"
javascript r shiny sweetalert2 shinyjs
javascript r shiny sweetalert2 shinyjs
edited Nov 24 '18 at 10:42
Mark
asked Nov 23 '18 at 16:30
MarkMark
945525
945525
Instead of writing.then((result) => ...
can't you write.then(function(result) ...
? It looks like shinyjs does not like the>
.
– Stéphane Laurent
Nov 24 '18 at 7:55
Tried, and updated the answer, can't get the correct syntax working Stephane. javascript is still a huge struggle for me compared to R coding
– Mark
Nov 24 '18 at 10:42
No.then(function(result){
, notthen((result){
.
– Stéphane Laurent
Nov 24 '18 at 10:52
Thanks! I managed to get the rest of the code to send variables back to R shiny server working as well now.
– Mark
Nov 24 '18 at 11:01
Nice. FYI I already did a Shiny app using sweetalert2 but without using shinyjs. Unfortunately I don't currently have the computer in which it is saved.
– Stéphane Laurent
Nov 24 '18 at 11:02
|
show 2 more comments
Instead of writing.then((result) => ...
can't you write.then(function(result) ...
? It looks like shinyjs does not like the>
.
– Stéphane Laurent
Nov 24 '18 at 7:55
Tried, and updated the answer, can't get the correct syntax working Stephane. javascript is still a huge struggle for me compared to R coding
– Mark
Nov 24 '18 at 10:42
No.then(function(result){
, notthen((result){
.
– Stéphane Laurent
Nov 24 '18 at 10:52
Thanks! I managed to get the rest of the code to send variables back to R shiny server working as well now.
– Mark
Nov 24 '18 at 11:01
Nice. FYI I already did a Shiny app using sweetalert2 but without using shinyjs. Unfortunately I don't currently have the computer in which it is saved.
– Stéphane Laurent
Nov 24 '18 at 11:02
Instead of writing
.then((result) => ...
can't you write .then(function(result) ...
? It looks like shinyjs does not like the >
.– Stéphane Laurent
Nov 24 '18 at 7:55
Instead of writing
.then((result) => ...
can't you write .then(function(result) ...
? It looks like shinyjs does not like the >
.– Stéphane Laurent
Nov 24 '18 at 7:55
Tried, and updated the answer, can't get the correct syntax working Stephane. javascript is still a huge struggle for me compared to R coding
– Mark
Nov 24 '18 at 10:42
Tried, and updated the answer, can't get the correct syntax working Stephane. javascript is still a huge struggle for me compared to R coding
– Mark
Nov 24 '18 at 10:42
No.
then(function(result){
, not then((result){
.– Stéphane Laurent
Nov 24 '18 at 10:52
No.
then(function(result){
, not then((result){
.– Stéphane Laurent
Nov 24 '18 at 10:52
Thanks! I managed to get the rest of the code to send variables back to R shiny server working as well now.
– Mark
Nov 24 '18 at 11:01
Thanks! I managed to get the rest of the code to send variables back to R shiny server working as well now.
– Mark
Nov 24 '18 at 11:01
Nice. FYI I already did a Shiny app using sweetalert2 but without using shinyjs. Unfortunately I don't currently have the computer in which it is saved.
– Stéphane Laurent
Nov 24 '18 at 11:02
Nice. FYI I already did a Shiny app using sweetalert2 but without using shinyjs. Unfortunately I don't currently have the computer in which it is saved.
– Stéphane Laurent
Nov 24 '18 at 11:02
|
show 2 more comments
1 Answer
1
active
oldest
votes
Thanks to Stéphane Laurents comments, this is the solution:
Including the means to send a variable back to R shiny.
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(function(result){
swal('succes');
if (result.value === true) {
var val1= true;
Shiny.setInputValue('option1', val1, {priority: "event"});}
else {
swal('failure');
var val2= true;
Shiny.setInputValue('option2', val2, {priority: "event"});}
});
};"
Note that you don't need to include theMath.random()
if you set the propertypriority = "event"
, like this:Shiny.setInputValue("option1", val1, {priority: "event"});
. The functionShiny.setInputValue
is the same asShiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html
– Stéphane Laurent
Nov 24 '18 at 12:29
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
I posted a new question
– Mark
Dec 4 '18 at 18:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450161%2fobserve-sweetalert2-confirm-with-javascript-in-r-shiny%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks to Stéphane Laurents comments, this is the solution:
Including the means to send a variable back to R shiny.
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(function(result){
swal('succes');
if (result.value === true) {
var val1= true;
Shiny.setInputValue('option1', val1, {priority: "event"});}
else {
swal('failure');
var val2= true;
Shiny.setInputValue('option2', val2, {priority: "event"});}
});
};"
Note that you don't need to include theMath.random()
if you set the propertypriority = "event"
, like this:Shiny.setInputValue("option1", val1, {priority: "event"});
. The functionShiny.setInputValue
is the same asShiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html
– Stéphane Laurent
Nov 24 '18 at 12:29
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
I posted a new question
– Mark
Dec 4 '18 at 18:08
add a comment |
Thanks to Stéphane Laurents comments, this is the solution:
Including the means to send a variable back to R shiny.
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(function(result){
swal('succes');
if (result.value === true) {
var val1= true;
Shiny.setInputValue('option1', val1, {priority: "event"});}
else {
swal('failure');
var val2= true;
Shiny.setInputValue('option2', val2, {priority: "event"});}
});
};"
Note that you don't need to include theMath.random()
if you set the propertypriority = "event"
, like this:Shiny.setInputValue("option1", val1, {priority: "event"});
. The functionShiny.setInputValue
is the same asShiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html
– Stéphane Laurent
Nov 24 '18 at 12:29
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
I posted a new question
– Mark
Dec 4 '18 at 18:08
add a comment |
Thanks to Stéphane Laurents comments, this is the solution:
Including the means to send a variable back to R shiny.
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(function(result){
swal('succes');
if (result.value === true) {
var val1= true;
Shiny.setInputValue('option1', val1, {priority: "event"});}
else {
swal('failure');
var val2= true;
Shiny.setInputValue('option2', val2, {priority: "event"});}
});
};"
Thanks to Stéphane Laurents comments, this is the solution:
Including the means to send a variable back to R shiny.
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(function(result){
swal('succes');
if (result.value === true) {
var val1= true;
Shiny.setInputValue('option1', val1, {priority: "event"});}
else {
swal('failure');
var val2= true;
Shiny.setInputValue('option2', val2, {priority: "event"});}
});
};"
edited Dec 4 '18 at 16:52
answered Nov 24 '18 at 11:00
MarkMark
945525
945525
Note that you don't need to include theMath.random()
if you set the propertypriority = "event"
, like this:Shiny.setInputValue("option1", val1, {priority: "event"});
. The functionShiny.setInputValue
is the same asShiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html
– Stéphane Laurent
Nov 24 '18 at 12:29
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
I posted a new question
– Mark
Dec 4 '18 at 18:08
add a comment |
Note that you don't need to include theMath.random()
if you set the propertypriority = "event"
, like this:Shiny.setInputValue("option1", val1, {priority: "event"});
. The functionShiny.setInputValue
is the same asShiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html
– Stéphane Laurent
Nov 24 '18 at 12:29
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
I posted a new question
– Mark
Dec 4 '18 at 18:08
Note that you don't need to include the
Math.random()
if you set the property priority = "event"
, like this: Shiny.setInputValue("option1", val1, {priority: "event"});
. The function Shiny.setInputValue
is the same as Shiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html– Stéphane Laurent
Nov 24 '18 at 12:29
Note that you don't need to include the
Math.random()
if you set the property priority = "event"
, like this: Shiny.setInputValue("option1", val1, {priority: "event"});
. The function Shiny.setInputValue
is the same as Shiny.onInputChange
, but its name is better. Ref: shiny.rstudio.com/articles/communicating-with-js.html– Stéphane Laurent
Nov 24 '18 at 12:29
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Good to know, but for the amount of time this code is running I don't think it will have a big impact, but good to know that there is a newer, slightly cleaner way of coding it
– Mark
Nov 24 '18 at 12:57
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Stephane, I'm now looking at the next hurdle, namely getting text input alerts with validation steps working in the new version. Would you have a chance to help me with those?
– Mark
Dec 4 '18 at 16:21
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
Sorry, I don't see what you mean. Which "new version" ?
– Stéphane Laurent
Dec 4 '18 at 18:05
I posted a new question
– Mark
Dec 4 '18 at 18:08
I posted a new question
– Mark
Dec 4 '18 at 18:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450161%2fobserve-sweetalert2-confirm-with-javascript-in-r-shiny%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Instead of writing
.then((result) => ...
can't you write.then(function(result) ...
? It looks like shinyjs does not like the>
.– Stéphane Laurent
Nov 24 '18 at 7:55
Tried, and updated the answer, can't get the correct syntax working Stephane. javascript is still a huge struggle for me compared to R coding
– Mark
Nov 24 '18 at 10:42
No.
then(function(result){
, notthen((result){
.– Stéphane Laurent
Nov 24 '18 at 10:52
Thanks! I managed to get the rest of the code to send variables back to R shiny server working as well now.
– Mark
Nov 24 '18 at 11:01
Nice. FYI I already did a Shiny app using sweetalert2 but without using shinyjs. Unfortunately I don't currently have the computer in which it is saved.
– Stéphane Laurent
Nov 24 '18 at 11:02