r - 单击时闪亮仪表板的 dropdownMenu 事件
问题描述
按照这个问题和答案获取最近点击的通知项在闪亮仪表板中的下拉菜单
我在下面创建了一个应用程序,它在单击任务项时很好地打开了一个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。只有在中间击中另一个项目时才会再次打开它。如何解决?
解决方案
您可以在 rstudio 网站上找到一篇很好的文章:https ://shiny.rstudio.com/articles/js-send-message.html 。
问题的根源:
警告:Shiny 只监听消息值的变化。因此,如果您使用相同的参数调用 doAwesomeThing2 两次,则第二次调用不会触发 observeEvent 块,因为您发送的对象未更改。
解决方案:
这可以通过向您的对象添加一个随机值来克服,这会使整个对象看起来更改为闪亮。在 R 中,您只需忽略对象的那一部分....
因此,在您的情况下,您可以将代码更改为:
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
,但我没有添加showCloseButton
参数的可能性,所以我删除了它。
推荐阅读
- javascript - 为什么我得到“无法获取未定义或空引用的属性'值'”?
- python - 从打字中扩展 FrozenSet
- javascript - 如何使用 node.js 获取 pouchdb 文档的所有 id
- python - 如何调整seasonal_decompose() 上的x 标签?
- node.js - 在 CentOS-7-x86_64-GenericCloud.qcow2 映像上安装 yarn、npm 和 Node.js 会导致问题
- python - 带字符串的 Python 密钥生成器不起作用
- spring-boot - webflux Mono 响应为空
- python-3.x - 无法计算决策树回归器的模型性能
- jenkins - 错误消息:未配置 JIRA 站点 | 使用 JiraCloud
- json - 如何使用 json.net 反序列化字符串 - 该库有两个版本吗?