Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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)
like image 265
Gopala Avatar asked Jul 18 '16 14:07

Gopala


2 Answers

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

like image 157
Victorp Avatar answered Oct 06 '22 07:10

Victorp


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 = '')))
names(selectList) = ""

ui <- fluidPage(
  selectInput('mylist', 'Select Something',
              choices = c(Choose = '', selectList),
              selected = 1)
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)
like image 10
dangulo Avatar answered Oct 06 '22 09:10

dangulo