在 R Shiny 中使用 javascript 观察 sweetalert2 确认

Observe sweetalert2 confirm with javascript in R Shiny

我已经切换到 sweetalert2,因为旧版本比积极开发的新版本更受限制。

我 运行 遇到了问题,但是,我在旧版本中用于观察确认或取消的代码不再适用于我。

在旧版本中我曾经在

之后的'myjava'代码中添加一个函数
closeOnConfirm: true}

即:

,
 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()]);
}
}

但这似乎不适用于 sweetalert2。

我试着让网站上的示例正常工作,但没有成功。 https://sweetalert2.github.io/ 他们使用这样的结构:

.then((result) =>  {
  if (result.value === true) {
  swal('Processing');
    } 
});

但它一直导致 警告:错误:shinyjs:解析 JavaScript 文件时出错:语法错误:意外标记 >.

这是用来测试它的应用程序。您需要更改目录并下载这两个文件才能使 sweetalert2 正常工作 这里:https://www.jsdelivr.com/package/npm/sweetalert2

下载按钮在标题右侧sweetalert2 所需的 2 个文件位于名为 dist 的文件夹中:

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)

更新 我尝试了此 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');
} 
});
};"

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