遵循此问题并回答Get the most recently clicked notificationItem of a dropdownmenu in shinydashboard
我创建了一个应用程序,该应用程序在下面单击一个taskItem时会很好地打开一个Sweetalert。
library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(tidyverse)
ui <- fluidPage(
dashboardPage(
dashboardHeader(dropdownMenuOutput("dropdownmenu")),
dashboardSidebar(),
dashboardBody(
tags$script(HTML("function clickFunction(link){ Shiny.onInputChange('linkClicked',link);}")),
)))
server = shinyServer(function(input, output, session){
output$dropdownmenu = renderMenu({
aa <- 1:2 %>%
map(~taskItem(text = paste("This is no", .), value = ., color = c("red", "blue")[.]))
for(i in 1:length(aa)){
aa[[i]]$children[[1]] <- a(href="#","onclick"=paste0("clickFunction('",paste("This is no", i),"'); return false;"),
aa[[i]]$children[[1]]$children)
}
dropdownMenu(type = "tasks", badgeStatus = "warning",
.list = aa)
})
observeEvent(input$linkClicked, {
sendSweetAlert(
session = session,
text = input$linkClicked,
type = "info",
showCloseButton = TRUE)
})
})
shinyApp(ui = ui, server = server)
但是两次击中相同的taskItem不会再次打开Sweetalert。仅在中间之间碰到另一个项目时,它将再次打开。如何解决?
答案 0 :(得分:1)
您可以在rstudio网站上找到一篇不错的文章:https://shiny.rstudio.com/articles/js-send-message.html。
问题的根源:
注意事项: Shiny仅侦听消息值的更改。 因此,如果您使用相同的参数两次调用doAwesomeThing2,则 第二次调用不会触发observeEvent块,因为该对象 您发送的内容不变。
解决方案:
可以通过添加随机值来克服 到您的对象,这使得该对象作为一个整体显示为 闪亮在R中,您只需忽略对象的那一部分。...
因此,您可以将代码更改为:
Object.defineProperty(Object.prototype, 'merge', {
value: function(mergeObj){
for (var propertyName in mergeObj) {
if (mergeObj.hasOwnProperty(propertyName)) {
this[propertyName] = mergeObj[propertyName];
}
}
return this;
},
enumerable: false, // this is actually the default
});
对触发输入的调用将是:
tags$script(HTML("function clickFunction(link){
var rndm = Math.random();
Shiny.onInputChange('linkClicked', {data:link, nonce: Math.random()});}"
))
完整的示例:
input$linkClicked$data
注意:
我假设您具有library(shiny)
library(shinydashboard)
library(tidyverse)
library(shinyWidgets)
ui <- fluidPage(
dashboardPage(
dashboardHeader(dropdownMenuOutput("dropdownmenu")),
dashboardSidebar(),
dashboardBody(
tags$script(HTML("function clickFunction(link){
var rndm = Math.random();
Shiny.onInputChange('linkClicked', {data:link, nonce: Math.random()});}"
)),
)))
server = shinyServer(function(input, output, session){
output$dropdownmenu = renderMenu({
aa <- 1:2 %>%
map(~taskItem(text = paste("This is no", .), value = ., color = c("red", "blue")[.]))
for(i in 1:length(aa)){
aa[[i]]$children[[1]] <- a(href="#","onclick"=paste0("clickFunction('",paste("This is no", i),"'); return false;"),
aa[[i]]$children[[1]]$children)
}
dropdownMenu(type = "tasks", badgeStatus = "warning",
.list = aa)
})
observeEvent(input$linkClicked, {
sendSweetAlert(
session = session,
text = input$linkClicked$data,
type = "info"
)
})
})
shinyApp(ui = ui, server = server)
中的sweetalert()
函数,但是我没有添加shinyWidgets
参数的可能性,因此我将其删除。