![](/img/trans.png)
[英]How to know that an selectizeInput is ready to get updated by updateSelectizeInput?
[英]How do I use input from `selectizeInput` to filter a list of options and then `updateSelectizeInput`?
背景:
我有一個很大的股票代碼列表,27,000 行,我想在 shinyApp 的selectizeInput()
中進行選擇。 由於列表很大,我在updateSelectizeInput()
中使用server = T
。
目的:
我希望選項列表在用戶開始將字符串輸入selectizeInput()
之前不加載/呈現,這樣我就可以返回所有以該字母開頭的符號,以減少輸入中所有 27,000 行的加載。 我希望input$ticker
是觀察到的,然后是觸發過濾代碼邏輯的。 我如何在不使用特定按鈕的情況下實現這一目標?
下圖是
預期 output ,但帶有一個按鈕來產生行為而不是用戶在文本框中。 這與我想要的一致,但是當我在框中鍵入時不會自動開始搜索並且我有不好的代碼味道。
當前邏輯,在觀察者中使用input$ticker
來觸發 df 的選擇並用新的選擇填充updateSelectize()
,但是失敗了並且應用程序評估得太早了?\
嘗試加載選項一次,僅使用上傳按鈕不起作用
代表:
1.
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- {
renderUI(
shiny::fluidRow(
bs4Dash::box(
title = shiny::selectizeInput(
inputId = "ticker",
label = "Ticker:",
choices = NULL,
selected = "AAPL",
options = list(
placeholder = "e.g AAPL",
create = TRUE,
maxOptions = 50L
)
),
actionButton(
inputId = "update",
label = "UPDATE NOW"
),
id = "tickerBox",
closable = F,
maximizable = F,
width = 12,
height = "250px",
solidHeader = FALSE,
collapsible = F
)
)
)
}
server <- function(input, output, session){
choice <- reactive(
tickers[startsWith(tickers$symbol, input$ticker), ]
)
observeEvent(input$update, {
updateSelectizeInput(
session = session,
label = "Ticker:",
inputId ="ticker",
choices = choice(),
server = TRUE
)
})
}
shiny::shinyApp(ui = ui, server = server)
# REPREX for selectize, glitches and `input$ticker` observer causes loop gltich?
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- {
renderUI(
shiny::fluidRow(
bs4Dash::box(
title = shiny::selectizeInput(
inputId = "ticker",
label = "Ticker:",
choices = NULL,
selected = "AAPL",
options = list(
placeholder = "e.g AAPL",
create = TRUE,
maxOptions = 50L
)
),
actionButton(
inputId = "update",
label = "UPDATE NOW"
),
id = "tickerBox",
closable = F,
maximizable = F,
width = 12,
height = "250px",
solidHeader = FALSE,
collapsible = F
)
)
)
}
server <- function(input, output, session){
# updateSelectizeInput(
# session = session,
# label = "Ticker:",
# inputId ="ticker",
# choices = tickers,
# server = TRUE
# )
observeEvent(input$ticker, {
choices <- tickers[startsWith(tickers$symbol, input$ticker), ]
updateSelectizeInput(
session = session,
label = "Ticker:",
inputId ="ticker",
choices = choices,
server = TRUE
)
})
}
shiny::shinyApp(ui = ui, server = server)
# REPREX for selectize
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- {
renderUI(
shiny::fluidRow(
bs4Dash::box(
title = shiny::selectizeInput(
inputId = "ticker",
label = "Ticker:",
choices = NULL,
selected = "AAPL",
options = list(
placeholder = "e.g AAPL",
create = TRUE,
maxOptions = 50L
)
),
actionButton(
inputId = "update",
label = "UPDATE NOW"
),
id = "tickerBox",
closable = F,
maximizable = F,
width = 12,
height = "250px",
solidHeader = FALSE,
collapsible = F
)
)
)
}
server <- function(input, output, session){
# One call to try and load ticker df
observeEvent(input$update, {
updateSelectizeInput(
session = session,
label = "Ticker:",
inputId ="ticker",
choices = ticker,
server = TRUE
)
})
}
shiny::shinyApp(ui = ui, server = server)
你怎么看這樣的事情?
library(shiny)
tickers <- rep(rownames(mtcars), 850)
ui <- fluidPage(
tags$head(
tags$script(
HTML(
'document.addEventListener("keydown", function(e) {
Shiny.setInputValue("key_pressed", e.key);
})'
)
)
),
fluidRow(
column(2, selectizeInput("select", "Select", choices = "")),
column(1, actionButton("btn", "Search"))
)
)
server <- function(input, output, session) {
observeEvent(input$btn, {
req(input$key_pressed)
updateSelectizeInput(session, "select", choices = tickers[startsWith(tickers, input$key_pressed)], server = TRUE)
})
}
shinyApp(ui, server)
基本上我認為不可能只使用放在selectInput
中的單詞,我們需要單獨的input
。 我認為只有在選擇了某個選項之后selectInput
才是真實的( isTruthy()
)(當然它不能是""
),所以我們不能使用任何在某個選項之前作為單詞selectInput
框的東西實際上是選擇的。 我不確定,但如果我是對的,有必要為您想要的內容單獨input
。
但是,如果我們可以假設:
然后我們可以使用"keydown"
事件( keydown )。 現在用戶不需要在selectInput
框中輸入任何內容,她/他只需使用鍵盤上的一個鍵,例如C
(字母大小在這里很重要,因為我們使用的是startsWith()
)然后按下“搜索”按鈕(當然,這個字母仍然可以放在selectInput
框中以模仿您試圖實現的目標)。 我們甚至可以想象沒有按鈕的解決方案,但恐怕在大多數用例中不會推薦它,我的意思是如果用戶可以使用鍵盤與應用程序交互,不僅可以選擇選項,還可以用於其他目的,那么每次用戶使用鍵盤上的鍵時,我們都會重新計算新選項——好吧——什么都沒有。
原來selectizeInput
不接受 df 並且必須是原子向量。 當我使用tickers[[1]]
時,問題似乎解決了,列表不再是 flash。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.