繁体   English   中英

根据用户输入的闪亮的服务器端建议

[英]Shiny serverside suggestions according to user typing

pickerInput根据用户开始输入的内容更新选择器输入的选择。 类似于您开始使用 Google 打字时发生的情况。

在此处输入图像描述

这些建议必须在服务器端处理。

下面是我的代码。 问题是用户输入的内容——如果不是现有的选择——没有发送到服务器。 有没有办法发送用户输入的内容?

也许pickerInput不是正确的方法? 我还能怎么做到这一点?

library(shiny)
library(shinyWidgets)

suggest <- function(x) {
  # would in reality send whatever the user starts typing to an API that returns suggestions
  choices <- c("Some", "One", "Suggests", "This", "According", "To", "Input")
  choices[grep(x, choices, ignore.case = T)]
}

ui <- fluidPage(
  pickerInput(inputId = "id1",
              choices = c(),
              options = list(`live-search` = T))
)

server <- function(input, output, session) {
  observe({
    req(input$one)
    updatePickerInput(session, inputId = "id1", choices = suggest())
  })
}

shinyApp(ui, server)

您可以使用shiny::selectizeInput()来实现您想要的:

library(shiny)

# Generate arbitrarily thousands of choices which can only be rendered 
# serverside to avoid app slowdown:
k <- expand.grid(col1 = letters, col2 = letters, col3 = LETTERS)
choices <- with(k, paste0(col1, col2, col3))

ui <- fluidPage(
  selectizeInput(
    inputId = "selector", 
    label = "Selector", 
    choices = NULL
  )
)

server <- function(input, output, session) {
  observe({
    updateSelectizeInput(
      session = session,
      inputId = "selector",
      choices = choices, 
      server = TRUE
    )
  })
}

shinyApp(ui, server)

reprex 包(v2.0.1)于 2022-07-23 创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM