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

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

modularise a currently working dropdown "updateSelectInput" shiny input

问题

I have translated the code part as requested:

  1. myProvLists = data %>%
  2. pull(provincia) %>%
  3. unique()
  4. # UI component for the module
  5. ui_location_selections <- function(id) {
  6. ns <- NS(id)
  7. selectInput("provinceSelect", label = "Select Province Data", choices = c(myProvLists))
  8. selectInput("municipioSelect", label = "Select Municipio Variable", choices = c())
  9. # selectInput("distritoSelect", label = "Select Distrito Variable", choices = c())
  10. }
  11. # Server component for the module
  12. server_location_selections <- function(id){
  13. moduleServer(id, function(input, output, session){
  14. observeEvent(input$provinceSelect,{
  15. updateSelectInput(
  16. session,
  17. "municipioSelect",
  18. choices = data %>% filter(provincia == input$provinceSelect) %>% select(municipio) %>% unique() %>% pull(municipio)
  19. )
  20. })
  21. ## (1) ## first drop down - observe the selection
  22. observeEvent(input$provinceSelect,{
  23. updateSelectInput(
  24. session,
  25. "municipioSelect",
  26. choices = data %>% filter(provincia == input$provinceSelect) %>% select(municipio) %>% unique() %>% pull(municipio)
  27. )
  28. })
  29. }
  30. )
  31. }
  32. # Call the module in the app
  33. ui <- fluidPage(
  34. ui_location_selections("provincias"),
  35. ui_location_selections("municipios"),
  36. renderUI(output$provincias)
  37. )
  38. server <- function(input, output, session, data) {
  39. server_location_selections("provincias")
  40. }
  41. 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:

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

  1. ui_dygraph &lt;- function(id) {
  2. ns &lt;- NS(id)
  3. choices &lt;- map_chr()
  4. tagList(
  5. tags$div(
  6. class = &quot;panel-header&quot;,
  7. selectInput(
  8. ns(&quot;loc&quot;), &quot;Select dropdown&quot;,
  9. choices,
  10. width = NULL,
  11. selectize = TRUE,
  12. selected = choices[[1]]
  13. )
  14. )
  15. )
  16. }
  17. server_dygraph &lt;- function(id){
  18. moduleServer(id, function(input, output, session){
  19. observeEvent()
  20. }
  21. )
  22. }

Shiny App:

  1. library(shiny)
  2. library(tidyverse)
  3. library(bslib)
  4. # data &lt;- ...
  5. ui &lt;- fluidPage(
  6. fluidPage(
  7. theme = bs_theme(bootswatch = &quot;minty&quot;),
  8. title = &quot;hi&quot;,
  9. fluidRow(
  10. column(2,
  11. selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = c()),
  12. selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c()),
  13. selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
  14. )
  15. ),
  16. tableOutput(&#39;filteredDataOUT&#39;)
  17. )
  18. )
  19. server &lt;- function(input, output, session) {
  20. #####################################################################################
  21. ################################# Property Search ###################################
  22. #####################################################################################
  23. ## (1) ## first drop down - observe the selection
  24. observeEvent(input$provinceSelect,{
  25. updateSelectInput(
  26. session,
  27. &quot;municipioSelect&quot;,
  28. choices = data %&gt;% filter(provincia == input$provinceSelect) %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio)
  29. )
  30. })
  31. # then update the second dropdown selections
  32. observe({
  33. updateSelectInput(
  34. session,
  35. &quot;provinceSelect&quot;,
  36. choices = data %&gt;% select(provincia) %&gt;% unique() %&gt;% pull(provincia)
  37. )
  38. })
  39. ## (1) ## Do the same for the second dropdown
  40. observeEvent(input$municipioSelect,{
  41. updateSelectInput(
  42. session,
  43. &quot;distritoSelect&quot;,
  44. choices = data %&gt;% filter(municipio == input$municipioSelect) %&gt;% select(distrito) %&gt;% unique() %&gt;% pull(distrito)
  45. )
  46. })
  47. observe({
  48. updateSelectInput(
  49. session,
  50. &quot;distritoSelect&quot;,
  51. choices = data %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio))
  52. })
  53. #### new code
  54. filteredDATA = reactive(
  55. filteredData &lt;- data %&gt;%
  56. filter(provincia == input$provinceSelect &amp; municipio == input$municipioSelect &amp; distrito == input$distritoSelect) %&gt;%
  57. select(-c(&quot;provincia&quot;, &quot;municipio&quot;, &quot;distrito&quot;))
  58. )
  59. output$filteredDataOUT &lt;- renderTable(
  60. filteredDATA()
  61. )
  62. }
  63. shinyApp(ui = ui, server = server)

