我有一个 selectInput,我可以在 a
之间选择任何内容和e
.
我想要一个自动添加的按钮a
, b
,和e
如果我点击它,就会出现选择。
问题: 我不希望它重置输入中已选择的项目。
例如,用户已选择 a
和d
,然后他单击按钮。而不是得到 a
, b
和e
,我希望用户得到a
, b
, d
,和e
因为他已经选择了a
和d
.
问题:
如何修改updateSelectInput()
所以它不会每次都重新初始化,只是将选项添加到已经选择的其他尚未选择的选项中,这里只添加 b
和e
并且不将“重置为 a
、 b
和 e
?
代码:
ui <- shinyUI(fluidPage(
titlePanel("Yo"),
sidebarLayout(
sidebarPanel(
uiOutput("select"),
actionButton("clear","Adding new")
),
mainPanel(
)
)
))
server <- shinyServer(function(input, output,session) {
output$select <- renderUI({
selectizeInput("sel", "The choices",choices = c("a","b","c","d","e"), selected = NULL, multiple = TRUE,options = list())
})
observeEvent (input$clear,{
updateSelectizeInput(session,"sel", selected = c("a","b","e"), options = list())
})
})
# Run the application
shinyApp(ui = ui, server = server)
最佳答案
好吧,我又笨了,这是一个非常简单的修复,
您只需获取变量中的预选参数并将其添加到更新中的选定参数中即可:
observeEvent (input$clear,{
edit <- input$sel
updateSelectizeInput(session,"sel", selected = c(edit,"a","b","e"), options = list())
})
关于r - 在 R Shiny 中更新选择输入而不重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38499569/