问题
In the dashboardHeader function of shinydashboard, you can add multiple menus, such as a drop down menu of notifications. When a notification is displayed, it is natural for the user to click on a specific notification, so as to get more information or to take some action. I'd like to be able to detect which notificationItem the user has clicked on, and do some things based on that (e.g. update a map, show a table with the data relevant to that notification, etc.).
here's what I hoped would work, but doesn't:
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)
})
})
In my ideal world, something like this would update the "a" to an id or index that I could use to determine which notification had been clicked. Is that possible?
回答1:
This answer based entirely on this concept by @Batanichek : https://stackoverflow.com/a/34413701/3463439
As referenced in the question comments.
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)
})
})
All I need to do now is ensure that each notificationItem has a distinct id that I keep track of, and that I use this information to make whatever updates I require.
来源:https://stackoverflow.com/questions/34418993/get-the-most-recently-clicked-notificationitem-of-a-dropdownmenu-in-shinydashboa