Dieter Köberl
Dieter Köberl

Reputation: 711

Shiny: create a selectInput after choosing a value of another selectInput

I have a shiny application where I want to build a conditional query system on a data frame.

First, I want to have a selectInput showing all available tables. After the user has chosen a table, I want another box to appear where he can select the column name he wants to filter for. This is what I have until now:

ui.r:

library(shiny)
library(shinydashboard)

source("global_variables.r")

ui=dashboardPage(

  dashboardHeader(title="Test"),
  dashboardSidebar(

    sidebarMenu(

      menuItem("Conditionals",tabName = "conditionals")

    )

  ),
  dashboardBody(

    tabItems(

      tabItem(tabName="conditionals",
              fluidRow(

                box(

                  title = "Conditionals",
                  width = 4,
                  selectInput("Con_tableName",choices=c("NONE",tableNames),label = "Table Name"),
                  tags$div(id = 'placeholder') 


                )

              )

      )

    )

  )

)

server.r:

library(shiny)

source("global_variables.r", local = FALSE)

Table1=data.frame();

shinyServer(
  function(input, output,session) {



    observe({
      reactive(
        if(input$Con_tableName!="NONE"){

          insertUI( selector="#placeholder",
                    ui={
                      selectInput("Con_colName",choices=c("NONE",colnames(dynGet(input$Con_tableName))),label = "Column Name")
                    }
          )
        }
      )
    })
  }
)

global_variables.r:

tableNames=c("Table1","Table2","Table3")

The problem is, that if I choose a value in the selectInput, observe doesnt get fired.

EDIT: According to BigDataScientists comment,changed insertUI to renderUI. Updated files:

ui.r:

library(shiny)
library(shinydashboard)

source("global_variables.r")

ui=dashboardPage(

  dashboardHeader(title="Test"),
  dashboardSidebar(

    sidebarMenu(

      menuItem("Conditionals",tabName = "conditionals")

    )

  ),
  dashboardBody(

    tabItems(

      tabItem(tabName="conditionals",
              fluidRow(

                box(

                  title = "Conditionals",
                  width = 4,
                  selectInput("Con_tableName",choices=c("NONE",tableNames),label = "Table Name"),
                  uiOutput("conditionalUI")


                )

              )
      )

    )

  )
)

server.r:

library(shiny)

source("global_variables.r", local = FALSE)

Table1=data.frame();

shinyServer(
  function(input, output,session) {



    observeEvent(input$Con_tableName,{
      reactive(
        if(input$Con_tableName!="NONE"){

          output$conditionalUI=renderUI({

           selectInput("Con_colName",choices=c("NONE",colnames(input$Con_tableName)),label = "Column Name")

          })

        }
      )
    })
  }
)

Upvotes: 0

Views: 2163

Answers (1)

dvarelas
dvarelas

Reputation: 988

You can use conditionalPanel(). Below there is a small example which might work in your case.

library(shiny)

shinyApp(
  ui <- fluidPage(
    mainPanel(
      selectInput("input1", "Select something", choices = c('','1','2','3')),
      conditionalPanel("input.input1!=''",
                       selectInput('input2', "Select something else", choices = c('4','5')))
    )
  ),
  server <- function(input, output){}
)

Upvotes: 2

Related Questions