首页 > 解决方案 > shinyWidgets updatePickerInput() 服务器端更新(类似于 updateSelectizeInput 函数)?

问题描述

有谁知道使用shinyWidgets::pickerInput()大量选择但加快渲染速度的方法?(类似于服务器端选择 using updateSelectizeInput()

我最终想要做的是:拥有一个有大量选择(~1M)的shinyWidgets pickerInput()。

问题:加载这么多选择的应用程序需要很长时间。

我知道一种解决方法是使用shiny::selectizeInput()UI 元素,因为有一种巧妙的方法来执行 server-side shiny::updateSelectInput(),这使得初始 UI 元素的加载和随后使用一堆选项的更新运行得非常快(如所述在这篇文章中)

但是,我真的很喜欢它提供的格式选项和灵活性shinyWidgets::pickerInput()

例子:

这可以快速工作并加载,但使用shiny::selectizeInput()而不是shinyWidgets::pickerInput()我想使用的 。

library(shiny)

ui = shiny::fluidPage(
  shiny::uiOutput(outputId = "ui_output")
)

server = function(input, output) {
  
  output$ui_output = shiny::renderUI({
    
    shiny::selectizeInput(
      inputId = "select",
      label = "Select:",
      choices = NULL
    )
    
  })
  
  shiny::observe({
    
    shiny::updateSelectizeInput(
      inputId = "select",
      choices = 1:1000000,
      selected = 1,
      server = TRUE
    )
    
  })
  
}

shiny::shinyApp(ui, server)

当尝试对 做同样的事情时shinyWidgets::pickerInput(),我最初尝试将选项直接加载到 UI 中:

server = function(input, output) {
  
  output$ui_output = shiny::renderUI({
    
    shinyWidgets::pickerInput(
      inputId = "select",
      label = "Select:",
      choices = 1:1000000
    )
    
  })
  
}

并且还尝试使用我在第一个示例中使用的相同逻辑,即最初使用 加载 UI 对象choices = NULL,然后shiny::observe()调用更新 UI 元素的选择(这可行,但速度很慢):

server = function(input, output) {
  
  output$ui_output = shiny::renderUI({
    
    shinyWidgets::pickerInput(
      inputId = "select",
      label = "Select:",
      choices = NULL
    )
    
  })
  
  shiny::observe({
    
    shinyWidgets::updatePickerInput(
      session = getDefaultReactiveDomain(),
      inputId = "select",
      choices = 1:1000000,
      selected = 1
    )
    
  })
  
}

标签: rshinyserver-side-renderingshinywidgets

解决方案


推荐阅读