首页 > 解决方案 > 单击时闪亮仪表板的 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。只有在中间击中另一个项目时才会再次打开它。如何解决?

标签: rshinyshinydashboardsweetalert

解决方案


您可以在 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参数的可能性,所以我删除了它。


推荐阅读