0

I have R Shiny application with two UI selectors where each one has a big list of items. I know how to make server-side element for one element. But how to make two related server-side 'selectInput' elements. So, when exist a change in one element - another should reflect appropriately.

Example.

  1. The first list has 97,310 elements.

  2. The second list should have:

2.1. for 1st element "Aaban" - 2 related values (10, 11)

2.2. for 2nd element "Aabha" - 1 related value (12)

2.3. for 3rd element "Aabid" - 3 related values (13, 14, 15)

Here is R code.

library(shiny)
library(dplyr)
library(babynames)

# 1. Data sets

# 1.1. First data set (list)
list_names <- babynames::babynames %>% 
  distinct(name) %>%
  pull(name) %>%
  sort()

# 1.2. Second data set (data frame) for 3 first 'names' elements
df_ages <- data.frame(
  name = c("Aaban", "Aaban", "Aabha", "Aabid", "Aabid", "Aabid"),
  age  = c(10, 11, 12, 13, 14, 15))

# 2. UI
ui <- fluidPage(
  fluidRow(selectInput("si_name", "Name", multiple = FALSE, choices = character(0))),
  fluidRow(selectInput("si_age", "Age", multiple = FALSE, choices = character(0))))

# 3. Server
server <- function(input, output, session) {
  updateSelectizeInput(session, "si_name", choices = list_names, server = TRUE)
  updateSelectizeInput(session, "si_age", choices = df_ages$age, server = TRUE)
}

# 4. App
shinyApp(ui, server)

Thanks!

Andrii
  • 2,843
  • 27
  • 33
  • 1
    What do you mean by "related values" ? – Stéphane Laurent Jun 03 '19 at 18:39
  • By "related" values I mean the following - when user select "Aaban" in "Name" - the second UI element "Age" should have only 2 possible values "10, 11". Now it has all "age" values. – Andrii Jun 03 '19 at 18:43
  • Ok. What is `list_names` ? Is it the names in `df_ages` ? – Stéphane Laurent Jun 03 '19 at 18:45
  • Yes. The "list_names" has unique names. The "df_ages" data frame has "ages" for each name. So, one name can have more than one ages. The issue to show in the second selector only "related" ages - not all. – Andrii Jun 03 '19 at 18:50

1 Answers1

3

I think you want:

# 2. UI
ui <- fluidPage(
  fluidRow(selectInput("si_name", "Name", multiple = FALSE, 
                       choices = unique(df_ages$name))),
  fluidRow(selectInput("si_age", "Age", multiple = FALSE, choices = character(0)))
)

# 3. Server
server <- function(input, output, session) {

  observeEvent(input$si_name, {
    ages <- df_ages[df_ages$name == input$si_name, "age"]
    updateSelectizeInput(session, "si_age", choices = ages, server = TRUE)
  })

}
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • 2
    @andrii Nice. Could you please [accept the answer](https://stackoverflow.com/help/someone-answers) ? – Stéphane Laurent Jun 03 '19 at 19:06
  • Yes, sure. Did it. Thanks! – Andrii Jun 03 '19 at 19:38
  • @StéphaneLaurent please, any chance I could ping your brain on https://stackoverflow.com/questions/71228067/r-shiny-selectizeinput-not-working-with-updateselectizeinput/71229020?noredirect=1#comment125923590_71229020 ? thanks – Angelo Feb 23 '22 at 21:08
  • @StéphaneLaurent do you have any input on a similar issue here? [link](https://stackoverflow.com/questions/73735865/how-do-i-use-input-from-selectizeinput-to-filter-a-list-of-options-and-then-u) – JJ Fantini Sep 15 '22 at 18:44