R Shiny中的“动态”下拉菜单仅在单击时更新

2ic8powd  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(139)

我有一个应用程序,其中有一些下拉菜单,根据用户选择(哪个位置)更新

但是,当其中一个水平具有NA值时,表中不显示任何内容。

我想做两件事:
1.如果表在给定区域中具有NA值,则使用以前的级别结果填充该表。例如,在第二个映像中,它将具有Alcaraz的表,因为Select Zona Variable中具有NA
我试过做像filter(str_detect(distrito, if_else(input$distrito == "NA", "", input$distrito)))这样的事情,但没有运气。目标是如果有NA值,就什么都不过滤。
1.仅在单击下一个下拉菜单时激活它。(如果这是一个新问题,请告诉我)-想法是首先为Gipuzkoa制作一个表,只有在选择了Select Municipio Variable时才更新该表,然后也对Select Distrito Variable执行相同的操作。我这里的主要问题是应用程序应用了所有的过滤器,我想先看看X1 M10 N1 X的汇总统计数据和其他表格,然后再看看X1 M11 N1 X,然后是Distrito,最后是Zona-目前我的表直接跳到zona的过滤器。
数据:

data = structure(list(purchase_price = c(207000, 65000, 62000, 2e+05, 
293000, 80000, 96500, 119500, 149999, 144000, 298000, 135000, 
310000, 285000, 269000, 120000, 595000, 355000, 96000, 490000, 
195000, 235000, 197000, 70000, 215000, 169000, 124900, 195000, 
185000, 190000, 390348, 113500, 295000, 299995, 156000, 195000, 
185000, 260000, 370000, 180000, 105000, 249000, 390000, 295000, 
86999, 219900, 264999, 56800, 179900, 150000, 145000, 168500, 
160000, 180000, 168000, 42300, 119000, 350000, 390000, 110000, 
420000, 154000, 429000, 85000, 259000, 495000, 170000, 102490, 
469000, 245000, 138000, 127000, 1390000, 320000, 420000, 292000, 
87500, 120000, 475000, 170000, 61000, 255000, 49000, 226000, 
220000, 3e+05, 30000, 265000, 330000, 220000, 220000, 139000, 
880000, 75000, 220000, 76400, 150000, 46000, 25000, 170000), 
    provincia = c("Gipuzkoa", "Valencia", "Valencia", "Valencia", 
    "Málaga", "Araba - Álava", "Tarragona", "Jaén", "Tarragona", 
    "Barcelona", "Barcelona", "Alicante", "Granada", "Málaga", 
    "Barcelona", "Tarragona", "Tarragona", "Barcelona", "Valencia", 
    "Tarragona", "Castellón", "Segovia", "Alicante", "Tarragona", 
    "Málaga", "Girona", "Cantabria", "Barcelona", "Barcelona", 
    "Barcelona", "Barcelona", "Sevilla", "Granada", "Barcelona", 
    "Barcelona", "Cáceres", "Barcelona", "Valencia", "Gipuzkoa", 
    "Santa Cruz de Tenerife", "Tarragona", "Almería", "Alicante", 
    "Granada", "Tarragona", "Toledo", "Tarragona", "Huelva", 
    "Castellón", "Albacete", "Madrid", "Girona", "Castellón", 
    "Zaragoza", "Madrid", "Alicante", "Barcelona", "Barcelona", 
    "Sevilla", "Castellón", "Valencia", "Málaga", "Alicante", 
    "Lleida", "Girona", "Madrid", "Alicante", "Pontevedra", "Barcelona", 
    "Illes Balears", "Málaga", "A Coruña", "Barcelona", "Barcelona", 
    "Barcelona", "Málaga", "Cádiz", "Valencia", "Barcelona", 
    "Toledo", "Castellón", "Barcelona", "Huelva", "Barcelona", 
    "Tarragona", "A Coruña", "Ciudad Real", "Illes Balears", 
    "Ourense", "Barcelona", "Barcelona", "Málaga", "Málaga", 
    "Córdoba", "Tarragona", "Castellón", "Valencia", "Castellón", 
    "Navarra", "Cádiz"), municipio = c("Bajo Bidasoa", "Valencia, Zona de", 
    "Valencia, Zona de", "Valencia, Zona de", "Costa del Sol Occidental - Zona de Estepona", 
    "Laguardia - Rioja Alavesa", "Tarragonès", "Campiña de Jaén", 
    "Tarragonès", "Maresme", "Maresme", "Marina Baixa", "Vega de Granada", 
    "Costa del Sol Occidental - Zona de Estepona", "Maresme", 
    "Tarragonès", "Tarragonès", "Barcelonès", "Horta Nord", 
    "Tarragonès", "Plana Baixa", "Cuéllar, Zona de", "Alacantí", 
    "Baix Camp", "Costa del Sol Occidental - Zona de Benalmádena", 
    "La Selva", "Costa Oriental", "Vallès Oriental", "Vallès Oriental", 
    "Vallès Oriental", "Maresme", "Sierra Norte", "Vega de Granada", 
    "Vallès Occidental", "Baix Llobregat Sud", "Llanos de Cáceres", 
    "Barcelonès", "La Safor", "Donostialdea - Oarsoldea", "Tenerife", 
    "Tarragonès", "Almería capital y entorno", "Alacantí", 
    "Vega de Granada", "Tarragonès", "Los Montes de Toledo", 
    "Tarragonès", "Huelva capital y entorno", "Plana Alta", 
    "Sierra de Alcaraz - Campo de Montiel", "Zona Sur de Madrid", 
    "La Selva", "Plana Alta", "Zaragoza, Zona de", "Madrid, Zona de", 
    "Vega Baja", "Barcelonès", "Bages", "Sevilla capital y entorno", 
    "Plana Alta", "Valencia, Zona de", "Costa del Sol Occidental - Zona de Estepona", 
    "Marina Alta", "Segrià", "Alt Empordà", "Madrid, Zona de", 
    "Marina Baixa", "Comarca de Vigo", "Vallès Occidental", 
    "Mallorca", "Costa del Sol Occidental - Zona de Estepona", 
    "Comarca de Ferrol", "Vallès Occidental", "Osona", "Osona", 
    "Costa del Sol Occidental - Zona de Estepona", "La Janda", 
    "Ribera Alta (Valencia)", "Osona", "Toledo, Zona de", "Plana Alta", 
    "Osona", "Huelva capital y entorno", "Vallès Oriental", 
    "Baix Penedès", "Comarca de Ferrol", "Alcudia (Ciudad Real)", 
    "Mallorca", "Comarca de Ourense", "Vallès Occidental", "Vallès Occidental", 
    "Costa del Sol Occidental - Zona de Estepona", "Málaga capital y entorno", 
    "La Subbética", "Baix Penedès", "Plana Alta", "Valencia, Zona de", 
    "Plana Baixa", "Comarca de Pamplona", "Campiña de Jerez"
    ), distrito = c("Irun", "Valencia Capital", "Valencia Capital", 
    "Valencia Capital", "Estepona", "Navaridas", "Salou", "Marmolejo", 
    "Salou", "Mataró", "Dosrius", "Benidorm", "Granada Capital", 
    "Manilva", "Dosrius", "Roda de Berà", "Roda de Berà", "Barcelona Capital", 
    "Puig", "Tarragona Capital", "Vila-real", "Marugán", "San Vicente del Raspeig / Sant Vicent del Raspeig", 
    "Mont-roig del Camp", "Benalmádena", "Anglès", "Laredo", 
    "Granollers", "Granollers", "Granollers", "Cabrils", "El Ronquillo", 
    "Cenes de la Vega", "Santa Perpètua de Mogoda", "Sant Boi de Llobregat", 
    "Cáceres Capital", "Barcelona Capital", "Barx", "Donostia - San Sebastián", 
    "Tacoronte", "Salou", "Almería Capital", "El Campello", 
    "Albolote", "Salou", "Nambroca", "Salou", "Huelva Capital", 
    "Castellón de la Plana / Castelló de la Plana", "Alcaraz", 
    "Fuenlabrada", "Riells i Viabrea", "Castellón de la Plana / Castelló de la Plana", 
    "Zaragoza Capital", "Madrid Capital", "Torrevieja", "Badalona", 
    "Castellgalí", "Sevilla Capital", "Cabanes", "Valencia Capital", 
    "Estepona", "Dénia", "Lleida Capital", "Roses", "Madrid Capital", 
    "L'Alfàs del Pi", "Vigo", "Sabadell", "Palma de Mallorca", 
    "Estepona", "Fene", "Cerdanyola del Vallès", "Vic", "Vic", 
    "Estepona", "Vejer de la Frontera", "Senyera", "Vic", "Toledo Capital", 
    "Borriol", "Santa Eugènia de Berga", "Huelva Capital", "Sant Celoni", 
    "Calafell", "Fene", "Almadén", "Palma de Mallorca", "Ourense Capital", 
    "Terrassa", "Terrassa", "Estepona", "Málaga Capital", "Lucena", 
    "Calafell", "Castellón de la Plana / Castelló de la Plana", 
    "Valencia Capital", "Onda", "Pamplona / Iruña", "Jerez de la Frontera"
    ), zona = c("Pinar - Anaka - Belaskoenea", "Els Orriols", 
    "Barrio de Benicalap", "Barrio de Benimaclet", "Parque Central", 
    NA, "Mar i Camp - Platja dels Capellans", NA, "Platja de Llevant", 
    "Cerdanyola Sud", "Can Massuet del Far", "Levante Alto", 
    "Centro - Sagrario", "Manilva Pueblo", "Canyamars", NA, NA, 
    "Vilapicina i la Torre Llobeta", "El Puig", "Llevant", "Centro", 
    NA, "Centro", "Poble", "Zona Centro Comercial Torrequebrada", 
    NA, "Zona Playa", "Lledoner", "Lledoner", "Lledoner", NA, 
    NA, NA, NA, "Casablanca", "Mejostilla", "El Poble Sec - Parc de Montjuïc", 
    NA, "Amara Zaharra - Arbaizenea", "Campo de Golf - Agua García - Juan Fernández", 
    "Platja de Llevant", "Plaza de Toros - Santa Rita", "Playa Muchavista", 
    NA, "Mar i Camp - Platja dels Capellans", NA, "Centre", "Tres Ventanas", 
    "El Grao", NA, "El Naranjo", NA, "Oeste", "La Magdalena", 
    "Recoletos", "Zona Carrefour - Urbanizaciones", "Sant Roc", 
    NA, "Encarnación - Regina", NA, "Penya - Roja - Avda. Francia", 
    "Bel - Air", "El Montgó", "Mariola", "Centre", "Embajadores - Lavapiés", 
    "Escandinavia - Cautivador", "Casablanca - Calvario", "Creu Alta", 
    "Son Serra - Sa Vileta", "Cancelada", NA, "Bellaterra", "El Sucre - El Nadal", 
    "El Sucre - El Nadal", "Paraiso - Barronal", "Vejer", NA, 
    "El Sucre - El Nadal", "Santa Bárbara", NA, NA, "La Orden", 
    NA, "Segur Platja", NA, NA, "Cala Major", "Centro", "Barri del Centre", 
    "Ca n'Aurell", "Cancelada", "Pinares de San Antón", NA, 
    "Segur Platja", "Norte", "Beteró", NA, "San Juan", "El Rocío - La Milagrosa"
    )), row.names = c(NA, -100L), class = c("tbl_df", "tbl", 
"data.frame"))

