Home > Blockchain >  Passing filtered unique values back to a Shiny dropdown box
Passing filtered unique values back to a Shiny dropdown box

Time:09-29

I have some data which looks like:

   provincia municipio                
   <chr>     <chr>                    
 1 Alicante  Alacantí                 
 2 Madrid    Zona Suroeste            
 3 Valencia  Valencia, Zona de        
 4 Cádiz     Campo de Gibraltar       
 5 Barcelona Maresme                  
 6 Madrid    Madrid, Zona de          
 7 Almería   Levante Almeriense       
 8 Sevilla   Sevilla capital y entorno
 9 Tarragona Baix Penedès             
10 Cádiz     Bahía de Cádiz  

I want to create a series of dropdown boxes.

  1. which filters on the provincia column first, say, Madrid - then the data will be filtered down to just show the municipio results for Madrid - which will be passed to the second dropdown.

How can I use filtered dropdowns?

Data:

data = structure(list(provincia = c("Alicante", "Madrid", "Valencia", 
"Cádiz", "Barcelona", "Madrid", "Almería", "Sevilla", "Tarragona", 
"Cádiz", "Cádiz", "Zaragoza", "Málaga", "Cádiz", "Alicante", 
"Barcelona", "Valencia", "Córdoba", "Cádiz", "Madrid", "Barcelona", 
"Madrid", "Málaga", "Gipuzkoa", "Madrid", "Girona", "Tarragona", 
"Tarragona", "Jaén", "Barcelona", "Tarragona", "Tarragona", 
"Tarragona", "Tarragona", "Granada", "Granada", "Tarragona", 
"Tarragona", "Madrid", "Tarragona", "Pontevedra", "Tarragona", 
"Málaga", "Alicante", "Tarragona", "León", "Alicante", "Valencia", 
"Málaga", "Toledo", "Málaga", "Tarragona", "Granada", "Alicante", 
"Madrid", "Málaga", "Almería", "Madrid", "Madrid", "Cádiz", 
"Cádiz"), municipio = c("Alacantí", "Zona Suroeste", "Valencia, Zona de", 
"Campo de Gibraltar", "Maresme", "Madrid, Zona de", "Levante Almeriense", 
"Sevilla capital y entorno", "Baix Penedès", "Bahía de Cádiz", 
"Bahía de Cádiz", "Zaragoza, Zona de", "Costa del Sol Occidental - Zona de Estepona", 
"Bahía de Cádiz", "Vega Baja", "Barcelonès", "La Safor", "Córdoba, Zona de", 
"Bahía de Cádiz", "Madrid, Zona de", "Anoia", "Madrid, Zona de", 
"Costa del Sol Occidental - Zona de Estepona", "Donostialdea - Oarsoldea", 
"Madrid Sureste - Cuenca Tajuña", "La Selva", "Tarragonès", 
"Tarragonès", "Jaén capital y entorno", "Garraf", "Tarragonès", 
"Tarragonès", "Tarragonès", "Tarragonès", "Vega de Granada", 
"Vega de Granada", "Tarragonès", "Baix Camp", "Madrid, Zona de", 
"Baix Camp", "Comarca de Vigo", "Baix Camp", "Costa del Sol Occidental - Zona de Estepona", 
"Alacantí", "Baix Camp", "El Bierzo", "Marina Baixa", "La Safor", 
"Costa del Sol Occidental - Zona de Benalmádena", "La Mesa de Ocaña", 
"Costa del Sol Occidental - Zona de Estepona", "Baix Camp", "Vega de Granada", 
"Alacantí", "Zona Noroeste", "Costa del Sol Occidental - Zona de Benalmádena", 
"Poniente Almeriense", "Zona Sur de Madrid", "Madrid, Zona de", 
"Bahía de Cádiz", "Campo de Gibraltar")), row.names = c(NA, 
-61L), class = c("tbl_df", "tbl", "data.frame"))

Shiny App:

library(shiny)

main = unique(data$provincia)


data %>% 
  filter(provincia == "Madrid")
####################################################################################################
ui <- fluidPage(
  selectInput("MAIN", "Select the MAIN region", choices = main),
  selectInput("subMAIN", "Select the sub-MAIN region", choices = output$filteredMAIN)
    
)


server <- function(input, output) {
  eventReactive({
    filteredMAIN = data %>% 
      filter(provincia == .data[[input$MAIN]]) %>% 
      select(municipio) %>% 
      distinct() %>% 
      pull()
  })
}


shinyApp(ui = ui, server = server)

EDIT: Attempt 2

library(shiny)

data = read_csv("/run/media/bscuser/A34E-C6B8/inmobiliarioProject2/comprar/propertyPageData/propertyData_2022_09_26.csv") %>% 
  select(geoLocationName) %>% 
  separate(
    col = geoLocationName,
    into = c("provincia", "municipio", "distrito", "zona"),
    sep = "\\|"
  ) %>% 
  drop_na()
####################################################################################################



ui <-  fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("MAIN", label = "Select MAIN Data", choices = unique(data$provincia)),
      selectInput("select_var", label = "Select Variable", choices = output$varnames )
    )
    # mainPanel(
    #   tableOutput("table")
    #)
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$MAIN,{
    updateSelectInput(session, "select_var", choices = names(input$select_data))
    
  })
  
  output$varnames <- reactive({
    filteredMAIN = data %>% 
      filter(provincia == .data[[input$MAIN]]) %>% 
      select(municipio) %>% 
      distinct() %>% 
      pull()
  })
  
  # output$table <- renderTable({
  #   head(varnames[[input$select_var]])
  # })
}


shinyApp(ui = ui, server = server)

CodePudding user response:

Does this give you something close to what you want. It's confusing because you seem to want to select municipalities within provinces, but the labelling of your input widgets refers to variables...

library(shiny)
library(tidyverse)

# Definition deleted for brevity
# data <- ...

ui <-  fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("MAIN", label = "Select MAIN Data", choices = c()),
      selectInput("select_var", label = "Select Variable", choices = c() )
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$MAIN,{
    updateSelectInput(
      session, 
      "select_var", 
      choices = data %>% 
                  filter(provincia == input$MAIN) %>% 
                  select(municipio) %>% 
                  unique() %>% 
                  pull(municipio)
    )
  })
  
  observe({
    updateSelectInput(
      session, 
      "MAIN", 
      choices=data %>% 
                select(provincia) %>% 
                unique() %>% 
                pull(provincia)
    )
  })
}

shinyApp(ui = ui, server = server)

This gives, for example,

enter image description here

and

enter image description here

  • Related