我想在每次单击按钮时将输入文本添加到Shiny应用程序的矢量中。这是我正在使用的示例:
library(shiny)
ui <- fluidPage(
textInput(inputId = "inquiry", label = "enter text"),
actionButton(inputId = "searchButton", label = "Run"),
verbatimTextOutput("queryList", placeholder = FALSE)
)
server <- function(input, output, session) {
queryList <- c()
observeEvent(input$searchButton, {
queryList[length(queryList)+1] <- input$inquiry
output$queryList <- renderPrint({
queryList
})
})
}
shinyApp(ui = ui, server = server)
因此,如果输入"item1"
并单击按钮,则输入"item2"
并再次单击按钮,queryList
应该看起来像c("item1", "item2")
,但看起来只需将"item1"
替换为"item2"
。我确定我缺少一些非常简单的内容……queryList[length(queryList)+1]
看起来有些奇怪,但是它在非反应性环境中有效。
答案 0 :(得分:3)
使queryList反应式为我修复它:
library(shiny)
ui <- fluidPage(
textInput(inputId = "inquiry", label = "enter text"),
actionButton(inputId = "searchButton", label = "Run"),
verbatimTextOutput("queryList", placeholder = FALSE)
)
server <- function(input, output, session) {
queryList <- reactiveValues()
queryList$values <- c()
observeEvent(input$searchButton, {
queryList$values[length(queryList$values) + 1] <- input$inquiry
})
output$queryList <- renderPrint({
if (!is.null(queryList$values)) {
queryList$values
}
})
}
shinyApp(ui = ui, server = server)