【问题标题】:How to set text in `selectizeInput` which is not in choices from code如何在“selectizeInput”中设置不在代码选择中的文本
【发布时间】:2021-09-10 19:31:37
【问题描述】:

当我在 selectizeInput 中使用 options = list(create = TRUE) 时,我可以手动添加一个值 - 另请参阅 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, output, session) {})

【问题讨论】:

    标签: r shiny selectize.js


    【解决方案1】:

    这是你想要达到的目标吗?

    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)
    

    【讨论】:

    • 被您琐碎的解决方案惊呆了,我注意到您按原样回答了问题,但问题是错误的。对于那个很抱歉。我已经修改了原始帖子。
    • 哈。是的,你的名声和问题似乎不一致。
    • 我指出了这一点,因为它是对错误问题的正确答案。
    【解决方案2】:
    
    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)
    
    

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      相关资源
      最近更新 更多