我正在将R Shiny从sweetalert升级到sweetalert2,到目前为止,R Shiny服务器已经设法对警报消息中的ok / cancel按钮起作用进行了常规响应,但是现在我陷入了下一种类型文本输入消息,我曾在其中执行以下验证:
Here,您可以找到其中实现了sweetalert2的应用程序。
在新问题中,我试图用包含输入消息的消息替换该应用程序中的javascript:
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title: null,
html : null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html,
input: 'text',
showCancelButton : true,
showConfirmButton : true,
closeOnConfirm: false,
confirmButtonColor: '#339FFF',
allowOutsideClick: false,
inputValidator: function(value) {
if(value === '') { return !value && 'You need to write something!'}
else {
var val2= true;
Shiny.setInputValue('option2', val2, {priority: 'event'}) };
}
});
};"
到目前为止,该方法仍然有效,但是我不知道如何添加其他使用特殊字符的检查(文件名中不允许) 在我的旧代码中,我有以下代码用于sweetalert(1)的工作:
var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
if(format.test(inputValue)){
swal.showInputError('Special characters are not allowed');
return false;
}
但是当我构建它时,它在sweetalert2中不起作用:
myjava <- "shinyjs.swalFromButton = function(params) { swalFromButton = function(params) { var defaultParams = {
title: null,
html : null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html,
input: 'text',
showCancelButton : true,
showConfirmButton : true,
closeOnConfirm: false,
confirmButtonColor: '#339FFF',
allowOutsideClick: false,
inputValidator: function(value) {
if(value === '') { return !value && 'You need to write something!'}
else {
var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
if(format.test(value)){
return !value && 'Special characters are not allowed'}
else {
var val2= true;
Shiny.setInputValue('option2', value, {priority: 'event'})}
}
}
});
};
答案 0 :(得分:1)
如另一篇文章中所承诺的,这是一个没有shinyjs
的解决方案:
library(shiny)
js <- "
Shiny.addCustomMessageHandler('sweet',
function(message) {
swal({
title : message.title,
html : message.html,
input : 'text',
showConfirmButton : true,
confirmButtonText : 'Confirm',
confirmButtonColor: '#00cc00',
showCancelButton : true,
cancelButtonText : 'Cancel',
cancelButtonColor : '#339fff',
allowOutsideClick: true,
allowEscapeKey: true,
inputValidator: function(value) {
if(value === '') {
return 'You need to write something!'
} else {
var format = /\\`|\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\+|\\=|\\[|\\{|\\]|\\}|\\||\\\\|\\'|\\<|\\,|\\.|\\>|\\?|\\/|\"|\\;|\\:/g;
if(format.test(value)){
return 'Special characters are not allowed'
}
}
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
swal('failure');
} else {
swal('success');
Shiny.setInputValue('option1', result.value, {priority: 'event'});
}
});
}
);
"
ui <- basicPage(
tags$head(tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.all.min.js"),
tags$link(rel="stylesheet", type="text/css", href = "https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.min.css"),
tags$script(js)
),
actionButton("messageButton", "Click me")
)
server <- function(input, output, session){
observeEvent(input$messageButton, {
session$sendCustomMessage(type = "sweet",
message = list(title = paste('<span style ="color:#339FFF;">An alert with an input text'),
html = "Enter text"))
})
observe({print(input$option1)})
}
shinyApp(ui, server)
答案 1 :(得分:0)
好,解决了,找出了正确的语法
消除!value &&
的窍门:
shinyjs.swalFromButton = function(params) { var defaultParams = {
title: null,
html : null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html,
input: 'text',
showCancelButton : true,
showConfirmButton : true,
closeOnConfirm: false,
confirmButtonColor: '#339FFF',
allowOutsideClick: false,
inputValidator: function(value) {
if(value === '') {
return 'You need to write something!'}
else {
var format = /[~!@#$%^&*()_+\-=\[\]{};':"\\|,<>\/?]+/;
if(format.test(value)){
return 'Special characters are not allowed'}
}
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
Shiny.setInputValue('option2', value, {priority: 'event'})}
}
}
});
};