Menu Item click is not triggering in R Shiny Dashboard

...I am building R Shiny Dashboard. I have built the code UI part. Now I want to add the content of menu item that should display on right side dynamically. I have applied observerevent for tab item. But it is not working properly like below:

Code:

library(shiny)
library(shinythemes)


#UI client..............................

ui <- fluidPage(
  tags$style(HTML("
              h4 {
              color: white;
              padding-top: 5px;
              font-weight: bold;
              }
              .content-wrapper{
              overflow:auto;
              width:150%;
              }
              
              "
)
),
dashboardPage(

  dashboardHeader(title="OPTUM",
                tags$li(h4("Welcome User!"),
                        class = "dropdown",
                        dropdownMenu(type = "messages",
                                     messageItem(
                                       from = "Logout",
                                       message = "Session Ends"
                                     ))
                        
                )),
## Sidebar content

dashboardSidebar(
  sidebarMenu(
    menuItem("OverView", tabName = "Sitecontent", icon = icon("th")),
    menuItem("Candidate Information", tabName = "CandidateInformation", icon = icon("th")),
    menuItem("Assesment", tabName = "Assesment", icon = icon("th"),
             
             menuItem('Affect',
                      tabName = 'Affect',
                      icon = icon('th'))
    )
  )
),
dashboardBody(
  tabItems(
    # OverView tab  content
    tabItem(tabName = "OverView",
            h2("OverView ")
            
    ),
    
    # Assesment tab content
    tabItem(tabName = "Assesment",
            h2("Assesment ")
            
            
    ),
    
    # Predictive Result tab content
    tabItem(tabName = "PredictAny",
            h2("PredictAny")
            
    ),
    
    # PreventiveResults tab content
    tabItem(tabName = "PreventiveResults",
            h2("PreventiveResults")
    ),
    tabItem(tabName = "Affect",
            h2("Assesment on Affect")
    )
  )
 )
 )
)

 server <- function(input, output,session) { 
   observeEvent(input$Affect, {  #Not triggering
     sendSweetAlert(
        session = session,
        title = "Done!",
        text = "Affect Triggered.",
        type = "success"
      )
  })


  }
  shinyApp(ui, server)

I am not able to hit observeEvent(input$Affect,. Not sure what I am doing wrong here.. please help me on this.

So this issue is a menu item can't be observed in the same way you observe say an action button click with input$button_id.

To tell the server what menu item has been selected you first have to give your sidebarMenu an id then use an if condition inside an observer to only run the code when tab Affect has been selected.

Something like this:

In your UI

dashboardSidebar(
  sidebarMenu(
    id = "tabs",
    menuItem("OverView", tabName = "Sitecontent", icon = icon("th")),
    menuItem("Candidate Information", tabName = "CandidateInformation", icon = icon("th")),
    menuItem("Assesment", tabName = "Assesment", icon = icon("th")),
    menuItem('Affect',tabName = 'Affect', icon = icon('th'))
  )
),

In your server

observeEvent(input$tabs, { 
  if (input$tabs == "Affect") {
    sendSweetAlert(
      session = session,
      title = "Done!",
      text = "Affect Triggered.",
      type = "success"
    )
  }
})

I also noticed you're using sendSweetAlert() from the shinyWidgets package but don't have the package loaded at the top of your script so be sure to add library(shinyWidgets) up there!

5 Likes

Hey, this could be done by assigning an id to sidebarMenu and using an Observer at server. Below is the working example:

library(shiny)
library(shinythemes)


#UI client..............................

ui <- fluidPage(
  tags$style(
    HTML(
      "
      h4 {
      color: white;
      padding-top: 5px;
      font-weight: bold;
      }
      .content-wrapper{
      overflow:auto;
      width:150%;
      }
      
      "
    )
    ),
  dashboardPage(
    dashboardHeader(title = "OPTUM",
                    tags$li(
                      h4("Welcome User!"),
                      class = "dropdown",
                      dropdownMenu(type = "messages",
                                   messageItem(from = "Logout",
                                               message = "Session Ends"))
                      
                    )),
    ## Sidebar content
    
    dashboardSidebar(sidebarMenu(
      id = "sbMenu",
      menuItem("OverView", tabName = "Sitecontent", icon = icon("th")),
      menuItem(
        "Candidate Information",
        tabName = "CandidateInformation",
        icon = icon("th")
      ),
      menuItem(
        "Assesment",
        tabName = "Assesment",
        icon = icon("th"),
        menuItem('Affect', tabName = 'Affect', icon = icon('th'))
      )
    )),
    dashboardBody(tabItems(
      # OverView tab  content
      tabItem(tabName = "OverView",
              h2("OverView ")),
      
      # Assesment tab content
      tabItem(tabName = "Assesment",
              h2("Assesment ")),
      
      # Predictive Result tab content
      tabItem(tabName = "PredictAny",
              h2("PredictAny")),
      
      # PreventiveResults tab content
      tabItem(tabName = "PreventiveResults",
              h2("PreventiveResults")),
      tabItem(tabName = "Affect",
              h2("Assesment on Affect"))
    ))
  )
    )

server <- function(input, output, session) {
  
  observe({
    if (input$sbMenu == 'Affect') {
      sendSweetAlert(
        session = session,
        title = "Done!",
        text = "Affect Triggered.",
        type = "success"
      )
    }
  })
  
}
shinyApp(ui, server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.