Exercise with Navbar and ConditionalPanel. It doesn't respect the condition


#1

Good evening to everyone. I’m using this code to practice with the layout and in particular with condionalpanels. Selecting “Third” in the Navbar and then “About” in the Navbar Menu I get a mainpanel with two tabs (Italian and Spanish). I would like that selecting “Italian”, in the sidebar will appear only “Select Italian File” and that selecting “Spanish” will appear only “Select Spanish File”. I tried to use the conditionalpanels but in this case they do not work. Can you explain the reason? Thank you.

This is my code:


#Shiny layout Structure


library(shiny)
library(markdown)

ui<-navbarPage("HOME",
           tabPanel("First",
                    sidebarLayout(
            
                    
                      sidebarPanel(width=3, 
                                   
                        # Input: Select a file ----
                        conditionalPanel(
                          'input.dataset === "Curiosity"',
                          fileInput("file1", "Choose CSV File", 
                                    multiple = TRUE,
                                    accept = c("text/csv",
                                               "text/comma-separated-values,text/plain",
                                               ".csv"))),
                    conditionalPanel(
                      'input.dataset=="My Plot"',
                        radioButtons("plotType", "Plot type",
                                     c("Scatter"="p", "Line"="l"))),
                    
                    conditionalPanel(
                      'input.dataset=="Discovery"',
                      radioButtons("Find me", "Find me",
                                   c("In the garden"="g", "Under the table"="t"))),
                    conditionalPanel(
                      'input.dataset=="About the App"',
                      radioButtons("This App is", "This App is",
                                   c("Perfect"="r", "Wow"="w")))),
                
                        
                      
                      mainPanel(
                        tabsetPanel(id='dataset',
                                    tabPanel(("My Plot"),  plotOutput("plot")),
                                    tabPanel(("About the App"), pre("This App is an example")),
                                    tabPanel("Curiosity"),
                                    tabPanel("Discovery"))
                       
                      )
            )),
                    
           
           tabPanel("Second",
                    verbatimTextOutput("summary"))
           ,
           navbarMenu("Third",
                      tabPanel("Table",
                               sidebarLayout(
                                 sidebarPanel(width=3),
                             mainPanel(
                               DT::dataTableOutput("table"))
                      )),
                      tabPanel("About",
                               sidebarLayout(
                                 sidebarPanel(width = 3, pre("Here you can chose your file"),
                                              conditionalPanel(
                                                condition="input.conditionedPanels==Italian",
                                                fileInput("file2", "Choose Italian File", 
                                                          multiple = TRUE,
                                                          accept = c("text/csv",
                                                                     "text/comma-separated-values,text/plain",
                                                                     ".csv")
                                              )),
                                 
                                              conditionalPanel(
                                                condition="input.conditionedPanels==Spanish",
                                     fileInput("file3", "Choose Spanish File", 
                                               multiple = TRUE,
                                               accept = c("text/csv",
                                                          "text/comma-separated-values,text/plain",
                                                          ".csv"))
                                  )),
                                 
                                mainPanel(
                               
                                 tabsetPanel(id='dataset2',
                                             
                                 tabPanel(("Italian"), pre("CIAO!")),
                                 tabPanel(("Spanish"), pre("HOLA!")))
                                
                                
                               
                      )  ))
                    
           )
)





  
 
server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(cars, type=input$plotType)
  })
  
  output$summary <- renderPrint({
    summary(cars)
  })
  
  output$table <- DT::renderDataTable({
    DT::datatable(cars)
  })
}

shinyApp(ui,server)

#2

Hi Bee, I’m new to shiny/R/programming, but I guess maybe this should be “Italian”?

And maybe both “Spanish” and “Italian” should be strings, like in this example?
https://shiny.rstudio.com/reference/shiny/latest/conditionalPanel.html

As in maybe try "input.conditionedPanels=='Italian'" for file2 :slight_smile:


#3

Hi Sowla.
Thank you for finding my mistake but unfortunately it still does not work, even with your suggestion.


#4

Hi @Bee,

From what I can see, @sowla starts us in the right direction.

I got this to work by referring to the id of the tabsetPanel in the conditional. For example, condition="input.dataset2=='Spanish'"

Just to make sure I was getting the values that I wanted, I put an observe() into the server function (old style debugging…)

  observe(
    print(input$dataset2)
  )

Hope this helps!


#5

Thank you so much @ijlyttle!!!