显示/隐藏Shinydashboard中的menuItem

时间:2019-03-28 14:19:05

标签: r shiny shinydashboard shinyjs

进入应用程序后,我需要隐藏一个menuItem。当用户选择某个值时,必须显示menuItem。

我尝试使用shinyjs函数hidden,它隐藏了menuItem,但是当使用showtoggle时,没有出现menuItem。

我找到了R shinydashboard - show/hide multiple menuItems based on user input 并提出了

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)

它可以工作,但是隐藏/显示的项目对齐不正确,或者编码不正确。

有什么想法可以使它变得更好吗?

1 个答案:

答案 0 :(得分:2)

有点晚了,但无论如何: 检查dynamic content上的闪亮仪表板功能。

这应该做到:

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)