如何使闪亮的输入相互观察和重置

How to make shiny inputs observe and reset each other

我正在尝试制作一个闪亮的应用程序。
当我更改单个输入(cyl)时,我希望第二个输入(齿轮)重置为空白值。 (反之亦然)。
我的问题是,当我重置单个输入时,它似乎会重置两个输入。
换句话说,将 (cyl) 更改为 6 会将 (gear) 重置为空白,但这也会将 (cyl) 重置为空白。
最后一种行为是我要避免的。
感谢您的帮助。

这是一个代表。

library(shiny)

df <- mtcars

ui <- fluidPage(
  
  uiOutput("cyl_select")  ,
  uiOutput("gear_select") 
  
  
  
)

server <- function(input, output, session) {
  
  output$cyl_select <- renderUI({
    
    selectInput("cyl_select", "cyl_select", unique(c(" ", df$cyl)), selected = c(" "))
    
  })
  
  observeEvent(input$gear_select != " ", 
               {
                 
                 updateSelectInput(session, "cyl_select", selected = c(" "))
                 
               })
  
  
  output$gear_select <- renderUI({
    
    
    selectInput("gear_select", "gear_select", unique(c(" ", df$gear)), selected = c(" "))

    
  })
  
  
  observeEvent(input$cyl_select != " ", 
               {
                 
                 updateSelectInput(session, "gear_select", selected = c(" "))
                 
               })

}

shinyApp(ui = ui, server = server)

您需要将 != " " 检查移动到观察者函数中。这应该有效:

library(shiny)    
df <- mtcars    
ui <- fluidPage(      
  uiOutput("cyl_select"),
  uiOutput("gear_select")
)

server <- function(input, output, session) {      
  output$cyl_select <- renderUI({        
    selectInput("cyl_select", "cyl_select", unique(c(" ", df$cyl)), selected = c(" "))        
  })
  
  observeEvent(input$gear_select, {
                if (input$gear_select  != " ")  {
                  updateSelectInput(session, "cyl_select", selected = c(" "))
                }                     
               })
  
  
  output$gear_select <- renderUI({        
    selectInput("gear_select", "gear_select", unique(c(" ", df$gear)), selected = c(" "))      
    
  })
  
  
  observeEvent(input$cyl_select, {
                 if (input$cyl_select != " ") {
                    updateSelectInput(session, "gear_select", selected = c(" "))
                 }                     
               })      
}

shinyApp(ui = ui, server = server)