3

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

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

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

服务器.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 或索引。那可能吗?

4

1 回答 1

1

这个答案完全基于@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")
    )
)

服务器.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,并且我使用这些信息来进行我需要的任何更新。

于 2015-12-24T08:48:00.527 回答