R 闪亮:无法删除 SelectizeInput 中的最后一个值

R shiny: Cannot delete last value in SelectizeInput

我有一个很简单的问题:

为什么在下面的代码中无法删除 SelectizeInput 中最后输入的值?例如。我输入了 "a" "b" & "c" 之后我想从 SelectizeInput 中删除它们。为什么无法从 SelectizeInput 中删除最后一个剩余值?

    library(shiny)

ui <- bootstrapPage(
  textInput(inputId = "txtinput", label = "Insert label", value = "", placeholder = ""),
  actionButton(inputId = "actbtn", label = "Add label"),
  uiOutput('selctInput')
)


server <- function(input, output, session){

  #Create reactive values
  rv <- reactiveValues()

  #When Button is clicked..
  observeEvent(input$actbtn, {

    #Save value in a reactiveValues list
    rv$new.label <- input$txtinput

    #Collect all entries from text input
    rv$labels <- c(rv$labels, rv$new.label)

    #Clear textinput
    updateTextInput(session, "txtinput", value = "") 
  })

  #When selectizeInput changes...
  observeEvent(input$selctInput, {

    #Why is it not possible to completely empty selectizeInput? The last value just remains.
    rv$labels <- input$selctInput
    print(input$selctInput)
  })

  #Add selectizeInput to UI
  output$selctInput <- renderUI({

    selectizeInput(inputId = "selctInput", label= "Reorder / delete labels", 
                   choices = rv$labels, 
                   selected = rv$labels, multiple=TRUE,
                   options = list(plugins = list('remove_button', 'drag_drop')))
  })
}

shinyApp(ui, server)

您只需将 ignoreNULL = FALSE 添加到您的 observeEvent 以便它 "reacts" 成为 NULL 值

#When selectizeInput changes...
  observeEvent(input$selctInput, {

    #Why is it not possible to completely empty selectizeInput? The last value just remains.
    rv$labels <- input$selctInput
    print(input$selctInput)
  }, ignoreNULL = FALSE)

更新:

在找到这个 link https://gist.github.com/pvictor/ee154cc600e82f3ed2ce0a333bc7d015 之后,似乎有一个更简单的方法:

library(shiny)
ui <- fluidPage(
  selectizeInput("select", "Select", c(),
                 multiple = TRUE, options = list(
                   'plugins' = list('remove_button'),
                   'create' = TRUE,
                   'persist' = FALSE)
  ),
  textOutput("out")
)

server <- function(input, output){
  output$out <- renderText({
    input$select
  })
}

shinyApp(ui, server)