应用程序:

library(shiny)

ui_update_filter_properties_location <- function(id) {
  ns <- NS(id)

  tabPanel(
    id,
    tagList(
      tags$div(
        class = "panel-inputs",
        selectInput(ns("provinceSelect"), label = "Select Province Data", choices = c()),
        selectInput(ns("municipioSelect"), label = "Select Municipio Variable", choices = c()),
        selectInput(ns("distritoSelect"), label = "Select Distrito Variable", choices = c()),
        selectInput(ns("zonaSelect"), label = "Select Zona Variable", choices = c()),
        tableOutput(ns('filteredDataOUT'))
      )
    )
  )
}

server_update_filter_properties_location <- function(id, data) {
  moduleServer(
    id,
    function(input, output, session) {

      observe({
        choices = data %>% select(provincia) %>% unique() %>% pull(provincia)

        updateSelectInput(
          session,
          "provinceSelect",
          choices = choices
        )
      })

      ## (2) ## The municipios are then updated to reflect the provincias selection
      observeEvent(input$provinceSelect,{

        choices = data %>% filter(provincia == input$provinceSelect) %>% select(municipio) %>% unique() %>% pull(municipio)

        updateSelectInput(
          session,
          "municipioSelect",
          choices = choices
        )
      })

      ## (3) ## Update the distrito dropdown depending on the muncipio selected
      observeEvent(input$municipioSelect,{

        choices = data %>% filter(municipio == input$municipioSelect) %>% select(distrito) %>% unique() %>% pull(distrito)
        updateSelectInput(
          session,
          "distritoSelect",
          choices = choices
        )
      })

      ## (4) ## Update the zona dropdown depending on the distrito selected
      observeEvent(input$distritoSelect,{

        choices = data %>% filter(distrito == input$distritoSelect) %>% select(zona) %>% unique() %>% pull(zona)

        updateSelectInput(
          session,
          "zonaSelect",
          choices = choices
        )
      })


      filteredDATA = reactive(
        filteredData <- data %>%
          filter(provincia == input$provinceSelect & municipio == input$municipioSelect & distrito == input$distritoSelect & zona == input$zonaSelect) %>%
          select(-c("provincia", "municipio", "distrito", "zona"))
      )
      output$filteredDataOUT <- renderTable(
        filteredDATA()
      )

    }
  )
}



