带有 R 闪亮下拉菜单的 Sweetalert2 消息
Sweetalert2 message with dropdown in R shiny
如何构建带有下拉菜单的 sweetalert2
消息的 R Shiny 版本。
我已经在 R Shiny 中构建了相当数量和类型的 sweetalert 消息,但这对我来说是一种新类型,我坚持使用正确的方法从消息中的 select 输入中获取选择作为文本而不是数字。
它在某种程度上可以正常工作,但输出是列表中第 n 个元素的数量而不是文本字符串.....
原始纯javascript示例:example下select(贴在本帖底部。
在我制作的演示应用程序吐出数字而不是文本之后,我尝试了这个:(按照我最终基于另一个 SO 关于 sweetalerts 的问题构建的工作解决方案
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value === 'Select a batchname') {
resolve('You need to select a batchname')
} else {
resolve()
}
})
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
Shiny.setInputValue('SweetDropChoice', result.value, {priority: 'event'});
}
});
};"
我认为我的问题是,我不知道如何在我自己的版本中正确使用示例中的解析。
这是用来测试它的应用程序。您需要更改目录并下载这两个文件才能使 sweetalert2
正常工作
这里: https://www.jsdelivr.com/package/npm/sweetalert2 ,
下载按钮在标题右侧:sweetalert2
所需的 2 个文件位于名为 dist 的文件夹中:
sweetalert2.min.js & sweetalert2.min.css
setwd(PASTE LOCATION WHERE YOU SAVED THE SWEETALERT SCRIPTS)
library(shiny)
library(shinyjs)
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true})
.then(function(result){
if (result.value === 'Select a batchname') {
resolve('You need to select a batchname:)')
} else {
var batchname = result.value
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});}
});
};"
ui <- fluidPage(
actionButton(inputId = 'messagebutton', label = 'click me'),
verbatimTextOutput('Choice', placeholder = T),
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = myjava),
tags$head(includeScript("sweetalert2.min.js"),
includeCSS("sweetalert2.min.css")
)
)
server <- function(input, output, session) {
values <- reactiveValues(Choice = '?',
Choices = rownames(mtcars)[1:6] ## dummy input to use in the sweetalert with dropdown
)
observeEvent(input$messagebutton, {
shinyjs::js$swalFromButton( title = paste('<span style ="color:#339FFF;">An alert with a choice'),
html = paste('Pick a name'),
inputOptions = values$Choices)
})
output$Choice <- renderPrint({input$SweetDropChoice}) ## print output of new sweetalert
}
shinyApp(ui = ui, server = server)
@Stéphane 奇怪的是我现在可以使用另一个版本了...
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value === '') {
resolve('You need to select a batchname')
} else {
resolve()
}
})
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
var batchname = params.inputOptions[result.value];
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
}
});
};"
这是我的版本。但据我所知,这和你的一样。除了我没有包含 if(result.dismiss === swal.DismissReason.cancel)
(顺便说一句,我不确定这是否正确,result
中没有 dismiss
字段)。
myjava <- "
shinyjs.swalFromButton = function(params) {
var defaultParams = {
title: null,
html: null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({
title: params.title,
html: params.html,
inputOptions: params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value !== '') {
resolve();
} else {
resolve('You need to select a batch :)');
}
});
}
})
.then(function(result){
var batchname = params.inputOptions[result.value];
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
});
};"
如何构建带有下拉菜单的 sweetalert2
消息的 R Shiny 版本。
我已经在 R Shiny 中构建了相当数量和类型的 sweetalert 消息,但这对我来说是一种新类型,我坚持使用正确的方法从消息中的 select 输入中获取选择作为文本而不是数字。
它在某种程度上可以正常工作,但输出是列表中第 n 个元素的数量而不是文本字符串.....
原始纯javascript示例:example下select(贴在本帖底部。
在我制作的演示应用程序吐出数字而不是文本之后,我尝试了这个:(按照我最终基于另一个 SO 关于 sweetalerts 的问题构建的工作解决方案
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value === 'Select a batchname') {
resolve('You need to select a batchname')
} else {
resolve()
}
})
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
Shiny.setInputValue('SweetDropChoice', result.value, {priority: 'event'});
}
});
};"
我认为我的问题是,我不知道如何在我自己的版本中正确使用示例中的解析。
这是用来测试它的应用程序。您需要更改目录并下载这两个文件才能使 sweetalert2
正常工作
这里: https://www.jsdelivr.com/package/npm/sweetalert2 ,
下载按钮在标题右侧:sweetalert2
所需的 2 个文件位于名为 dist 的文件夹中:
sweetalert2.min.js & sweetalert2.min.css
setwd(PASTE LOCATION WHERE YOU SAVED THE SWEETALERT SCRIPTS)
library(shiny)
library(shinyjs)
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true})
.then(function(result){
if (result.value === 'Select a batchname') {
resolve('You need to select a batchname:)')
} else {
var batchname = result.value
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});}
});
};"
ui <- fluidPage(
actionButton(inputId = 'messagebutton', label = 'click me'),
verbatimTextOutput('Choice', placeholder = T),
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = myjava),
tags$head(includeScript("sweetalert2.min.js"),
includeCSS("sweetalert2.min.css")
)
)
server <- function(input, output, session) {
values <- reactiveValues(Choice = '?',
Choices = rownames(mtcars)[1:6] ## dummy input to use in the sweetalert with dropdown
)
observeEvent(input$messagebutton, {
shinyjs::js$swalFromButton( title = paste('<span style ="color:#339FFF;">An alert with a choice'),
html = paste('Pick a name'),
inputOptions = values$Choices)
})
output$Choice <- renderPrint({input$SweetDropChoice}) ## print output of new sweetalert
}
shinyApp(ui = ui, server = server)
@Stéphane 奇怪的是我现在可以使用另一个版本了...
myjava <- "shinyjs.swalFromButton = function(params) {
var defaultParams = {
title : null,
html : null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({title : params.title, html : params.html, inputOptions : params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value === '') {
resolve('You need to select a batchname')
} else {
resolve()
}
})
}
})
.then(function(result){
if(result.dismiss === swal.DismissReason.cancel) {
} else {
var batchname = params.inputOptions[result.value];
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
}
});
};"
这是我的版本。但据我所知,这和你的一样。除了我没有包含 if(result.dismiss === swal.DismissReason.cancel)
(顺便说一句,我不确定这是否正确,result
中没有 dismiss
字段)。
myjava <- "
shinyjs.swalFromButton = function(params) {
var defaultParams = {
title: null,
html: null,
inputOptions: null
};
params = shinyjs.getParams(params, defaultParams);
swal({
title: params.title,
html: params.html,
inputOptions: params.inputOptions,
input: 'select',
inputPlaceholder: 'Select a batchname',
showCancelButton: true,
inputValidator: function(value) {
return new Promise(function(resolve) {
if (value !== '') {
resolve();
} else {
resolve('You need to select a batch :)');
}
});
}
})
.then(function(result){
var batchname = params.inputOptions[result.value];
Shiny.setInputValue('SweetDropChoice', batchname, {priority: 'event'});
});
};"