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

时间:2016-02-27 01:34:18

标签: r shiny shinydashboard

我们的闪亮页面有多个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之类的特定复选框后更新这些see more filters。但是,我不知道如何检测它是否已经加载了列表。

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

#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),但这不正确。

感谢对此情况的任何建议。

提前致谢!

1 个答案:

答案 0 :(得分:4)

最后,我在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)
})
  

当我们输入输入框时,selectize将开始搜索与我们输入的字符串部分匹配的选项。当所有可能的选项都写在HTML页面上时,可以在客户端进行搜索(默认行为)。它也可以在服务器端完成,使用R匹配字符串并返回结果。当选择的数量非常大时,这尤其有用。例如,当选择输入有100,000个选项时,将所有这些选项一次性写入页面会很慢,但我们可以从空的选择输入开始,只获取我们可能需要的选项,这可以要快得多。我们将在下面介绍两种类型的选择输入。

相关问题