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

Z. *_*ang 2 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)
Run Code Online (Sandbox Code Playgroud)

所以我想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")
}
Run Code Online (Sandbox Code Playgroud)

但是,如果要choises在selectizeInput中进行更新:

#ui
selectizeInput("filter1","Please select from below list", choices = NULL, multiple = TRUE)
Run Code Online (Sandbox Code Playgroud)

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

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

提前致谢!

Z. *_*ang 5

最后,我从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)
})
Run Code Online (Sandbox Code Playgroud)

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