简体   繁体   English

观察Sweetalert2在R Shiny中使用javascript进行确认

[英]Observe sweetalert2 confirm with javascript in R Shiny

I have switched to sweetalert2 since the old version is more limited than the new version which is actively developped. 由于旧版本比积极开发的新版本更受限制,因此我改用sweetalert2。

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 在旧版本中,我曾经在' myjava '代码后添加一个函数

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. 但这似乎不适用于sweetalert2。

I tried to try and make the examples on the site work but no luck. 我试图尝试使站点上的示例正常工作,但是没有运气。 https://sweetalert2.github.io/ They use a structure like : https://sweetalert2.github.io/他们使用如下结构:

.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 >. 但它始终导致出现警告:错误:shinyjs:解析JavaScript文件时出错:SyntaxError:意外令牌>。

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 您将需要更改目录并下载两个文件,以使sweetalert2在此处工作: https : sweetalert2

download button is on the right of the title sweetalert2 and the 2 files needed are in the dist folder named: 下载按钮位于标题sweetalert2的右侧,所需的2个文件位于名为dist的dist文件夹中:

sweetalert2.min.js & sweetalert2.min.css 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. 更新我尝试了这种javascript的无尽变体,按照建议的方式删除了有问题的>符号,但是R在解析提供的javascript代码时总是抛出“错误”。

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. 感谢StéphaneLaurents的评论,这是解决方案:包括将变量发送回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"});}  
});
};"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM