updateSelectInput 未更新 Shiny 中的observeEvent 中的值

问题描述 投票:0回答:1

我试图在 R 编程中清除 Shiny 应用程序中的选择,这样当我这样做时,我的选择将返回到 NULL 或空字符串,并且我的地图可以识别它已清除,因此它应该返回到默认值。但是当我选择清除组时,选择输入中的值保持不变,它们不会更新,我不确定为什么。

这是我当前使用的代码:

library(shiny)
library(leaflet)
library(DT)
library(shinythemes)
library(shinyWidgets)y
#sample data frame
tabla_filtro_pais <- data.frame(PAIS=c("México", "México", "México", "Brasil", "México", 
                                       "México", "México", "Colombia", "México", "México",
                                       "México", "México", "México", "México", "México", 
                                       "México", "México", "México", "México", "México", 
                                       "México", "México", "México", "México", "México"),
                                ENTIDAD=c("Jalisco", "Nuevo León", "México", "São Paulo",
                                          "Puebla", "Jalisco", "Puebla", "Antioquia", 
                                          "Jalisco", "Sonora", "Coahuila De Zaragoza", 
                                          "Ciudad De México", "Baja California Sur", 
                                          "Hidalgo", "Nuevo León", "Oaxaca", 
                                          "Veracruz Ignacio De La Llave","Nuevo León", 
                                          "Michoacán De Ocampo", "Yucatán", 
                                          "Puebla", "Hidalgo", "Sinaloa", "Chiapas", "México"),
                                Municipio=c("La Manzanilla De La Paz", "General Bravo", 
                                            "Temoaya", "Cajamar", "Cuautinchán", "Tequila", 
                                            "Teteles De Ávila Castillo", "Sabaneta", "Juchitlán",
                                            "Guaymas", "Matamoros", "Cuauhtémoc", "Loreto", 
                                            "Atotonilco De Tula", "Los Ramones", 
                                            "Santa Cruz Xoxocotlán",  "Tlilapan", "Juárez",     
                                            "Irimbo", "Kanasín", "San José Chiapa", 
                                            "Zapotlán De Juárez", "Concordia", "Suchiapa",
                                            "Temamatla"))
