Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the most recently clicked notificationItem of a dropdownmenu in shinydashboard

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?

like image 832
CPhil Avatar asked Dec 15 '25 19:12

CPhil


1 Answers

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.

like image 71
CPhil Avatar answered Dec 17 '25 12:12

CPhil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!