在shinydashboard中获取最近点击的dropdownmenu的notificationItem

时间:2015-12-22 15:19:18

标签: r shiny shinydashboard

在shinydashboard的dashboardHeader功能中,您可以添加多个菜单,例如通知的下拉菜单。当显示通知时,用户自然会点击特定通知,以获取更多信息或采取某些措施。我希望能够检测用户点击了哪个notificationItem,并根据它做了一些事情(例如,更新地图,显示包含与该通知相关的数据的表格等)。

这是我希望的工作,但不是:
ui.R

dashboardPage(  
  dashboardHeader(dropdownMenuOutput("dropdownmenu")),  
  dashboardSidebar(),  
  dashboardBody(textOutput("notificationoutput"))  
)  

server.R

server = shinyServer(function(input, output, session){
  output$dropdownmenu = renderMenu({
    dropdownMenu(type = "notifications", badgeStatus = "warning",
                 notificationItem(icon = icon("users"), status = "info",
                                  "5 new members joined today"
                 ),
                 notificationItem(icon = icon("warning"), status = "danger",
                                  "Resource usage near limit."
                 ),
                 notificationItem(icon = icon("shopping-cart", lib = "glyphicon"),
                                  status = "success", "25 sales made"
                 ),
                 notificationItem(icon = icon("user", lib = "glyphicon"),
                                  status = "danger", "You changed your username"
                 )
    )
  })
  output$notificationoutput = renderText({
    if(is.null(input$dropdownmenu)){
      notificationitemid = "a"
    }else{
      notificationitemid = input$dropdownmenu 
    }
    return(notificationitemid)
  })
})

在我的理想世界中,类似这样的东西会将“a”更新为我可以用来确定点击了哪个通知的id或索引。这可能吗?

1 个答案:

答案 0 :(得分:0)

这个答案完全基于@Batanichek的这个概念:https://stackoverflow.com/a/34413701/3463439 正如问题评论中提到的那样 ui.R:

dashboardPage(
  dashboardHeader(dropdownMenuOutput("dropdownmenu")),
  dashboardSidebar(),
  dashboardBody(
    tags$script(HTML("function clickFunction(link){ 
                   Shiny.onInputChange('linkClicked',link);
                     }")),
    textOutput("notificationoutput")
    )
)

server.R

server = shinyServer(function(input, output, session){
  output$dropdownmenu = renderMenu({
    aa = notificationItem(text = "moo", href = "www")
    aa$children[[1]]=a(href="#","onclick"=paste0("clickFunction('","moo","'); return false;"),aa$children[[1]]$children)
    dropdownMenu(type = "notifications", badgeStatus = "warning",
                 aa,
                 notificationItem(icon = icon("users"), status = "info",
                                  "5 new members joined today"
                 ),
                 notificationItem(icon = icon("warning"), status = "danger",
                                  "Resource usage near limit."
                 ),
                 notificationItem(icon = icon("shopping-cart", lib = "glyphicon"),
                                  status = "success", "25 sales made"
                 ),
                 notificationItem(icon = icon("user", lib = "glyphicon"),
                                  status = "danger", "You changed your username"
                 )
    )
  })
  output$notificationoutput = renderText({
    if(is.null(input$linkClicked)){
      notificationitemid = "a"
    }else{
      notificationitemid = input$linkClicked 
    }
    return(notificationitemid)
  })
})

我现在需要做的就是确保每个notificationItem都有一个我跟踪的不同ID,并且我使用这些信息来进行我需要的任何更新。