将当前工作中的下拉菜单“updateSelectInput”模块化为闪亮的输入。

huangapple go评论69阅读模式
英文:

modularise a currently working dropdown "updateSelectInput" shiny input

问题

I have translated the code part as requested:

myProvLists = data %>%
  pull(provincia) %>%
  unique()

# UI component for the module
ui_location_selections <- function(id) {
  ns <- NS(id)
  selectInput("provinceSelect", label = "Select Province Data", choices = c(myProvLists))
  selectInput("municipioSelect", label = "Select Municipio Variable", choices = c())
  # selectInput("distritoSelect", label = "Select Distrito Variable", choices = c())
}

# Server component for the module

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

    observeEvent(input$provinceSelect,{
      updateSelectInput(
        session,
        "municipioSelect",
        choices = data %>% filter(provincia == input$provinceSelect) %>% select(municipio) %>% unique() %>% pull(municipio)
      )
    })

    ## (1) ## first drop down - observe the selection
    observeEvent(input$provinceSelect,{
      updateSelectInput(
        session,
        "municipioSelect",
        choices = data %>% filter(provincia == input$provinceSelect) %>% select(municipio) %>% unique() %>% pull(municipio)
      )
    })

  }
  )
}

# Call the module in the app
ui <- fluidPage(
  ui_location_selections("provincias"),
  ui_location_selections("municipios"),
  renderUI(output$provincias)
)

server <- function(input, output, session, data) {
  server_location_selections("provincias")
}

shinyApp(ui, server)

Please note that the code you provided has some issues, and it seems you are trying to create a Shiny app with modularized UI and server components. However, the code still needs further adjustments to work correctly.

英文:

I have some data which looks like:

# A tibble: 100 &#215; 5
   purchase_price provincia     municipio                                   distrito         zona                              
            &lt;dbl&gt; &lt;chr&gt;         &lt;chr&gt;                                       &lt;chr&gt;            &lt;chr&gt;                             
 1         207000 Gipuzkoa      Bajo Bidasoa                                Irun             Pinar - Anaka - Belaskoenea       
 2          65000 Valencia      Valencia, Zona de                           Valencia Capital Els Orriols                       
 3          62000 Valencia      Valencia, Zona de                           Valencia Capital Barrio de Benicalap               
 4         200000 Valencia      Valencia, Zona de                           Valencia Capital Barrio de Benimaclet              
 5         293000 M&#225;laga        Costa del Sol Occidental - Zona de Estepona Estepona         Parque Central                    
 6          80000 Araba - &#193;lava Laguardia - Rioja Alavesa                   Navaridas        NA                                
 7          96500 Tarragona     Tarragon&#232;s                                  Salou            Mar i Camp - Platja dels Capellans
 8         119500 Ja&#233;n          Campi&#241;a de Ja&#233;n                             Marmolejo        NA                                
 9         149999 Tarragona     Tarragon&#232;s                                  Salou            Platja de Llevant                 
10         144000 Barcelona     Maresme                                     Matar&#243;           Cerdanyola Sud

I am trying to modularise my code to make it more complete and clean.

For example I am trying to do something where I only define the UI and observeEvent once. My current solution I have to define it 3 times and I am trying to clean the code and be more "efficient".

ui_dygraph &lt;- function(id) {
  ns &lt;- NS(id)

  choices &lt;- map_chr()
  
  tagList(
    tags$div(
      class = &quot;panel-header&quot;,
      selectInput(
        ns(&quot;loc&quot;), &quot;Select dropdown&quot;,
        choices,
        width = NULL,
        selectize = TRUE,
        selected = choices[[1]]
      )
    )
  )
}

server_dygraph &lt;- function(id){
  moduleServer(id, function(input, output, session){
    observeEvent()
  }
  )
}

Shiny App:

library(shiny)
library(tidyverse)
library(bslib)

# data &lt;- ...

