首页 > 解决方案 > 如何在使用 updateSelectizeInput 进行搜索时防止 selectInput 移动

问题描述

我有一个闪亮的大应用程序,其中有两个selectInput,一个有两个选项,另一个用于updateSelectizeInput获取server = TRUE选项,因为要显示超过 36000 个元素,这会根据第一个中选择的内容而变化selectInput

它有效,但正如您从下面的 gif 中看到的那样,当我输入时,搜索框正在移动。我的猜测是它在服务器端搜索时移动。这可能是什么原因?我试图在一个简单的应用程序中重新创建问题,但是虽然我添加了应用程序的所有相关部分,但在可重现的示例中没有问题。

在此处输入图像描述

这是一个示例代码:

library(shiny)
library(shinyjs)

option_list1<-seq(1,36000,1)
option_list2<-seq(36000, 72000,1)

ui <- fluidPage(
  useShinyjs(),
  selectInput(
    inputId = "scsource",
    label = "Select source of scRNA-seq data",
    choices = list(
      "Carraro et al(2021) - Lung",
      "Reyfman et al(2018) - Lung"
    ),
    selected = NULL,
    multiple = FALSE
  ),
  uiOutput("scgeneinput")
)

server <- function(input, output, session) {
  output$scgeneinput<-renderUI({
    req(input$scsource)
    selectInput(
      "scgene",
      "Select multiple genes",
      multiple = TRUE,
      choices = NULL
    )
  })
  
  observeEvent(input$scsource, {
    if (input$scsource == "Carraro et al(2021) - Lung"){
      delay(500, updateSelectizeInput(session, "scgene", choices = option_list1, server = TRUE))
    }
    else if(input$scsource == "Reyfman et al(2018) - Lung"){
      delay(500, updateSelectizeInput(session, "scgene", choices = option_list2, server = TRUE))
    }
  })
}

shinyApp(ui, server)

另外,我正在使用shinyjs::delay函数,因为没有它,选项就看不到。这不会发生在小型可重现应用程序中。

任何帮助,将不胜感激。

标签: rshinyselectinput

解决方案


推荐阅读