Data:

  1. data = structure(list(purchase_price = c(207000, 65000, 62000, 2e+05,
  2. 293000, 80000, 96500, 119500, 149999, 144000, 298000, 135000,
  3. 310000, 285000, 269000, 120000, 595000, 355000, 96000, 490000,
  4. 195000, 235000, 197000, 70000, 215000, 169000, 124900, 195000,
  5. 185000, 190000, 390348, 113500, 295000, 299995, 156000, 195000,
  6. 185000, 260000, 370000, 180000, 105000, 249000, 390000, 295000,
  7. 86999, 219900, 264999, 56800, 179900, 150000, 145000, 168500,
  8. 160000, 180000, 168000, 42300, 119000, 350000, 390000, 110000,
  9. 420000, 154000, 429000, 85000, 259000, 495000, 170000, 102490,
  10. 469000, 245000, 138000, 127000, 1390000, 320000, 420000, 292000,
  11. 87500, 120000, 475000, 170000, 61000, 255000, 49000, 226000,
  12. 220000, 3e+05, 30000, 265000, 330000, 220000, 220000, 139000,
  13. 880000, 75000, 220000, 76400, 150000, 46000, 25000, 170000),
  14. provincia = c(&quot;Gipuzkoa&quot;, &quot;Valencia&quot;, &quot;Valencia&quot;, &quot;Valencia&quot;,
  15. &quot;M&#225;laga&quot;, &quot;Araba - &#193;lava&quot;, &quot;Tarragona&quot;, &quot;Ja&#233;n&quot;, &quot;Tarragona&quot;,
  16. &quot;Barcelona&quot;, &quot;Barcelona&quot;, &quot;Alicante&quot;, &quot;Granada&quot;, &quot;M&#225;laga&quot;,
  17. &quot;Barcelona&quot;, &quot;Tarragona&quot;, &quot;Tarragona&quot;, &quot;Barcelona&quot;, &quot;Valencia&quot;,
  18. &quot;Tarragona&quot;, &quot;Castell&#243;n&quot;, &quot;Segovia&quot;, &quot;Alicante&quot;, &quot;Tarragona&quot;,
  19. &quot;M&#225;laga&quot;, &quot;Girona&quot;, &quot;Cantabria&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;,
  20. &quot;Barcelona&quot;, &quot;Barcelona&quot;, &quot;Sevilla&quot;, &quot;Granada&quot;, &quot;Barcelona&quot;,
  21. &quot;Barcelona&quot;, &quot;C&#225;ceres&quot;, &quot;Barcelona&quot;, &quot;Valencia&quot;, &quot;Gipuzkoa&quot;,
  22. &quot;Santa Cruz de Tenerife&quot;, &quot;Tarragona&quot;, &quot;Almer&#237;a&quot;, &quot;Alicante&quot;,
  23. &quot;Granada&quot;, &quot;Tarragona&quot;, &quot;Toledo&quot;, &quot;Tarragona&quot;, &quot;Huelva&quot;,
  24. &quot;Castell&#243;n&quot;, &quot;Albacete&quot;, &quot;Madrid&quot;, &quot;Girona&quot;, &quot;Castell&#243;n&quot;,
  25. &quot;Zaragoza&quot;, &quot;Madrid&quot;, &quot;Alicante&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;,
  26. &quot;Sevilla&quot;, &quot;Castell&#243;n&quot;, &quot;Valencia&quot;, &quot;M&#225;laga&quot;, &quot;Alicante&quot;,
  27. &quot;Lleida&quot;, &quot;Girona&quot;, &quot;Madrid&quot;, &quot;Alicante&quot;, &quot;Pontevedra&quot;, &quot;Barcelona&quot;,
  28. &quot;Illes Balears&quot;, &quot;M&#225;laga&quot;, &quot;A Coru&#241;a&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;,
  29. &quot;Barcelona&quot;, &quot;M&#225;laga&quot;, &quot;C&#225;diz&quot;, &quot;Valencia&quot;, &quot;Barcelona&quot;,
  30. &quot;Toledo&quot;, &quot;Castell&#243;n&quot;, &quot;Barcelona&quot;, &quot;Huelva&quot;, &quot;Barcelona&quot;,
  31. &quot;Tarragona&quot;, &quot;A Coru&#241;a&quot;, &quot;Ciudad Real&quot;, &quot;Illes Balears&quot;,
  32. &quot;Ourense&quot;, &quot;Barcelona&quot;, &quot;Barcelona&quot;, &quot;M&#225;laga&quot;, &quot;M&#225;laga&quot;,
  33. &quot;C&#243;rdoba&quot;, &quot;Tarragona&quot;, &quot;Castell&#243;n&quot;, &quot;Valencia&quot;, &quot;Castell&#243;n&quot;,
  34. &quot;Navarra&quot;, &quot;C&#225;diz&quot;), municipio = c(&quot;Bajo Bidasoa&quot;, &quot;Valencia, Zona de&quot;,
  35. &quot;Valencia, Zona de&quot;, &quot;Valencia, Zona de&quot;, &quot;Costa del Sol Occidental - Zona de Estepona&quot;,
  36. &quot;Laguardia - Rioja Alavesa&quot;, &quot;Tarragon&#232;s&quot;, &quot;Campi&#241;a de Ja&#233;n&quot;,
  37. &quot;Tarragon&#232;s&quot;, &quot;Maresme&quot;, &quot;Maresme&quot;, &quot;Marina Baixa&quot;, &quot;Vega de Granada&quot;,
  38. &quot;Costa del Sol Occidental - Zona de Estepona&quot;, &quot;Maresme&quot;,
  39. &quot;Tarragon&#232;s&quot;, &quot;Tarragon&#232;s&quot;, &quot;Barcelon&#232;s&quot;, &quot;Horta Nord&quot;,
  40. &quot;Tarragon&#232;s&quot;, &quot;Plana Baixa&quot;, &quot;Cu&#233;llar, Zona de&quot;, &quot;Alacant&#237;&quot;,
  41. &quot;Baix Camp&quot;, &quot;Costa del Sol Occidental - Zona de Benalm&#225;dena&quot;,
  42. &quot;La Selva&quot;, &quot;Costa Oriental&quot;, &quot;Vall&#232;s Oriental&quot;, &quot;Vall&#232;s Oriental&quot;,
  43. &quot;Vall&#232;s Oriental&quot;, &quot;Maresme&quot;, &quot;Sierra Norte&quot;, &quot;Vega de Granada&quot;,
  44. &quot;Vall&#232;s Occidental&quot;, &quot;Baix Llobregat Sud&quot;, &quot;Llanos de C&#225;ceres&quot;,
  45. &quot;Barcelon&#232;s&quot;, &quot;La Safor&quot;, &quot;Donostialdea - Oarsoldea&quot;, &quot;Tenerife&quot;,
  46. &quot;Tarragon&#232;s&quot;, &quot;Almer&#237;a capital y entorno&quot;, &quot;Alacant&#237;&quot;,
  47. &quot;Vega de Granada&quot;, &quot;Tarragon&#232;s&quot;, &quot;Los Montes de Toledo&quot;,
  48. &quot;Tarragon&#232;s&quot;, &quot;Huelva capital y entorno&quot;, &quot;Plana Alta&quot;,
  49. &quot;Sierra de Alcaraz - Campo de Montiel&quot;, &quot;Zona Sur de Madrid&quot;,
  50. &quot;La Selva&quot;, &quot;Plana Alta&quot;, &quot;Zaragoza, Zona de&quot;, &quot;Madrid, Zona de&quot;,
  51. &quot;Vega Baja&quot;, &quot;Barcelon&#232;s&quot;, &quot;Bages&quot;, &quot;Sevilla capital y entorno&quot;,
  52. &quot;Plana Alta&quot;, &quot;Valencia, Zona de&quot;, &quot;Costa del Sol Occidental - Zona de Estepona&quot;,
  53. &quot;Marina Alta&quot;, &quot;Segri&#224;&quot;, &quot;Alt Empord&#224;&quot;, &quot;Madrid, Zona de&quot;,
  54. &quot;Marina Baixa&quot;, &quot;Comarca de Vigo&quot;, &quot;Vall&#232;s Occidental&quot;,
  55. &quot;Mallorca&quot;, &quot;Costa del Sol Occidental - Zona de Estepona&quot;,
  56. &quot;Comarca de Ferrol&quot;, &quot;Vall&#232;s Occidental&quot;, &quot;Osona&quot;, &quot;Osona&quot;,
  57. &quot;Costa del Sol Occidental - Zona de Estepona&quot;, &quot;La Janda&quot;,
  58. &quot;Ribera Alta (Valencia)&quot;, &quot;Osona&quot;, &quot;Toledo, Zona de&quot;, &quot;Plana Alta&quot;,
  59. &quot;Osona&quot;, &quot;Huelva capital y entorno&quot;, &quot;Vall&#232;s Oriental&quot;,
  60. &quot;Baix Pened&#232;s&quot;, &quot;Comarca de Ferrol&quot;, &quot;Alcudia (Ciudad Real)&quot;,
  61. &quot;Mallorca&quot;, &quot;Comarca de Ourense&quot;, &quot;Vall&#232;s Occidental&quot;, &quot;Vall&#232;s Occidental&quot;,
  62. &quot;Costa del Sol Occidental - Zona de Estepona&quot;, &quot;M&#225;laga capital y entorno&quot;,
  63. &quot;La Subb&#233;tica&quot;, &quot;Baix Pened&#232;s&quot;, &quot;Plana Alta&quot;, &quot;Valencia, Zona de&quot;,
  64. &quot;Plana Baixa&quot;, &quot;Comarca de Pamplona&quot;, &quot;Campi&#241;a de Jerez&quot;
  65. ), distrito = c(&quot;Irun&quot;, &quot;Valencia Capital&quot;, &quot;Valencia Capital&quot;,
  66. &quot;Valencia Capital&quot;, &quot;Estepona&quot;, &quot;Navaridas&quot;, &quot;Salou&quot;, &quot;Marmolejo&quot;,
  67. &quot;Salou&quot;, &quot;Matar&#243;&quot;, &quot;Dosrius&quot;, &quot;Benidorm&quot;, &quot;Granada Capital&quot;,
  68. &quot;Manilva&quot;, &quot;Dosrius&quot;, &quot;Roda de Ber&#224;&quot;, &quot;Roda de Ber&#224;&quot;, &quot;Barcelona Capital&quot;,
  69. &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;,
  70. &quot;Mont-roig del Camp&quot;, &quot;Benalm&#225;dena&quot;, &quot;Angl&#232;s&quot;, &quot;Laredo&quot;,
  71. &quot;Granollers&quot;, &quot;Granollers&quot;, &quot;Granollers&quot;, &quot;Cabrils&quot;, &quot;El Ronquillo&quot;,
  72. &quot;Cenes de la Vega&quot;, &quot;Santa Perp&#232;tua de Mogoda&quot;, &quot;Sant Boi de Llobregat&quot;,
  73. &quot;C&#225;ceres Capital&quot;, &quot;Barcelona Capital&quot;, &quot;Barx&quot;, &quot;Donostia - San Sebasti&#225;n&quot;,
  74. &quot;Tacoronte&quot;, &quot;Salou&quot;, &quot;Almer&#237;a Capital&quot;, &quot;El Campello&quot;,
  75. &quot;Albolote&quot;, &quot;Salou&quot;, &quot;Nambroca&quot;, &quot;Salou&quot;, &quot;Huelva Capital&quot;,
  76. &quot;Castell&#243;n de la Plana / Castell&#243; de la Plana&quot;, &quot;Alcaraz&quot;,
  77. &quot;Fuenlabrada&quot;, &quot;Riells i Viabrea&quot;, &quot;Castell&#243;n de la Plana / Castell&#243; de la Plana&quot;,
  78. &quot;Zaragoza Capital&quot;, &quot;Madrid Capital&quot;, &quot;Torrevieja&quot;, &quot;Badalona&quot;,
  79. &quot;Castellgal&#237;&quot;, &quot;Sevilla Capital&quot;, &quot;Cabanes&quot;, &quot;Valencia Capital&quot;,
  80. &quot;Estepona&quot;, &quot;D&#233;nia&quot;, &quot;Lleida Capital&quot;, &quot;Roses&quot;, &quot;Madrid Capital&quot;,
  81. &quot;L&#39;Alf&#224;s del Pi&quot;, &quot;Vigo&quot;, &quot;Sabadell&quot;, &quot;Palma de Mallorca&quot;,
  82. &quot;Estepona&quot;, &quot;Fene&quot;, &quot;Cerdanyola del Vall&#232;s&quot;, &quot;Vic&quot;, &quot;Vic&quot;,
  83. &quot;Estepona&quot;, &quot;Vejer de la Frontera&quot;, &quot;Senyera&quot;, &quot;Vic&quot;, &quot;Toledo Capital&quot;,
  84. &quot;Borriol&quot;, &quot;Santa Eug&#232;nia de Berga&quot;, &quot;Huelva Capital&quot;, &quot;Sant Celoni&quot;,
  85. &quot;Calafell&quot;, &quot;Fene&quot;, &quot;Almad&#233;n&quot;, &quot;Palma de Mallorca&quot;, &quot;Ourense Capital&quot;,
  86. &quot;Terrassa&quot;, &quot;Terrassa&quot;, &quot;Estepona&quot;, &quot;M&#225;laga Capital&quot;, &quot;Lucena&quot;,
  87. &quot;Calafell&quot;, &quot;Castell&#243;n de la Plana / Castell&#243; de la Plana&quot;,
  88. &quot;Valencia Capital&quot;, &quot;Onda&quot;, &quot;Pamplona / Iru&#241;a&quot;, &quot;Jerez de la Frontera&quot;
  89. ), zona = c(&quot;Pinar - Anaka - Belaskoenea&quot;, &quot;Els Orriols&quot;,
  90. &quot;Barrio de Benicalap&quot;, &quot;Barrio de Benimaclet&quot;, &quot;Parque Central&quot;,
  91. NA, &quot;Mar i Camp - Platja dels Capellans&quot;, NA, &quot;Platja de Llevant&quot;,
  92. &quot;Cerdanyola Sud&quot;, &quot;Can Massuet del Far&quot;, &quot;Levante Alto&quot;,
  93. &quot;Centro - Sagrario&quot;, &quot;Manilva Pueblo&quot;, &quot;Canyamars&quot;, NA, NA,
  94. &quot;Vilapicina i la Torre Llobeta&quot;, &quot;El Puig&quot;, &quot;Llevant&quot;, &quot;Centro&quot;,
  95. NA, &quot;Centro&quot;, &quot;Poble&quot;, &quot;Zona Centro Comercial Torrequebrada&quot;,
  96. NA, &quot;Zona Playa&quot;, &quot;Lledoner&quot;, &quot;Lledoner&quot;, &quot;Lledoner&quot;, NA,
  97. NA, NA, NA, &quot;Casablanca&quot;, &quot;Mejostilla&quot;, &quot;El Poble Sec - Parc de Montju&#239;c&quot;,
  98. NA, &quot;Amara Zaharra - Arbaizenea&quot;, &quot;Campo de Golf - Agua Garc&#237;a - Juan Fern&#225;ndez&quot;,
  99. &quot;Platja de Llevant&quot;, &quot;Plaza de Toros - Santa Rita&quot;, &quot;Playa Muchavista&quot;,
  100. NA, &quot;Mar i Camp - Platja dels Capellans&quot;, NA, &quot;Centre&quot;, &quot;Tres Ventanas&quot;,
  101. &quot;El Grao&quot;, NA, &quot;El Naranjo&quot;, NA, &quot;Oeste&quot;, &quot;La Magdalena&quot;,
  102. &quot;Recoletos&quot;, &quot;Zona Carrefour - Urbanizaciones&quot;, &quot;Sant Roc&quot;,
  103. NA, &quot;Encarnaci&#243;n - Regina&quot;, NA, &quot;Penya - Roja - Avda. Francia&quot;,
  104. &quot;Bel - Air&quot;, &quot;El Montg&#243;&quot;, &quot;Mariola&quot;, &quot;Centre&quot;, &quot;Embajadores - Lavapi&#233;s&quot;,
  105. &quot;Escandinavia - Cautivador&quot;, &quot;Casablanca - Calvario&quot;, &quot;Creu Alta&quot;,
  106. &quot;Son Serra - Sa Vileta&quot;, &quot;Cancelada&quot;, NA, &quot;Bellaterra&quot;, &quot;El Sucre - El Nadal&quot;,
  107. &quot;El Sucre - El Nadal&quot;, &quot;Paraiso - Barronal&quot;, &quot;Vejer&quot;, NA,
  108. &quot;El Sucre - El Nadal&quot;, &quot;Santa B&#225;rbara&quot;, NA, NA, &quot;La Orden&quot;,
  109. NA, &quot;Segur Platja&quot;, NA, NA, &quot;Cala Major&quot;, &quot;Centro&quot;, &quot;Barri del Centre&quot;,
  110. &quot;Ca n&#39;Aurell&quot;, &quot;Cancelada&quot;, &quot;Pinares de San Ant&#243;n&quot;, NA,
  111. &quot;Segur Platja&quot;, &quot;Norte&quot;, &quot;Beter&#243;&quot;, NA, &quot;San Juan&quot;, &quot;El Roc&#237;o - La Milagrosa&quot;
  112. )), row.names = c(NA, -100L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;,
  113. &quot;data.frame&quot;))

