问题描述
当我在 options = list(create = TRUE)
中使用 selectizeInput
时,我可以手动添加一个值 - 另请参阅 https://selectize.dev/docs.html 和 https://shiny.rstudio.com/gallery/selectize-examples.html 中的示例 3。
如何从服务器代码添加新值?下面的示例使用了一个假设的 updateSelectizeInput
并且预期不起作用。
library(shiny)
ui = fluidPage(
selectizeInput("select",'Select',choices = c("anton","bertha"),options = list(create = TRUE)
),actionButton("settext","Set Text from server")
)
server = function(input,output,session) {
# This code does not work,shows the idea
updateSelectizeInput(session,"select",options = list(value = "Caesar"))
}
shinyApp(ui,function(input,session) {})
解决方法
这是你想要达到的目标吗?
library(shiny)
choices <- c("anton","bertha")
ui = fluidPage(
selectizeInput("select",'Select',choices = choices,options = list(create = TRUE)
),actionButton("settext","Set Text from server")
)
server = function(input,output,session) {
# Your update is appending Caesar to the choices
updateSelectizeInput(session,"select",choices = c(choices,"Caesar"))
}
shinyApp(ui,server)
,
library(shiny)
ui = fluidPage(
selectizeInput("select",'Select or edit manually',choices = c("anton","bertha"),options = list(create = TRUE)
),verbatimTextOutput("showtext",placeholder = TRUE),actionButton("goButton","Get Text from server")
)
server = function(input,session) {
output$showtext = renderPrint({
input$goButton
isolate(input$select)
})
}
shinyApp(ui,server)