簡體   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