#ui logic
ui <- fluidPage(
  ##tipo de font
  theme = bslib::bs_theme(
    base_font = bslib::font_google("Open Sans"),
    code_font = bslib::font_google("Roboto Mono")
  ),
  ##css para personalizar la pagina
  tags$head(tags$style(
    HTML(
      "
      .navbar .navbar-nav > li > a, .navbar .navbar-brand {
        color: #953735;
      }
      .navbar, .navbar-default {
        background-color: #230206;
        border-color: #953735;
      }
      .leaflet-container {
        background: #D3D3D3;
      }
      /* Color de la tabla y contenidos */
      .dataTables_wrapper {
        color: #230206;
      }
      .shiny-input-container {
        color: #953735;
      }
      /* Custom para radio buttons */
      .btn-custom-class {
      background-color: #EADBDA; /* color de fondo */
      color: #230206; /* color de texto */
      border-color: #953735; /* color de border */
    }
    .btn-custom-class:hover {
      background-color: #D3D3D3; /* color fondo hover */
      color: #230206; /* color letra hover */
    }
    .input-group-btn {
        width: auto;
        vertical-align: middle;
      }
      .btn-xs {
        padding: 1px 5px;
        line-height: 1.2;
        margin-right: 0.5px;
        height: fit-content;
        align-self: strech;
        vertical-align: middle;
      }
      /* colores custom para los CircleMarkerClusters */
      .marker-cluster-large {
      background-color: rgba(170,26,61,0.6) !important;
      }
      .marker-cluster-large div {
      background-color: rgba(149,23,53,0.6) !important;
      }
      .marker-cluster-medium {
      background-color: rgba(202,117,140,0.6) !important;
      }
      .marker-cluster-medium div {
      background-color: rgba(187,77,106,0.6) !important;
      }
      .marker-cluster-small {
      background-color: rgba(239,209,217,0.6) !important;
      }
      .marker-cluster-small div {
      background-color: rgba(231,175,198,0.6) !important;
      }

    "
    )
  )),
  ##pestaña del mapa
  navbarPage(
    title = "Some title",
    id = "nav",
    tabPanel("Some tab name",
             fluidRow(
               column(
                 width = 3,
                 ##primer filtro "País"
                 div(
                   class = "d-flex align-items-center",
                   div(
                     ##boton para resetear el filtro
                     actionButton("resetCountry", label = "X", class = "btn btn-default btn-xs"),
                     style = 'margin-right: 1px;
                   position: relative;
                   top: 7px;'
                   ),
                   div(
                     selectInput(
                       "country",
                       "País",
                       choices = c("Seleccione" = "",
                                   "Brasil","México","Colombia"),
                       width = "280px"
                     ),
                     style = "flex:1;"
                   )
                 ),
                 ##filtro de estado para el pais
                 div(
                   class = "d-flex align-items-center",
                   ##boton para resetear el filtro
                   div(
                     actionButton("resetState", label = "X", class = "btn btn-default btn-xs"),
                     style = 'margin-right: 1px;
                   position: relative;
                   top: 7px;'
                   ),
                   div(
                     selectInput(
                       "state",
                       "Estado/Provincia/Departamento/Región",
                       choices = NULL,
                       width = "280px"
                     ),
                     style = "flex:1;"
                   )
                 ),
                 ##filtro de municipio para estado y pais
                 div(
                   class = "d-flex align-items-center",
                   div(
                     ##boton para resetear el filtro
                     actionButton("resetMunicipality", label = "X", class = "btn btn-default btn-xs"),
                     style = 'margin-right: 1px;
                   position: relative;
                   top: 7px;'
                   ),
                   div(
                     selectInput(
                       "municipality",
                       "Municipio/Provincia/Cantón/Comuna",
                       choices = NULL,
                       width = "280px"
                     ),
                     style = "flex:1;"
                   )
                 ),
                 position = "left"
               ),
               column(width = 9,
                      leafletOutput("map", height = "800px"))
             )),
  )
)
##server logic
server <- function(input, output, session) {
  ###RESET DE FILTROS USANDO EL BOTON
  # reset el filtro de pais
  observeEvent(input$resetCountry, {
    print(input$resetCountry)
    updateSelectInput(session, "country", selected = "")
    updateSelectInput(session,
                      "state",
                      choices = c("Seleccione" = ""),
                      selected = "")
    updateSelectInput(
      session,
      "municipality",
      choices = c("Seleccione" = ""),
      selected = ""
    )
  })
  
  observeEvent(input$resetState, {
    updateSelectInput(session, "state", selected = "")
    updateSelectInput(
      session,
      "municipality",
      choices = c("Seleccione" = ""),
      selected = ""
    )
  })
  
  observeEvent(input$resetMunicipality, {
    updateSelectInput(
      session,
      "municipality",
      choices = c("Seleccione" = ""),
      selected = ""
    )
  })

  # Update Dropdowns
  observeEvent(input$country, {
    if (!is.null(input$country) && input$country != "") {
      updateSelectInput(
        session,
        "state",
        choices = c("Seleccione" = "",
                    sort(##this part is ilustrative so that it shows that the select input 
                      ##on this lines are variable depending on country input
                      unique(tabla_filtro_pais$ENTIDAD[tabla_filtro_pais$PAIS == input$country &
                                                         tabla_filtro_pais$YEAR == aux_year()]),
                      decreasing = FALSE
                    )),
        selected = ""
      )
    } else {
      updateSelectInput(session,
                        "state",
                        choices = c("Seleccione" = ""),
                        selected = "")
      updateSelectInput(
        session,
        "municipality",
        choices = c("Seleccione" = ""),
        selected = ""
      )
    }
  })
  
  observeEvent(input$state, {
    if (!is.null(input$state) && input$state != "") {
      updateSelectInput(
        session,
        "municipality",
        choices = c("Seleccione" = "",
                    sort(##this part is ilustrative so that it shows that the select input 
                      ##on this lines are variable depending on country input and state input
                      unique(tabla_filtro_pais$Municipio[tabla_filtro_pais$ENTIDAD == input$state &
                                                           tabla_filtro_pais$YEAR == aux_year()]),
                      decreasing = FALSE
                    )),
        selected = ""
      )
    } else {
      updateSelectInput(
        session,
        "municipality",
        choices = c("Seleccione" = ""),
        selected = ""
      )
    }
  })
}

我不明白更新中缺少什么,其他一切都相应地工作,并且不使用这些操作按钮的 updateSelectInput 工作得很好。

r shiny
1个回答
0
投票

您的代码无法运行 - 有几个拼写错误和对缺失函数的调用

aux_year()
,但您应该能够通过使用
observe()
来实现您想要的目标,如下所示:

observe({
req(input$country) 
updateSelectInput(<code using input$country>)
})

observeEvent
旨在响应诸如按下
actionButton
之类的事件,这与输入值更改不同。

© www.soinside.com 2019 - 2024. All rights reserved.