updateSelectInput 在observeEvent 中不起作用

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

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

我不明白更新中缺少什么,除了那部分之外,其他所有内容都相应地工作。我想要做的是,当按下操作按钮时,

print()
应该是所选值“”。

这是我当前使用的代码:

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 = ""
    )
    print(input$country)
    print(input$state)
    print(input$municipality)
  })
  
  observeEvent(input$resetState, {
    updateSelectInput(session, "state", selected = "")
    updateSelectInput(
      session,
      "municipality",
      choices = c("Seleccione" = ""),
      selected = ""
    )
    print(input$country)
    print(input$state)
    print(input$municipality)
  })
  
  observeEvent(input$resetMunicipality, {
    updateSelectInput(
      session,
      "municipality",
      choices = c("Seleccione" = ""),
      selected = ""
    )
    print(input$country)
    print(input$state)
    print(input$municipality)
  })

  # 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]),
                      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]),
                      decreasing = FALSE
                    )),
        selected = ""
      )
    } else {
      updateSelectInput(
        session,
        "municipality",
        choices = c("Seleccione" = ""),
        selected = ""
      )
    }
  })
}
r shiny
1个回答
0
投票

我不知道它为什么起作用,但如果我在

choices = NULL
choices = c("Seleccione" = "")
selectInputs 的 UI 代码中将
state
更改为
municipality
,则单击操作按钮时会重新设置值。

(您可能需要单击按钮两次才能看到值的变化,因为我认为 3 个

print()
在 UI 更新之前执行。)

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