Shiny selectInput very slow on larger data (~15,000 entries) in browser

北城余情 提交于 2020-04-05 07:30:10

问题


I have this basic shiny app, and it is blazing fast in the 'Viewer', but when I use the 'Open in Browser' option, the select input choices take a while to load.

selectList <- sapply(1:15000, function(x) paste(sample(letters, 10), collapse = ''))
ui <- fluidPage(
  selectInput('mylist', 'Select Something',
              choices = c(Choose = '', selectList),
              selected = 1)
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

According to this thread - https://groups.google.com/forum/#!topic/shiny-discuss/doHpFM6ZOGg, the issue had a fix in some old private branch. Latest install I am using is this and I see the slowness problem.

packageVersion('shiny')
[1] ‘0.13.2’

Any options I have to make this behave differently?

Additional need:

I would also like the selectized inputs to be dependent on a radio button input as follows. But, for some reason, I can't make the server side selectize input to work with observeEvent on the radio button. Any thoughts on what I am doing wrong?

# mylist
selectList1 <- sapply(1:15000, function(x) paste0(x, "_", paste(sample(LETTERS, 10), collapse = '')))
selectList2 <- sapply(1:15000, function(x) paste0(x, "_", paste(sample(letters, 10), collapse = '')))

# ui
ui <- fluidPage(
  selectizeInput(
    inputId = 'mylist', label = 'Select Something',
    choices = NULL,
    selected = 1
  ),
  radioButtons('letterType',
               'Select a Letter Type:',
               choices = c('Upper Case' = 'upper',
                           'Lower Case' = 'lower'),
               selected = 'upper',
               inline = TRUE)
)

# server
server <- function(input, output, session) {
  selectListReactive <- reactive({
    validate(need(is.null(input$letterType), FALSE))
    if (input$letterType == 'upper')
      selectList1
    else
      selectList2
  })
  observeEvent(input$letterType, {
    updateSelectizeInput(session = session, inputId = 'mylist',
                         choices = c(Choose = '', selectListReactive()),
                         server = TRUE)
  })
}

# app
shinyApp(ui = ui, server = server)

回答1:


Hi try to put the choices in the server with updateSelectizeInput and use server = TRUE to store the choices server-side, e.g. :

library("shiny")
# mylist
selectList <- sapply(1:15000, function(x) paste0(x, "_", paste(sample(letters, 10), collapse = '')))
# ui
ui <- fluidPage(
  selectizeInput(
    inputId = 'mylist', label = 'Select Something',
    choices = NULL,
    selected = 1
  )
)
# server
server <- function(input, output, session) {
  updateSelectizeInput(session = session, inputId = 'mylist', choices = c(Choose = '', selectList), server = TRUE)
}
# app
shinyApp(ui = ui, server = server)

You have to use selectizeInput and not selectInput for this to work




回答2:


I did it faster with data.table package:

library(data.table)
selectList <- as.data.table(sapply(1:15000, function(x) paste(sample(letters, 10), collapse = '')))
ui <- fluidPage(
  selectInput('mylist', 'Select Something',
              choices = c(Choose = '', selectList),
              selected = 1)
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

It can be worth



来源:https://stackoverflow.com/questions/38438920/shiny-selectinput-very-slow-on-larger-data-15-000-entries-in-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!