Sanjay
Sanjay

Reputation: 1236

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="Title",
                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.

Upvotes: 1

Views: 1696

Answers (1)

Pork Chop
Pork Chop

Reputation: 29407

Try this:

library(shiny)
library(shinythemes)
library(shinydashboard)

#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 = "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 = 'AffectTab',
                                    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 = "AffectTab",
                h2("Assesment on Affect")
        )
      )
    )
  )
  )

server <- function(input, output,session) { 

  observeEvent(input$tabs, {  #Not triggering
    print(input$tabs)
    if(input$tabs=="AffectTab"){
      sendSweetAlert(
        session = session,
        title = "Done!",
        text = "Affect Triggered.",
        type = "success"
      )
    }
  })
}
shinyApp(ui, server)

Upvotes: 2

Related Questions