如何在`selectizeInput`中设置不在代码选择中的文本

问题描述

当我在 options = list(create = TRUE) 中使用 selectizeInput 时,我可以手动添加一个值 - 另请参阅 https://selectize.dev/docs.htmlhttps://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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...