0

Im building a shiny app. when I include a conditionalPanel in my sideBar menu, I no longer see anything in my Dashboard body. Here is my code:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title="TEST"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("tab1", tabName="tab_number1", icon=icon("arrow-right"),
               conditionalPanel("input.sidebar==='tab_number1",
                                selectInput(inputId="selection",label = "Select country:",choices ="", selected=NULL))
               
      )
    )
  ),
  
  ## Body content
  dashboardBody(
    tabItems(
      #intro tab
        tabItem(tabName="tab_number1",
              fluidPage(
                fluidRow(
                  h2("This text should appear here!!"),
                  uiOutput("up"),
                  plotOutput(outputId="plotnumber1"),br(),
                  ))
      )
    )
  )
)

server <- function(input, output) { 
  
}
shinyApp(ui, server=server)

When I click in tab1 I don't see the text that I have in the dashboardBody. If I remove the conditional Panel in the sidebarMenu I am able to see the text.

Any ideas of what im doing wrong?

1 Answers1

0

When you have child items in a menuItem, you need to define menuSubItem to display the page/output. Also, you do not need a conditional Panel as it is by default. Try this

choices <- names(mtcars[-1])

ui <- dashboardPage(
  dashboardHeader(title="TEST"),
  dashboardSidebar(
    sidebarMenu(id="sidebar",
      menuItem("Home", tabName = "home", icon = icon("home")),
      menuItem("tab1", tabName="tab_1", icon=icon("arrow-right"),
               selectInput(inputId="selection",label = "Select country:",choices = choices, selected=NULL),
               menuSubItem("My Plot", tabName="tab_1plot",
                           icon = icon("line-chart"))
      ),
      menuItem("tab2", tabName="tab_2", icon = icon("arrow-right")),
      menuItem("tab3", tabName="tab_3", icon = icon("arrow-right"))
    )
  ),
  
  ## Body content
  dashboardBody(
    tabItems(
      #intro tab
      tabItem(tabName="tab_1plot",
              #fluidPage(
                fluidRow(
                  h2("This text should appear here!!"),
                  #uiOutput("up"),
                  plotOutput(outputId="plotnumber1")
                )#)
      ),
      tabItem(tabName="tab_2", fluidRow(plotOutput("p2"))
      ),
      tabItem(tabName="tab_3", fluidRow(plotOutput("p3"))
      )
    )
  )
)

server <- function(input, output,session) { 
  observe({print(input$sidebar)})
  output$plotnumber1 <- renderPlot(plot(mtcars[,c("mpg",input$selection)]))
  output$p2 <- renderPlot(plot(cars))
  output$p3 <- renderPlot(plot(pressure))
}
shinyApp(ui, server=server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thank you for your answer. I tried the code, but is not working. When I lick Tab1, I dont see anything. I have to click on the subMenu "My plot" in order to see what I have. I want that everything appears when you click the tab1 only. – Juan Pablo Rincon Mar 28 '23 at 16:50
  • Please check the output of `input$sidebar` in your console, when you select different tabs. – YBS Mar 28 '23 at 16:57