4

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?

Vesnič
  • 365
  • 5
  • 17

1 Answers1

8

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)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • A little late here too, but anyway :) Could you please elaborate a tiny bit on what exactly you did to fix this? For me at least, it took quite a while to discover that you changed `uiOutput` to `menutItemOutput` – Kasper Thystrup Karstensen Sep 15 '20 at 11:07
  • 1
    Exactly that's the difference. The problem with `uiOutput` in this case is, that it creates a `div` tag. However, for the menuItems we need to create a `li` tag. This is what `menutItemOutput` does. You can check the resulting HTML via rightclick -> inpsect. Cheers – ismirsehregal Sep 15 '20 at 11:30
  • @ismirsehregal How would I make the generated `menuItem` disappear, if I clicked on a different radio button? – sedsiv Dec 10 '21 at 14:14
  • You would need to add or exchange the `input$your_different_radiobutton` in the if condition. – ismirsehregal Dec 10 '21 at 15:22