ui &lt;-  fluidPage(
  fluidPage(
    theme = bs_theme(bootswatch = &quot;minty&quot;),
    title = &quot;hi&quot;,
    fluidRow(
      column(2,
             selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = c()),
             selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c()),
             selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
      )
    ),
    tableOutput(&#39;filteredDataOUT&#39;)

  )
)



server &lt;- function(input, output, session) {

  #####################################################################################
  ################################# Property Search ###################################
  #####################################################################################

  ## (1) ## first drop down - observe the selection
  observeEvent(input$provinceSelect,{
    updateSelectInput(
      session,
      &quot;municipioSelect&quot;,
      choices = data %&gt;% filter(provincia == input$provinceSelect) %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio)
    )
  })
  # then update the second dropdown selections
  observe({
    updateSelectInput(
      session,
      &quot;provinceSelect&quot;,
      choices = data %&gt;% select(provincia) %&gt;% unique() %&gt;% pull(provincia)
    )
  })
  ## (1) ## Do the same for the second dropdown
  observeEvent(input$municipioSelect,{
    updateSelectInput(
      session,
      &quot;distritoSelect&quot;,
      choices = data %&gt;% filter(municipio == input$municipioSelect) %&gt;% select(distrito) %&gt;% unique() %&gt;% pull(distrito)
    )
  })
  observe({
    updateSelectInput(
      session,
      &quot;distritoSelect&quot;,
      choices = data %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio))
  })

  #### new code

  filteredDATA = reactive(
    filteredData &lt;- data %&gt;%
      filter(provincia == input$provinceSelect &amp; municipio == input$municipioSelect &amp; distrito == input$distritoSelect) %&gt;%
      select(-c(&quot;provincia&quot;, &quot;municipio&quot;, &quot;distrito&quot;))
  )
  output$filteredDataOUT &lt;- renderTable(
    filteredDATA()
  )
}

shinyApp(ui = ui, server = server)

