Show/hide menuItem in shinydashboard

拟墨画扇 提交于 2019-12-08 05:06:16

问题


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:


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)


来源:https://stackoverflow.com/questions/55399905/show-hide-menuitem-in-shinydashboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!