EDIT:

My attempt which doesn't work:

  1. myProvLists = data %&gt;%
  2. pull(provincia) %&gt;%
  3. unique()
  4. # UI component for the module
  5. ui_location_selections &lt;- function(id) {
  6. ns &lt;- NS(id)
  7. selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = c(myProvLists))
  8. selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c())
  9. # selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
  10. }
  11. # Server component for the module
  12. server_location_selections &lt;- function(id){
  13. moduleServer(id, function(input, output, session){
  14. observeEvent(input$provinceSelect,{
  15. updateSelectInput(
  16. session,
  17. &quot;municipioSelect&quot;,
  18. choices = data %&gt;% filter(provincia == input$provinceSelect) %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio)
  19. )
  20. })
  21. ## (1) ## first drop down - observe the selection
  22. observeEvent(input$provinceSelect,{
  23. updateSelectInput(
  24. session,
  25. &quot;municipioSelect&quot;,
  26. choices = data %&gt;% filter(provincia == input$provinceSelect) %&gt;% select(municipio) %&gt;% unique() %&gt;% pull(municipio)
  27. )
  28. })
  29. }
  30. )
  31. }
  32. # Call the module in the app
  33. ui &lt;- fluidPage(
  34. ui_location_selections(&quot;provincias&quot;),
  35. ui_location_selections(&quot;municipios&quot;),
  36. renderUI(output$provincias)
  37. )
  38. server &lt;- function(input, output, session, data) {
  39. server_location_selections(&quot;provincias&quot;)
  40. }
  41. shinyApp(ui, server)

