0
votes

Minimum reproducible example:

#### global.R
combinations <- data.frame(animal = c(rep('cat', 2),
                                  rep('dog', 3),
                                  rep('horse', 2)),
                       color = c('blue', 
                                 'black', 
                                 'red', 
                                 'red', 
                                 'black',
                                 'red',
                                 'green'),
                       region = c('west',
                                  'west',
                                  'east',
                                  'west',
                                  'east',
                                  'west',
                                  'east'))

Basically, I want to offer an UI option on a shiny app to first choose your animal, and THEN have a 'color' drop-down pop out using only possible values for the animal selected. THEn a 'region' drop-down should pop out under and contain only acceptable values based on the other two.

I did a very basic version but am having trouble how to nest this.

#### ui.R
library(shiny)

shinyUI(fluidPage(

  # Sidebar with dropdown for animal
  sidebarLayout(
    sidebarPanel(
      selectInput("animal",
                  label = "Choose your animal:",
                 choices = unique(combinations$animal)) ,

      wellPanel(uiOutput("ui_1")),

    )
  )
))

Is there a way to avoid manually typing out every possible combination and wrapping it in a 'uiOutput' call? The real file I'm using has approximately 1200 possible combinations.

1

1 Answers

0
votes

You can simply nest uiOutput in the shinyUI and with a renderUI in the shinyServer create the appropriate widgets.

Here i report an example of what you want to do.

ui <- fluidPage(

  # Sidebar with dropdown for animal
  sidebarLayout(
    sidebarPanel(
      selectInput("animal",
                  label = "Choose your animal:",
                  choices = unique(combinations$animal)),
      uiOutput("animal_color")



    ),
    wellPanel(uiOutput("ui_1"))
  )
)

server <- function(input, output, session){
  output$animal_color <- renderUI({
    sel_animal <- input$animal
    col_choices <- combinations$color[which(combinations$animal==input$animal)]
    return(selectInput(inputId="color", label = "choose your color", choices = col_choices))
  })
}

using the same dataframe you used as example:

combinations <- data.frame(animal = c(rep('cat', 2),
                                  rep('dog', 3),
                                  rep('horse', 2)),
                       color = c('blue', 
                                 'black', 
                                 'red', 
                                 'red', 
                                 'black',
                                 'red',
                                 'green'),
                       region = c('west',
                                  'west',
                                  'east',
                                  'west',
                                  'east',
                                  'west',
                                  'east'))

you can do the same adding another uiOutput next to the uiOutput("animal_color") and rendering other widgets.