battousai1983
battousai1983

Reputation: 111

[RShiny][Shinydashboard]Show/Hide different input panel based on another input

I have built shiny App before, but I am getting rusty after not writing the code for a while.

Here is my goal:

I am designing a dynamic UI using shinydashboard. I have one selectInput (e.g., A, B, C and D). What I want to make the app perform is that, based on the user option (either A, B, C or D), it will dynamically pop up another input panel (a panel with specific collection of "numericInput"s or "selectInput"s), as each A/B/C/D option defines different situation.

I could not provide my own code (as I do not have any idea on even how to get started), but I find some page with example code here:

An example of dynamic UI

It is the example in section 10.2.1 of the page using tabsetPanel and updateTabsetPanel.

I checked their app link and it seems to be exactly what I want, but I suspect that some of the code has been depleted by Shiny already as when I copy and paste the code to my R script, I could not run it without error.

If you could kindly help, either on how to properly adjust the code on that page, or if there is any other approach, it will be greatly appreciated.

Upvotes: 0

Views: 605

Answers (1)

Pabort
Pabort

Reputation: 348

Easiest way would be using uiOutput and renderUI.

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("controller", "Show", choices = c("A", "B", "C", "D")),
      uiOutput("newSelect")
    ),
    mainPanel(
      textOutput("textId")
    )
  )
)

server <- function(input, output, session) {
  
  output$textId <- renderText({
    input$controller
  })
  
  output$newSelect <- renderUI({
    if(input$controller == "B"){
      selectInput("controller2", "New Select", choices = c(1:4))
    } else {
      NULL
    }
  })
  
}


shinyApp(ui,server)

Bare in mind this can make your app laggish if it gets too complex. If so, you can use some js to show or hide the other selectInputs conditionally

Upvotes: 0

Related Questions