答案1

得分: 2

以下是代码部分的翻译:

  1. library(shiny)
  2. library(tidyverse)
  3. library(bslib)
  4. # 数据 &lt;- ...
  5. ui &lt;- fluidPage(
  6. fluidPage(
  7. theme = bs_theme(bootswatch = &quot;minty&quot;),
  8. title = &quot;hi&quot;,
  9. fluidRow(
  10. column(2,
  11. selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = data %&gt;% pull(provincia) %&gt;% unique()),
  12. selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c()),
  13. selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
  14. )
  15. ),
  16. tableOutput(&#39;filteredDataOUT&#39;)
  17. )
  18. )
  19. server &lt;- function(input, output, session) {
  20. # 用于在响应式环境中更新和访问数据的响应式值列表
  21. values &lt;- reactiveValues()
  22. ## 基于 provinceSelect 过滤数据集,并使用剩余选择项更新 municipioSelect
  23. observeEvent(input$provinceSelect,{
  24. values[[&#39;provincia_df&#39;]] &lt;- data %&gt;% filter(provincia == input$provinceSelect)
  25. updateSelectInput(
  26. session, &quot;municipioSelect&quot;,
  27. choices = values[[&#39;provincia_df&#39;]] %&gt;% pull(municipio) %&gt;% unique()
  28. )
  29. })
  30. ## 也根据 municipioSelect 过滤数据集,并使用剩余选择项更新 distritoSelect
  31. observeEvent(input$municipioSelect,{
  32. values[[&#39;municipio_df&#39;]] &lt;- values[[&#39;provincia_df&#39;]] %&gt;% filter(municipio == input$municipioSelect)
  33. updateSelectInput(
  34. session, &quot;distritoSelect&quot;,
  35. choices = values[[&#39;municipio_df&#39;]] %&gt;% pull(distrito) %&gt;% unique()
  36. )
  37. })
  38. # 最终输出由 input$distritoSelect 自动触发
  39. output$filteredDataOUT &lt;- renderTable(
  40. values[[&#39;municipio_df&#39;]] %&gt;% filter(distrito == input$distritoSelect) %&gt;% select(-all_of(c(&quot;provincia&quot;, &quot;municipio&quot;, &quot;distrito&quot;)))
  41. )
  42. }
  43. 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.

  1. library(shiny)
  2. library(tidyverse)
  3. library(bslib)
  4. # data &lt;- ...
  5. ui &lt;- fluidPage(
  6. fluidPage(
  7. theme = bs_theme(bootswatch = &quot;minty&quot;),
  8. title = &quot;hi&quot;,
  9. fluidRow(
  10. column(2,
  11. selectInput(&quot;provinceSelect&quot;, label = &quot;Select Province Data&quot;, choices = data %&gt;% pull(provincia) %&gt;% unique()),
  12. selectInput(&quot;municipioSelect&quot;, label = &quot;Select Municipio Variable&quot;, choices = c()),
  13. selectInput(&quot;distritoSelect&quot;, label = &quot;Select Distrito Variable&quot;, choices = c())
  14. )
  15. ),
  16. tableOutput(&#39;filteredDataOUT&#39;)
  17. )
  18. )
  19. server &lt;- function(input, output, session) {
  20. #reactive value list to update and access data in reactive environments
  21. values &lt;- reactiveValues()
  22. ## filter dataset based on provinceSelect, and update municipioSelect with remaining choices
  23. observeEvent(input$provinceSelect,{
  24. values[[&#39;provincia_df&#39;]] &lt;- data %&gt;% filter(provincia == input$provinceSelect)
  25. updateSelectInput(
  26. session, &quot;municipioSelect&quot;,
  27. choices = values[[&#39;provincia_df&#39;]] %&gt;% pull(municipio) %&gt;% unique()
  28. )
  29. })
  30. ## filter dataset also on municipioSelect, and update distritoSelect with remaining choices
  31. observeEvent(input$municipioSelect,{
  32. values[[&#39;municipio_df&#39;]] &lt;- values[[&#39;provincia_df&#39;]] %&gt;% filter(municipio == input$municipioSelect)
  33. updateSelectInput(
  34. session, &quot;distritoSelect&quot;,
  35. choices = values[[&#39;municipio_df&#39;]] %&gt;% pull(distrito) %&gt;% unique()
  36. )
  37. })
  38. # final output is automaticall triggered by input$distritoSelect
  39. output$filteredDataOUT &lt;- renderTable(
  40. values[[&#39;municipio_df&#39;]] %&gt;% filter(distrito == input$distritoSelect) %&gt;%
  41. select(-all_of(c(&quot;provincia&quot;, &quot;municipio&quot;, &quot;distrito&quot;)))
  42. )
  43. }
  44. 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:

确定