Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a conditional panel to a selectinput in shiny?

I am trying to add a second inputpanel in my shiny application which content depend on the input of the first inputpanel choice, I tried tout use condional panel with no luck.


ui.R

TO <- read.csv("~/TO/TO/TO.csv", sep=";") library(shiny)

shinyUI(fluidPage(

# Application title titlePanel("dasboard"),

# Sidebar with a slider input for number of bins

sidebarLayout(

sidebarPanel(
  selectInput("country", label = h4("Pays"), 
              choices = levels(as.factor(TO$Pays))),
  conditionalPanel(
    condition = "input.country == 'Allemagne'",        
    selectInput("to", label = h4("Tour opérateur"), 
              choices = levels(as.factor(as.character(TO[as.character(TO$Pays)=="Allemagne",]$TO))))),
  conditionalPanel(
    condition = "input.country == 'Angleterre'",        
    selectInput("to", label = h4("Tour Operator"), 
                choices = levels(as.factor(as.character(TO[as.character(TO$Pays)=="Angleterre",]$TO)))))

...


The solution that I found is to create a conditionalPanel for every value of the first inputPanel But is the second inputPanel output is only correct for the first value.

Does anyone have a solution?

like image 739
user2486276 Avatar asked Jan 16 '15 11:01

user2486276


1 Answers

I know the approach below is not via the conditional panels, as I think it would be simpler to do it via examples given below.

First you can use updateSelectInput to update your entries, something like this

rm(list = ls())
library(shiny)
runApp(list(
  ui = bootstrapPage(
    selectInput('data', 'Data', c('mtcars', 'iris')),
    selectInput('Cols', 'Columns', "")
  ),
  server = function(input, output, session){
    outVar <- reactive({
      mydata <- get(input$data)
      names(mydata)
    })
    observe({
      updateSelectInput(session, "Cols",choices = outVar()
      )})
  }
))

Other way you can use renderUI to create the selectInput and populate it like so:

rm(list = ls())
library(shiny)
runApp(list(
  ui = bootstrapPage(
    selectInput('data', 'Data', c('mtcars', 'iris')),
    uiOutput('columns')
  ),
  server = function(input, output){
    output$columns <- renderUI({
      mydata <- get(input$data)
      selectInput('columns2', 'Columns', names(mydata))
    })
  }
))

Edit: how to add multiple widgets inside the renderUI You need to wrap your divs inside the tagList() like so:

rm(list = ls())
library(shiny)
runApp(list(
  ui = bootstrapPage(
    selectInput('data', 'Data', c('mtcars', 'iris')),
    uiOutput('columns')
  ),
  server = function(input, output){
    output$columns <- renderUI({
      mydata <- get(input$data)
      tagList(
      selectInput('columns2', 'Columns', names(mydata)),
      selectInput('columns3', 'Columns 2', names(mydata)))
    })
  }
))
like image 157
Pork Chop Avatar answered Sep 23 '22 04:09

Pork Chop