ui <- fluidPage(
  ui_update_filter_properties_location("myFilter")
)

server <- function(input, output) {
  server_update_filter_properties_location("myFilter", data)
}

shinyApp(ui = ui, server = server)
qrjkbowd

qrjkbowd1#

也许这个能满足你的需要

library(shiny)

ui_update_filter_properties_location <- function(id) {
  ns <- NS(id)
  
  tabPanel(
    id,
    tagList(
      tags$div(
        class = "panel-inputs",
        selectInput(ns("provinceSelect"), label = "Select Province Data", multiple = TRUE, choices = c()),
        selectInput(ns("municipioSelect"), label = "Select Municipio Variable", multiple = TRUE,  choices = c()),
        selectInput(ns("distritoSelect"), label = "Select Distrito Variable", multiple = TRUE, choices = c()),
        selectInput(ns("zonaSelect"), label = "Select Zona Variable", multiple = TRUE, choices = c()),
        actionBttn(ns("reset"), "Reset"),
        tableOutput(ns('filteredDataOUT'))
      )
    )
  )
}

server_update_filter_properties_location <- function(id, data) {
  moduleServer(
    id,
    function(input, output, session) {
      filtered <- reactiveValues(DATA=data)
      
      observe({
        choices = data %>% select(provincia) %>% unique() %>% pull(provincia)
        choices = c("",choices)
        updateSelectInput(
          session,
          "provinceSelect",
          choices = choices
        )
      })
      
      ## (2) ## The municipios are then updated to reflect the provincias selection
      observeEvent(input$provinceSelect,{
        filtered$DATA <- data %>% filter(provincia %in% input$provinceSelect) 
        
        choices = filtered$DATA %>% select(municipio) %>% unique() %>% pull(municipio)
        choices = c("",choices)
        updateSelectInput(
          session,
          "municipioSelect",
          choices = choices
        )
      }, ignoreNULL = FALSE)
      
      ## (3) ## Update the distrito dropdown depending on the muncipio selected
      observeEvent(input$municipioSelect, {
        print(input$municipioSelect)
        if (sum( is.na(input$municipioSelect) )<1 | !is.null(input$municipioSelect)) 
          filtered$DATA <- data %>% filter(provincia %in% input$provinceSelect) %>% 
                                    filter(municipio %in% input$municipioSelect)
        choices = filtered$DATA %>% select(distrito) %>% unique() %>% pull(distrito)
        choices = c("",choices)
        updateSelectInput(
          session,
          "distritoSelect",
          choices = choices
        )
      })
      
      ## (4) ## Update the zona dropdown depending on the distrito selected
      observeEvent(input$distritoSelect, {
        if (sum( is.na(input$distritoSelect) )<1  | !is.null(input$distritoSelect)) 
          filtered$DATA <- data %>% filter(provincia %in% input$provinceSelect) %>% 
                                    filter(municipio %in% input$municipioSelect) %>% 
                                    filter(distrito %in% input$distritoSelect)
        choices = filtered$DATA  %>% select(zona) %>% unique() %>% pull(zona)
        choices = c("",choices)
        updateSelectInput(
          session,
          "zonaSelect",
          choices = choices
        )
      }, ignoreNULL = FALSE)
      
      observeEvent(input$zonaSelect, {
        if (sum( is.na(input$zonaSelect) )<1  | !is.null(input$zonaSelect)) 
          filtered$DATA <- data %>% filter(provincia %in% input$provinceSelect) %>% 
            filter(municipio %in% input$municipioSelect) %>% 
            filter(distrito %in% input$distritoSelect) %>% 
            filter(zona %in% input$zonaSelect)
      }, ignoreNULL = FALSE)
      
      observeEvent(input$reset, {
        filtered$DATA <- data 
        updateSelectInput(session,"provinceSelect",choices = c("",unique(filtered$DATA$provincia)))
        updateSelectInput(session,"municipioSelect",choices = c("",unique(filtered$DATA$municipio)))
        updateSelectInput(session,"distritoSelect",choices = c("",unique(filtered$DATA$distrito)))
        updateSelectInput(session,"zonaSelect",choices = c("",unique(filtered$DATA$zona)))
      })
      
      output$filteredDataOUT <- renderTable(
        filtered$DATA
      )
      
    }
  )
}

ui <- fluidPage(
  ui_update_filter_properties_location("myFilter")
)

server <- function(input, output) {
  server_update_filter_properties_location("myFilter", data)
}

shinyApp(ui = ui, server = server)

相关问题