简体   繁体   中英

Show/hide menuItem in shinydashboard

I need a menuItem hidden, when the app is entered into. When a user chooses a certain value, the menuItem has to appear.

I have tried shinyjs functions hidden , and it hides a menuItem, but when using show or toggle , a menuItem doesn't appear.

I've found R shinydashboard - show/hide multiple menuItems based on user input and came up with this

library(shiny)
library(shinydashboard)
library(shinyjs)



header <- dashboardHeader(title = "APP", titleWidth = 330)

sidebar <- dashboardSidebar(
  sidebarMenu(id="tabs",
              menuItem("",tabName="default"),
              menuItem("Scenarios",tabName = "scenarios", icon = icon("flag")),
              uiOutput("recOpt"),
              menuItem("Simulation", tabName = "game", icon = icon("gamepad")), 
              menuItem("Actions", tabName = "actions", icon = icon("folder"),
                              menuSubItem("Save project", tabName = "save"),
                              menuSubItem("Open project", tabName = "open")
                       )
              )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "scenarios",
            useShinyjs(),
            radioButtons("radio", h3("Radio buttons"),
                         choices = list("Choice 1" = 1,
                                        "Choice 2" = 2,
                                        "Choice 3" = 3))
    )
  )
)


ui <- dashboardPage(header, sidebar, body)


server <- function(input, output) {
  output$recOpt <- renderUI({
      if(input$radio == 2)
        menuItem("Options", tabName = "recOpt", icon = icon("bell"),
                 menuSubItem("No option",tabName="RO_00"),
                 menuSubItem("Option 1",tabName="RO_01")
        )
  })
}

shinyApp(ui, server)

It works but the hidden/shown item is not aligned correcty, nor the encoding is correct.

Have any ideas how to make it better?

A little late, but anyway: Check the shinydashboard capabilities on dynamic content .

This should do it:

library(shiny)
library(shinydashboard)
library(shinyjs)

header <- dashboardHeader(title = "APP", titleWidth = 330)

sidebar <- dashboardSidebar(
  sidebarMenu(id="tabs",
              menuItem("",tabName="default"),
              menuItem("Scenarios",tabName = "scenarios", icon = icon("flag")),
              menuItemOutput("recOpt"),
              menuItem("Simulation", tabName = "game", icon = icon("gamepad")), 
              menuItem("Actions", tabName = "actions", icon = icon("folder"),
                       menuSubItem("Save project", tabName = "save"),
                       menuSubItem("Open project", tabName = "open")
              )
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "scenarios",
            useShinyjs(),
            radioButtons("radio", h3("Radio buttons"),
                         choices = list("Choice 1" = 1,
                                        "Choice 2" = 2,
                                        "Choice 3" = 3))
    )
  )
)

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {
  output$recOpt <- renderMenu({
    if(input$radio == 2)
      menuItem("Options", tabName = "recOpt", icon = icon("bell"),
               menuSubItem("No option",tabName="RO_00"),
               menuSubItem("Option 1",tabName="RO_01")
      )
  })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM