当我在selectizeInput
中使用options = list(create = TRUE)
时,我可以手动添加值-另请参阅https://selectize.dev/docs.html和实施例3https://shiny.rstudio.com/gallery/selectize-examples.html.
如何从服务器代码中添加新值?下面的例子使用了一个假设的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, output, 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, output, session) {
output$showtext = renderPrint({
input$goButton
isolate(input$select)
})
}
shinyApp(ui, server)