![](/img/trans.png)
[英]sweetalert2 text input with validation of the text with javascript in R Shiny
[英]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文件時出錯:SyntaxError:意外令牌>。
這是用於測試的應用程序。 您將需要更改目錄並下載兩個文件,以使sweetalert2
在此處工作: https : sweetalert2
下載按鈕位於標題sweetalert2的右側,所需的2個文件位於名為dist的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é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.