Data:

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(&quot;Gipuzkoa&quot;, &quot;Valencia&quot;, &quot;Valencia&quot;, &quot;Valencia&quot;, 
&quot;M&#225;laga&quot;, &quot;Araba - &#193;lava&quot;, &quot;Tarragona&quot;, &quot;Ja&#233;n&quot;, &quot;Tarragona&quot;, 
&quot;Barcelona&quot;, &quot;Barcelona&quot;, &quot;Alicante&quot;, &quot;Granada&quot;, &quot;M&#225;laga&quot;, 
&quot;Barcelona&quot;, &quot;Tarragona&quot;, &quot;Tarragona&quot;, &quot;Barcelona&quot;, &quot;Valencia&quot;, 
&quot;Tarragona&quot;, &quot;Castell&#243;n&quot;, &quot;Segovia&quot;, &quot;Alicante&quot;, &quot;Tarragona&quot;, 
&quot;M&#225;laga&quot;, &quot;Girona&quot;, &quot;Cantabria&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;, 
&quot;Barcelona&quot;, &quot;Barcelona&quot;, &quot;Sevilla&quot;, &quot;Granada&quot;, &quot;Barcelona&quot;, 
&quot;Barcelona&quot;, &quot;C&#225;ceres&quot;, &quot;Barcelona&quot;, &quot;Valencia&quot;, &quot;Gipuzkoa&quot;, 
&quot;Santa Cruz de Tenerife&quot;, &quot;Tarragona&quot;, &quot;Almer&#237;a&quot;, &quot;Alicante&quot;, 
&quot;Granada&quot;, &quot;Tarragona&quot;, &quot;Toledo&quot;, &quot;Tarragona&quot;, &quot;Huelva&quot;, 
&quot;Castell&#243;n&quot;, &quot;Albacete&quot;, &quot;Madrid&quot;, &quot;Girona&quot;, &quot;Castell&#243;n&quot;, 
&quot;Zaragoza&quot;, &quot;Madrid&quot;, &quot;Alicante&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;, 
&quot;Sevilla&quot;, &quot;Castell&#243;n&quot;, &quot;Valencia&quot;, &quot;M&#225;laga&quot;, &quot;Alicante&quot;, 
&quot;Lleida&quot;, &quot;Girona&quot;, &quot;Madrid&quot;, &quot;Alicante&quot;, &quot;Pontevedra&quot;, &quot;Barcelona&quot;, 
&quot;Illes Balears&quot;, &quot;M&#225;laga&quot;, &quot;A Coru&#241;a&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;, 
&quot;Barcelona&quot;, &quot;M&#225;laga&quot;, &quot;C&#225;diz&quot;, &quot;Valencia&quot;, &quot;Barcelona&quot;, 
&quot;Toledo&quot;, &quot;Castell&#243;n&quot;, &quot;Barcelona&quot;, &quot;Huelva&quot;, &quot;Barcelona&quot;, 
&quot;Tarragona&quot;, &quot;A Coru&#241;a&quot;, &quot;Ciudad Real&quot;, &quot;Illes Balears&quot;, 
&quot;Ourense&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;, &quot;M&#225;laga&quot;, &quot;M&#225;laga&quot;, 
&quot;C&#243;rdoba&quot;, &quot;Tarragona&quot;, &quot;Castell&#243;n&quot;, &quot;Valencia&quot;, &quot;Castell&#243;n&quot;, 
&quot;Navarra&quot;, &quot;C&#225;diz&quot;), municipio = c(&quot;Bajo Bidasoa&quot;, &quot;Valencia, Zona de&quot;, 
&quot;Valencia, Zona de&quot;, &quot;Valencia, Zona de&quot;, &quot;Costa del Sol Occidental - Zona de Estepona&quot;, 
&quot;Laguardia - Rioja Alavesa&quot;, &quot;Tarragon&#232;s&quot;, &quot;Campi&#241;a de Ja&#233;n&quot;, 
&quot;Tarragon&#232;s&quot;, &quot;Maresme&quot;, &quot;Maresme&quot;, &quot;Marina Baixa&quot;, &quot;Vega de Granada&quot;, 
&quot;Costa del Sol Occidental - Zona de Estepona&quot;, &quot;Maresme&quot;, 
&quot;Tarragon&#232;s&quot;, &quot;Tarragon&#232;s&quot;, &quot;Barcelon&#232;s&quot;, &quot;Horta Nord&quot;, 
&quot;Tarragon&#232;s&quot;, &quot;Plana Baixa&quot;, &quot;Cu&#233;llar, Zona de&quot;, &quot;Alacant&#237;&quot;, 
&quot;Baix Camp&quot;, &quot;Costa del Sol Occidental - Zona de Benalm&#225;dena&quot;, 
&quot;La Selva&quot;, &quot;Costa Oriental&quot;, &quot;Vall&#232;s Oriental&quot;, &quot;Vall&#232;s Oriental&quot;, 
&quot;Vall&#232;s Oriental&quot;, &quot;Maresme&quot;, &quot;Sierra Norte&quot;, &quot;Vega de Granada&quot;, 
&quot;Vall&#232;s Occidental&quot;, &quot;Baix Llobregat Sud&quot;, &quot;Llanos de C&#225;ceres&quot;, 
&quot;Barcelon&#232;s&quot;, &quot;La Safor&quot;, &quot;Donostialdea - Oarsoldea&quot;, &quot;Tenerife&quot;, 
&quot;Tarragon&#232;s&quot;, &quot;Almer&#237;a capital y entorno&quot;, &quot;Alacant&#237;&quot;, 
&quot;Vega de Granada&quot;, &quot;Tarragon&#232;s&quot;, &quot;Los Montes de Toledo&quot;, 
&quot;Tarragon&#232;s&quot;, &quot;Huelva capital y entorno&quot;, &quot;Plana Alta&quot;, 
&quot;Sierra de Alcaraz - Campo de Montiel&quot;, &quot;Zona Sur de Madrid&quot;, 
&quot;La Selva&quot;, &quot;Plana Alta&quot;, &quot;Zaragoza, Zona de&quot;, &quot;Madrid, Zona de&quot;, 
&quot;Vega Baja&quot;, &quot;Barcelon&#232;s&quot;, &quot;Bages&quot;, &quot;Sevilla capital y entorno&quot;, 
&quot;Plana Alta&quot;, &quot;Valencia, Zona de&quot;, &quot;Costa del Sol Occidental - Zona de Estepona&quot;, 
&quot;Marina Alta&quot;, &quot;Segri&#224;&quot;, &quot;Alt Empord&#224;&quot;, &quot;Madrid, Zona de&quot;, 
&quot;Marina Baixa&quot;, &quot;Comarca de Vigo&quot;, &quot;Vall&#232;s Occidental&quot;, 
&quot;Mallorca&quot;, &quot;Costa del Sol Occidental - Zona de Estepona&quot;, 
&quot;Comarca de Ferrol&quot;, &quot;Vall&#232;s Occidental&quot;, &quot;Osona&quot;, &quot;Osona&quot;, 
&quot;Costa del Sol Occidental - Zona de Estepona&quot;, &quot;La Janda&quot;, 
&quot;Ribera Alta (Valencia)&quot;, &quot;Osona&quot;, &quot;Toledo, Zona de&quot;, &quot;Plana Alta&quot;, 
&quot;Osona&quot;, &quot;Huelva capital y entorno&quot;, &quot;Vall&#232;s Oriental&quot;, 
&quot;Baix Pened&#232;s&quot;, &quot;Comarca de Ferrol&quot;, &quot;Alcudia (Ciudad Real)&quot;, 
&quot;Mallorca&quot;, &quot;Comarca de Ourense&quot;, &quot;Vall&#232;s Occidental&quot;, &quot;Vall&#232;s Occidental&quot;, 
&quot;Costa del Sol Occidental - Zona de Estepona&quot;, &quot;M&#225;laga capital y entorno&quot;, 
&quot;La Subb&#233;tica&quot;, &quot;Baix Pened&#232;s&quot;, &quot;Plana Alta&quot;, &quot;Valencia, Zona de&quot;, 
&quot;Plana Baixa&quot;, &quot;Comarca de Pamplona&quot;, &quot;Campi&#241;a de Jerez&quot;
), distrito = c(&quot;Irun&quot;, &quot;Valencia Capital&quot;, &quot;Valencia Capital&quot;, 
&quot;Valencia Capital&quot;, &quot;Estepona&quot;, &quot;Navaridas&quot;, &quot;Salou&quot;, &quot;Marmolejo&quot;, 
&quot;Salou&quot;, &quot;Matar&#243;&quot;, &quot;Dosrius&quot;, &quot;Benidorm&quot;, &quot;Granada Capital&quot;, 
&quot;Manilva&quot;, &quot;Dosrius&quot;, &quot;Roda de Ber&#224;&quot;, &quot;Roda de Ber&#224;&quot;, &quot;Barcelona Capital&quot;, 
&quot;Puig&quot;, &quot;Tarragona Capital&quot;, &quot;Vila-real&quot;, &quot;Marug&#225;n&quot;, &quot;San Vicente del Raspeig / Sant Vicent del Raspeig&quot;, 
&quot;Mont-roig del Camp&quot;, &quot;Benalm&#225;dena&quot;, &quot;Angl&#232;s&quot;, &quot;Laredo&quot;, 
&quot;Granollers&quot;, &quot;Granollers&quot;, &quot;Granollers&quot;, &quot;Cabrils&quot;, &quot;El Ronquillo&quot;, 
&quot;Cenes de la Vega&quot;, &quot;Santa Perp&#232;tua de Mogoda&quot;, &quot;Sant Boi de Llobregat&quot;, 
&quot;C&#225;ceres Capital&quot;, &quot;Barcelona Capital&quot;, &quot;Barx&quot;, &quot;Donostia - San Sebasti&#225;n&quot;, 
&quot;Tacoronte&quot;, &quot;Salou&quot;, &quot;Almer&#237;a Capital&quot;, &quot;El Campello&quot;, 
&quot;Albolote&quot;, &quot;Salou&quot;, &quot;Nambroca&quot;, &quot;Salou&quot;, &quot;Huelva Capital&quot;, 
&quot;Castell&#243;n de la Plana / Castell&#243; de la Plana&quot;, &quot;Alcaraz&quot;, 
&quot;Fuenlabrada&quot;, &quot;Riells i Viabrea&quot;, &quot;Castell&#243;n de la Plana / Castell&#243; de la Plana&quot;, 
&quot;Zaragoza Capital&quot;, &quot;Madrid Capital&quot;, &quot;Torrevieja&quot;, &quot;Badalona&quot;, 
&quot;Castellgal&#237;&quot;, &quot;Sevilla Capital&quot;, &quot;Cabanes&quot;, &quot;Valencia Capital&quot;, 
&quot;Estepona&quot;, &quot;D&#233;nia&quot;, &quot;Lleida Capital&quot;, &quot;Roses&quot;, &quot;Madrid Capital&quot;, 
&quot;L&#39;Alf&#224;s del Pi&quot;, &quot;Vigo&quot;, &quot;Sabadell&quot;, &quot;Palma de Mallorca&quot;, 
&quot;Estepona&quot;, &quot;Fene&quot;, &quot;Cerdanyola del Vall&#232;s&quot;, &quot;Vic&quot;, &quot;Vic&quot;, 
&quot;Estepona&quot;, &quot;Vejer de la Frontera&quot;, &quot;Senyera&quot;, &quot;Vic&quot;, &quot;Toledo Capital&quot;, 
&quot;Borriol&quot;, &quot;Santa Eug&#232;nia de Berga&quot;, &quot;Huelva Capital&quot;, &quot;Sant Celoni&quot;, 
&quot;Calafell&quot;, &quot;Fene&quot;, &quot;Almad&#233;n&quot;, &quot;Palma de Mallorca&quot;, &quot;Ourense Capital&quot;, 
&quot;Terrassa&quot;, &quot;Terrassa&quot;, &quot;Estepona&quot;, &quot;M&#225;laga Capital&quot;, &quot;Lucena&quot;, 
&quot;Calafell&quot;, &quot;Castell&#243;n de la Plana / Castell&#243; de la Plana&quot;, 
&quot;Valencia Capital&quot;, &quot;Onda&quot;, &quot;Pamplona / Iru&#241;a&quot;, &quot;Jerez de la Frontera&quot;
), zona = c(&quot;Pinar - Anaka - Belaskoenea&quot;, &quot;Els Orriols&quot;, 
&quot;Barrio de Benicalap&quot;, &quot;Barrio de Benimaclet&quot;, &quot;Parque Central&quot;, 
NA, &quot;Mar i Camp - Platja dels Capellans&quot;, NA, &quot;Platja de Llevant&quot;, 
&quot;Cerdanyola Sud&quot;, &quot;Can Massuet del Far&quot;, &quot;Levante Alto&quot;, 
&quot;Centro - Sagrario&quot;, &quot;Manilva Pueblo&quot;, &quot;Canyamars&quot;, NA, NA, 
&quot;Vilapicina i la Torre Llobeta&quot;, &quot;El Puig&quot;, &quot;Llevant&quot;, &quot;Centro&quot;, 
NA, &quot;Centro&quot;, &quot;Poble&quot;, &quot;Zona Centro Comercial Torrequebrada&quot;, 
NA, &quot;Zona Playa&quot;, &quot;Lledoner&quot;, &quot;Lledoner&quot;, &quot;Lledoner&quot;, NA, 
NA, NA, NA, &quot;Casablanca&quot;, &quot;Mejostilla&quot;, &quot;El Poble Sec - Parc de Montju&#239;c&quot;, 
NA, &quot;Amara Zaharra - Arbaizenea&quot;, &quot;Campo de Golf - Agua Garc&#237;a - Juan Fern&#225;ndez&quot;, 
&quot;Platja de Llevant&quot;, &quot;Plaza de Toros - Santa Rita&quot;, &quot;Playa Muchavista&quot;, 
NA, &quot;Mar i Camp - Platja dels Capellans&quot;, NA, &quot;Centre&quot;, &quot;Tres Ventanas&quot;, 
&quot;El Grao&quot;, NA, &quot;El Naranjo&quot;, NA, &quot;Oeste&quot;, &quot;La Magdalena&quot;, 
&quot;Recoletos&quot;, &quot;Zona Carrefour - Urbanizaciones&quot;, &quot;Sant Roc&quot;, 
NA, &quot;Encarnaci&#243;n - Regina&quot;, NA, &quot;Penya - Roja - Avda. Francia&quot;, 
&quot;Bel - Air&quot;, &quot;El Montg&#243;&quot;, &quot;Mariola&quot;, &quot;Centre&quot;, &quot;Embajadores - Lavapi&#233;s&quot;, 
&quot;Escandinavia - Cautivador&quot;, &quot;Casablanca - Calvario&quot;, &quot;Creu Alta&quot;, 
&quot;Son Serra - Sa Vileta&quot;, &quot;Cancelada&quot;, NA, &quot;Bellaterra&quot;, &quot;El Sucre - El Nadal&quot;, 
&quot;El Sucre - El Nadal&quot;, &quot;Paraiso - Barronal&quot;, &quot;Vejer&quot;, NA, 
&quot;El Sucre - El Nadal&quot;, &quot;Santa B&#225;rbara&quot;, NA, NA, &quot;La Orden&quot;, 
NA, &quot;Segur Platja&quot;, NA, NA, &quot;Cala Major&quot;, &quot;Centro&quot;, &quot;Barri del Centre&quot;, 
&quot;Ca n&#39;Aurell&quot;, &quot;Cancelada&quot;, &quot;Pinares de San Ant&#243;n&quot;, NA, 
&quot;Segur Platja&quot;, &quot;Norte&quot;, &quot;Beter&#243;&quot;, NA, &quot;San Juan&quot;, &quot;El Roc&#237;o - La Milagrosa&quot;
)), row.names = c(NA, -100L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, 
&quot;data.frame&quot;))

