Muhammad Rafif
Muhammad Rafif

Reputation: 53

How to make selectizeinput faster

output$platform <- renderUI({
    dfff <- df()
    selectizeInput(inputId = "platform", "Platform", choices = unique(dfff$Platform), selected = "Facebook") 
  })
  
  output$objective <- renderUI({
    x <- paste0(input$platform)
    dfx <- df() %>% filter(Platform == x)
    choices <- unique(dfx$Objective)
    choices <- choices[!is.na(choices)]
    selectizeInput(inputId = "objective", "Objective", choices = choices)
  })
  output$product <- renderUI({
    x <- paste0(input$platform)
    x2 <- paste0(input$objective)
    dfx2 <- df() %>% filter(Platform == x) %>% filter(Objective == x2)
    selectizeInput(inputId = "product", "Product", choices = unique(dfx2$Product))
  })
  output$campaign <- renderUI({
    x <- paste0(input$platform)
    x2 <- paste0(input$objective)
    x3 <- paste0(input$product)
    dfx3 <- df() %>% filter(Platform == x) %>% filter(Objective == x2) %>%  filter(Product == x3)
    selectizeInput(inputId = "campaign", "Campaign", choices = unique(dfx3$Unique))
  })

Based on this code the purpose is for filtering data table output based on the selectizeinput, it worked but the problem is when the shiny is running for the first time, it loads the data very slowly. The UI pop up an error message first called "[Object] [object]" because the second selectize input and the rest of it are waiting for the first selectize input/above it, I tried using "selected = value" inside the selectizeinput to run the shiny faster in the beginning so the second, third and fourth selectize input is already defined not waiting for the other selectize input value first but the selected value didn't show up. Is there a way to make this function faster?

Upvotes: 0

Views: 83

Answers (1)

Jacob Bumgarner
Jacob Bumgarner

Reputation: 656

You should strive to use server-side select inputs. You have a couple of options to choose from:

shinyWidgets::virtualSelectize

This is my preference, as I really appreciate the shinyWidgets UI and customizability (documentation link).

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  shinyWidgets::virtualSelectInput(
    "platform",
    "Platform:",
    choices = NULL,
    search = TRUE
  )
)

server <- function(input, output, session) {
  shinyWidgets::updateVirtualSelect(
    inputId = "platform",
    choices = c(1:10000),
    selected = 42
  )
}

shinyApp(ui, server)

shiny::selectizeInput with server-side updates

This is another good option with flexible choice rendering customizability. See here for an example of custom choice rendering, and see the selectizeInput documentation for further reading (link).

This is also a good option if you're trying to keep your dependency graph small.

library(shiny)

ui <- fluidPage(
  shiny::selectizeInput(
    "platform",
    "Platform:",
    choices = NULL
  )
)

server <- function(input, output, session) {
  shiny::updateSelectizeInput(
    inputId = "platform",
    choices = c(1:10000),
    server = TRUE,
    selected = 42
  )
}

shinyApp(ui, server)

Upvotes: 0

Related Questions