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

时间:2018-11-23 16:30:05

标签: javascript r shiny sweetalert2 shinyjs

我已切换到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文件时出错:SyntaxError:意外令牌>。

这里是进行测试的应用程序。您将需要更改目录并下载两个文件以使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');
} 
});
};"

1 个答案:

答案 0 :(得分:0)

多亏了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"});}  
});
};"