EDIT:

My attempt which doesn't work:

myProvLists = data %&gt;%
pull(provincia) %&gt;%
unique()
# UI component for the module
ui_location_selections &lt;- function(id) {
ns &lt;- NS(id)
selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = c(myProvLists))
selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c())
# selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
}
# Server component for the module
server_location_selections &lt;- function(id){
moduleServer(id, function(input, output, session){
observeEvent(input$provinceSelect,{
updateSelectInput(
session,
&quot;municipioSelect&quot;,
choices = data %&gt;% filter(provincia == input$provinceSelect) %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio)
)
})
## (1) ## first drop down - observe the selection
observeEvent(input$provinceSelect,{
updateSelectInput(
session,
&quot;municipioSelect&quot;,
choices = data %&gt;% filter(provincia == input$provinceSelect) %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio)
)
})
}
)
}
# Call the module in the app
ui &lt;- fluidPage(
ui_location_selections(&quot;provincias&quot;),
ui_location_selections(&quot;municipios&quot;),
renderUI(output$provincias)
)
server &lt;- function(input, output, session, data) {
server_location_selections(&quot;provincias&quot;)
}
shinyApp(ui, server)

答案1

得分: 2

以下是代码部分的翻译:

library(shiny)
library(tidyverse)
library(bslib)

