如何使selectizeinput更快

huangapple go评论70阅读模式
英文:

How to make selectizeinput faster

问题

output$platform <- renderUI({
    dfff <- df()
    selectizeInput(inputId = "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", "目标", choices = choices)
  })
  output$product <- renderUI({
    x <- paste0(input$platform)
    x2 <- paste0(input$objective)
    dfx2 <- df() %>% filter(Platform == x) %>% filter(Objective == x2)
    selectizeInput(inputId = "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", "活动", choices = unique(dfx3$Unique))
  })
英文:
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?

答案1

得分: 0

shinyWidgets::virtualSelectize

这是我的首选,因为我非常欣赏shinyWidgets的用户界面和可自定义性(文档链接)。

shiny::selectizeInput 带有服务器端更新

这是另一个不错的选择,具有灵活的选项渲染自定义性。请参阅此处以查看自定义选项渲染的示例,并查看selectizeInput文档以获取更多信息(链接)。

这也是一个不错的选择,如果你希望保持依赖关系图较小。

英文:

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)

huangapple
  • 本文由 发表于 2023年3月31日 15:50:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定