R语言 闪亮的应用程序进入一个无休止的循环后,快速改变选择输入

m1m5dgzv  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(75)

我有一个闪亮的应用程序的问题,如果用户迅速改变selectInput的应用程序进入一个循环错误,它来回 Shuffle 选择输入独立于用户。摆脱这个bug的唯一方法是关闭应用程序。下面的示例并不理想,但它说明了行为:

library(shiny)
library(tidyverse)

ui <- fluidPage(
  p("The checkbox group controls the select input"),
  checkboxGroupInput("inCheckboxGroup", "Input checkbox",
                     c("1", "2", "3")),
  selectInput("inSelect", "Select input",
              c("1", "2", "3")),
  
  plotOutput(outputId = "plot")
)

server <- function(input, output, session) {
  observe({
    x <- input$inCheckboxGroup
    
    # Can use character(0) to remove all choices
    if (is.null(x))
      x <- character(0)
    
    selected <- ifelse(input$inSelect %in% x, input$inSelect, tail(x, 1))
    
    # Can also set the label and select items
    updateSelectInput(session, "inSelect",
                      label = paste("Select input label", length(x)),
                      choices = x,
                      selected = selected
    )
  })
  
  
  output$plot <-  renderPlot({
    req(input$inSelect)
    
    dat <- as.data.frame(replicate(as.numeric(input$inSelect), rnorm(1e6)))
    
    dat %>%
      gather() %>%
    ggplot() +
      geom_density(aes(x = value)) +
      facet_wrap(~key)
    
  })
  
}

shinyApp(ui, server)
}

选中所有复选框并快速更改故障输入(<1秒)以观察问题。这个问题与检查input$inSelect的当前值以及使用该检查来更新select输入的默认值有关。我的具体应用程序需要这个。据我所知,这是一个同步问题。
有什么可能的方法来克服它?

lyr7nygr

lyr7nygr1#

我相信问题是你对observe的使用。通过不精确地指定您想要observe的内容,只要observe中的 any reactive更改,您就触发一个事件。在您的示例中,这既是复选框组又是选择列表。我认为你只想在复选框组改变时触发更新(至少在这里),而不是在selct输入中的选择改变时。
因此,解决方案很容易。将observe替换为observeEvent(input$CheckBoxGroup,
这限制了仅对复选框组中的更改的React。
这为我解决了问题。
另外请注意,gather已被pivot_longer取代。

相关问题