R Shiny - 使用 updateSelectizeInput 优化页面加载时间

标签 r shiny shinydashboard

我们 Shiny 的页面有多个 selectizeInput 控件,其中一些控件的下拉框中有很长的列表。因此,初始加载时间非常长,因为它需要为所有 selectizeInput 控件预填充下拉框。

编辑:请看下面的例子,显示加载长列表如何影响页面加载时间。请复制以下代码,直接运行即可查看加载过程。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
selectizeInput("a","filter 1",choices = sample(1:100000, 10000),multiple = T),
selectizeInput("b","filter 2",choices = sample(1:100000, 10000),multiple = T),
selectizeInput("c","filter 3",choices = sample(1:100000, 10000),multiple = T),
selectizeInput("d","filter 4",choices = sample(1:100000, 10000),multiple = T),
selectizeInput("e","filter 5",choices = sample(1:100000, 10000),multiple = T),
selectizeInput("f","filter 6",choices = sample(1:100000, 10000),multiple = T)
                ),
dashboardBody()
)

server <- function(input, output) {
}

shinyApp(ui, server)

所以我想在用户单击某些复选框(如查看更多过滤器)后更新那些selectizeInput。但是,我不知道如何检测它是否已经加载了列表。

为了更清楚地解释这一点,请参阅下面的加载多个数据文件的解决方案。

#ui
checkboxInput("loadData", "load more data?", value = FALSE)

#server
#below runs only if checkbox is checked and it hasn't loaded 'newData' yet
#So after it loads, it will not load again 'newData'

if((input$loadData)&(!exists("newData"))){
    newData<<- readRDS("dataSample.rds")
}

但是,如果是更新selectizeInput中的choises:

#ui
selectizeInput("filter1","Please select from below list", choices = NULL, multiple = TRUE)

如何像检测对象是否存在 exists("newData") 一样找到条件?我试过 is.null(input$filter1$choises) 但它不正确。

感谢对这种情况的任何建议。

提前致谢!

最佳答案

最后我从 RStudio 上的帖子中找到了解决方案。 http://shiny.rstudio.com/articles/selectize.html

# in ui.R
selectizeInput('foo', choices = NULL, ...)

# in server.R
shinyServer(function(input, output, session) {
updateSelectizeInput(session, 'foo', choices = data, server = TRUE)
})

When we type in the input box, selectize will start searching for the options that partially match the string we typed. The searching can be done on the client side (default behavior), when all the possible options have been written on the HTML page. It can also be done on the server side, using R to match the string and return results. This is particularly useful when the number of choices is very large. For example, when there are 100,000 choices for the selectize input, it will be slow to write all of them at once into the page, but we can start from an empty selectize input, and only fetch the choices that we may need, which can be much faster. We will introduce both types of the selectize input below.

关于R Shiny - 使用 updateSelectizeInput 优化页面加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664657/

相关文章:

r - 在Heatmap.2中设置距离矩阵和聚类方法

r - 如何使用 dplyr 获取 NSE 中的变量名称

r Shiny 的导航栏页面将导航栏保持在屏幕顶部

R Shinydashboard 动态菜单选择

r - 调整 Shiny 仪表板侧边栏中的文本位置

html - 如何在 Shiny 和 Shinydashboard 中更改 verbatimTextOutput 的宽度和高度

R Shiny : Display Multiple Inputs and Text

r - 以 Shiny 的方式显示来自本地驱动器的 pdf

r - 如何修复 Shiny 中 esquisserUI 不弹出的过滤器选项?

r - 无法安装 `proj4` 包,因为在标准搜索位置找不到 libproj 和/或 proj_api.h