问题
Our shiny page has multiple selectizeInput
controls, and some of them have long lists in their drop-down boxes. Because of this, the initial loading time is very long as it needs to pre-fill drop-down boxes for all selectizeInput
controls.
Edit: Please see below example showing how loading long lists impacts page loading time. Please copy below codes and run directly to see loading process.
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)
So I am thinking to update those selectizeInput
after user clicks certain checkbox like see more filters
. However, I don't know how to detect whether it has already loaded the lists.
To explain this more clear, please see below solution for loading multiple data files.
#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")
}
However, if it is to update choises
in selectizeInput:
#ui
selectizeInput("filter1","Please select from below list", choices = NULL, multiple = TRUE)
How do I find a condition like I did for detecting whether object exsits exists("newData")
? I tried is.null(input$filter1$choises)
but it isn't correct.
Appreciate any suggestions for this situation.
Thanks in advance!
回答1:
Finally I found the solution from the post on 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.
来源:https://stackoverflow.com/questions/35664657/r-shiny-optimize-page-loading-time-with-updateselectizeinput