3

Is it a way to create an shiny observeEvent dependant from shiny subitem selection?

In the following reproductible example, I would like to automaticaly execute button 1 when submenu 1 is clicked and automaticaly execute button 3 when submenu 2 is clicked.

library(shinydashboard)
library(shiny)


ui <- dashboardPage(
 dashboardHeader(title = "Dynamic sidebar"),
 dashboardSidebar(
   sidebarMenuOutput("menu")
 ),
dashboardBody(heigth = 800,  tabItems(
                                     tabItem(tabName = "submenu_1",
                                             fluidRow(
                                               actionButton(inputId = "button_1",label = "Button 1",  icon = icon("fa"),width = '417px'),
                                               actionButton(inputId = "button_2",label = "Button 2",  icon = icon("fa"),width = '417px')
                                             )
                                     ),
                                       tabItem(tabName = "submenu_2",
                                               fluidRow(
                                                 actionButton(inputId = "button_3",label = "Button 3",  icon = icon("fa"),width = '417px'),
                                                 actionButton(inputId = "button_4",label = "Button 4",  icon = icon("fa"),width = '417px')
                                               )
                                       )

                        ),
            textOutput("text")
            )
)


server <- function(input, output) {
output$menu <- renderMenu({
sidebarMenu(
  menuItem("Menu item 1", 
           menuSubItem('Submenu 1',tabName = 'submenu_1',icon = icon('line-chart')),
           menuSubItem('Submenu 2',tabName = 'submenu_2',icon = icon('line-chart'))
           )
)
})


 observeEvent(input$button_1,{output$text <- renderText("Buutton 1 must be selected by default on Submenu 1")})
 observeEvent(input$button_2,{output$text <- renderText("You have selected button 2")})
 observeEvent(input$button_3,{output$text <- renderText("Buutton 3 must be selected by default on Submenu 2 ")})
 observeEvent(input$button_4,{output$text <- renderText("You have selected button 4")})
}

shinyApp(ui, server)

Thanks in advance!

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

5

Is that what you need??

You can add an id argument in sidebarMenu, and then add an observeEvent object triggered by input$sidebarmenu

library(shinydashboard)
library(shiny)


ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenuOutput("menu")
  ),
  dashboardBody(heigth = 800,  tabItems(
    tabItem(tabName = "submenu_1",
            fluidRow(
              actionButton(inputId = "button_1",label = "Button 1",  icon = icon("fa"),width = '417px'),
              actionButton(inputId = "button_2",label = "Button 2",  icon = icon("fa"),width = '417px')
            )
    ),
    tabItem(tabName = "submenu_2",
            fluidRow(
              actionButton(inputId = "button_3",label = "Button 3",  icon = icon("fa"),width = '417px'),
              actionButton(inputId = "button_4",label = "Button 4",  icon = icon("fa"),width = '417px')
            )
    )

  ),
  textOutput("text")
  )
)


server <- function(input, output) {
  output$menu <- renderMenu({
    sidebarMenu(id = "sidebarmenu",
      menuItem("Menu item 1", 
               menuSubItem('Submenu 1',tabName = 'submenu_1',icon = icon('line-chart')),
               menuSubItem('Submenu 2',tabName = 'submenu_2',icon = icon('line-chart'))
      )
    )
  })

  observeEvent(input$sidebarmenu,{
    output$text <- renderText({
      if(input$sidebarmenu=="submenu_1"){
        "Buutton 1 must be selected by default on Submenu 1"
      }else if(input$sidebarmenu=="submenu_2"){
        "Buutton 3 must be selected by default on Submenu 2 "
      }
    })
  })

  observeEvent(input$button_1,{
    output$text <- renderText("Buutton 1 must be selected by default on Submenu 1")
  })
  observeEvent(input$button_2,{
    output$text <- renderText("You have selected button 2")
  })
  observeEvent(input$button_3,{
    output$text <- renderText("Buutton 3 must be selected by default on Submenu 2 ")
  })
  observeEvent(input$button_4,{
    output$text <- renderText("You have selected button 4")
  })

}

shinyApp(ui, server)
2

The trick is to set the parameter id on the UI part.

The code below does the job :

library(shinydashboard)
library(shiny)


ui <- dashboardPage(
    dashboardHeader(title = "Dynamic sidebar"),
    dashboardSidebar(
        sidebarMenu(id="tabs",
            sidebarMenuOutput("menu")
        )
    ),
    dashboardBody(heigth = 800,  tabItems(
        tabItem(tabName = "submenu_1",
                fluidRow(
                    actionButton(inputId = "button_1",label = "Button 1",  icon = icon("fa"),width = '417px'),
                    actionButton(inputId = "button_2",label = "Button 2",  icon = icon("fa"),width = '417px')
                )
        ),
        tabItem(tabName = "submenu_2",
                fluidRow(
                    actionButton(inputId = "button_3",label = "Button 3",  icon = icon("fa"),width = '417px'),
                    actionButton(inputId = "button_4",label = "Button 4",  icon = icon("fa"),width = '417px')
                )
        )

    ),
    textOutput("text")
    )
)


server <- function(input, output) {
    output$menu <- renderMenu({
        sidebarMenu(
            menuItem("Menu item 1", 
                     menuSubItem('Submenu 1',tabName = 'submenu_1',icon = icon('line-chart')),
                     menuSubItem('Submenu 2',tabName = 'submenu_2',icon = icon('line-chart'))
            )
        )
    })

    observeEvent(input$tabs, {
        req(input$tabs)
        if (input$tabs == "submenu_1") {
            # Do whatever you want when submenu_1 is selected
            print("submenu_1 selected")
        } else if (input$tabs == "submenu_2") {
            # Do whatever you want when submenu_2 is selected 
            print("submenu_2 selected")
        }
    })
    observeEvent(input$button_1,{output$text <- renderText("Buutton 1 must be selected by default on Submenu 1")})
    observeEvent(input$button_2,{output$text <- renderText("You have selected button 2")})
    observeEvent(input$button_3,{output$text <- renderText("Buutton 3 must be selected by default on Submenu 2 ")})
    observeEvent(input$button_4,{output$text <- renderText("You have selected button 4")})
}

shinyApp(ui, server)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.