Show/hide menuItem in shinydashboard

2019-06-01 11:25发布

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?

1条回答
何必那么认真
2楼-- · 2019-06-01 11:43

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)
查看更多
登录 后发表回答