# 数据 &lt;- ...

ui &lt;-  fluidPage(
  fluidPage(
    theme = bs_theme(bootswatch = &quot;minty&quot;),
    title = &quot;hi&quot;,
    fluidRow(
      column(2,
             selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = data %&gt;% pull(provincia) %&gt;% unique()),
             selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c()),
             selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
      )
    ),
    tableOutput(&#39;filteredDataOUT&#39;)
    
  )
)


server &lt;- function(input, output, session) {
  
  # 用于在响应式环境中更新和访问数据的响应式值列表
  values &lt;- reactiveValues()
   
  ## 基于 provinceSelect 过滤数据集,并使用剩余选择项更新 municipioSelect
  observeEvent(input$provinceSelect,{
    values[[&#39;provincia_df&#39;]] &lt;- data %&gt;% filter(provincia == input$provinceSelect)
    updateSelectInput(
      session, &quot;municipioSelect&quot;,
      choices = values[[&#39;provincia_df&#39;]] %&gt;% pull(municipio) %&gt;% unique()
    )
  })
  
  ## 也根据 municipioSelect 过滤数据集,并使用剩余选择项更新 distritoSelect
  observeEvent(input$municipioSelect,{
    values[[&#39;municipio_df&#39;]] &lt;- values[[&#39;provincia_df&#39;]] %&gt;% filter(municipio == input$municipioSelect)
    updateSelectInput(
      session, &quot;distritoSelect&quot;,
      choices = values[[&#39;municipio_df&#39;]] %&gt;% pull(distrito) %&gt;% unique()
    )
  })
  
  # 最终输出由 input$distritoSelect 自动触发
  output$filteredDataOUT &lt;- renderTable(
    values[[&#39;municipio_df&#39;]] %&gt;% filter(distrito == input$distritoSelect) %&gt;% select(-all_of(c(&quot;provincia&quot;, &quot;municipio&quot;, &quot;distrito&quot;)))
  )
}

shinyApp(ui = ui, server = server)
英文:

The solution below is not so much modularisation, but rather a restructuring of the update logic which only requires 2 observeEvents and the reactive renderTable for output.

The key points are

  • defining the provincia choices directly in ui since they never have to be updated (which you also did in your own attempt)
  • observeEvents triggered by the provincia and municipio dropdowns, which subsequently filter the full dataframe based on the choice and update the remaining choices for the next level dropdown menu
  • no need for an observeEvent for the last dropdown in the hierarchy, because the reactiveTable updates based on this selection anyway

Out of habit, I replaced filteredDATA with a reactiveValues list values to carry intermediate datasets between reactive environments. This could instead always filter directly from data, so not strictly necessary.

library(shiny)
library(tidyverse)
library(bslib)

# data &lt;- ...

ui &lt;-  fluidPage(
  fluidPage(
    theme = bs_theme(bootswatch = &quot;minty&quot;),
    title = &quot;hi&quot;,
    fluidRow(
      column(2,
             selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = data %&gt;% pull(provincia) %&gt;% unique()),
             selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c()),
             selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
      )
    ),
    tableOutput(&#39;filteredDataOUT&#39;)
    
  )
)


server &lt;- function(input, output, session) {
  
  #reactive value list to update and access data in reactive environments
  values &lt;- reactiveValues()
   
  ## filter dataset based on provinceSelect, and update municipioSelect with remaining choices
  observeEvent(input$provinceSelect,{
    values[[&#39;provincia_df&#39;]] &lt;- data %&gt;% filter(provincia == input$provinceSelect)
    updateSelectInput(
      session, &quot;municipioSelect&quot;,
      choices = values[[&#39;provincia_df&#39;]] %&gt;% pull(municipio) %&gt;% unique()
    )
  })
  
  ## filter dataset also on municipioSelect, and update distritoSelect with remaining choices
  observeEvent(input$municipioSelect,{
    values[[&#39;municipio_df&#39;]] &lt;- values[[&#39;provincia_df&#39;]] %&gt;% filter(municipio == input$municipioSelect)
    updateSelectInput(
      session, &quot;distritoSelect&quot;,
      choices = values[[&#39;municipio_df&#39;]] %&gt;% pull(distrito) %&gt;% unique()
    )
  })
  
  # final output is automaticall triggered by input$distritoSelect
  output$filteredDataOUT &lt;- renderTable(
    values[[&#39;municipio_df&#39;]] %&gt;% filter(distrito == input$distritoSelect) %&gt;%
      select(-all_of(c(&quot;provincia&quot;, &quot;municipio&quot;, &quot;distrito&quot;)))
  )
}

shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年2月10日 05:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定