[英]How can I read selectInput value as a vector or character in shiny app?
[英]How can I store a selectInput in a vector?
我正在嘗試將 selectInput 保存在向量中。
在我的用戶界面中,我有:
selectInput("my_ID", "Search for a name", unique(datatable$names), "Henry")
在我的服務器中,我想將此輸入保存在一個變量中以供以后使用。
這基本上就是我想要的:
selectedNames <- input$my_ID
當然,這行不通。 所以我試過這個:
selectedNames <- reactive(input$my_ID)
但我再次收到此警告:
.getReactiveEnvironment()$currentContext() 中的錯誤:在沒有活動反應上下文的情況下不允許操作。 (你試圖做一些只能從反應式表達式或觀察者內部完成的事情。)
我也嘗試使用觀察者和我在互聯網上找到的不同代碼塊來完成它,但沒有任何效果。
最后,我想使用 selectedNames 是這樣的過濾器:
example <- datatable %>% filter(names %in% selectedNames())
我怎樣才能解決這個問題?
嘗試selectedNames <- reactive({input$my_ID})
然后在應用程序的其余部分使用selectedNames()
。
請參閱反應性 - 概述。
更新
工作測試示例:
library(shiny)
datatable <- data.frame(names = c('Henry', 'Charles', 'Robert', 'William'))
ui <- fluidPage(
selectInput("my_ID", "Search for a name", unique(datatable$names), "Henry")
)
server <- function(input, output, session) {
selectedNames <- reactive({input$my_ID})
observeEvent(req(selectedNames()), {
example <- datatable %>% filter(names %in% selectedNames())
print(example)
})
}
shinyApp(ui, server)
你真的不需要反應式,可以直接使用input$my_ID
,如下所示:
library(shiny)
datatable <- data.frame(names = c('Henry', 'Charles', 'Robert', 'William'))
ui <- fluidPage(
selectInput("my_ID", "Search for a name", unique(datatable$names), "Henry")
)
server <- function(input, output, session) {
observeEvent(req(input$my_ID), {
example <- datatable %>% filter(names %in% input$my_ID)
print(example)
})
}
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.