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

时间:2018-07-12 21:14:39

标签: r shiny selectize.js shiny-reactivity

我有一个非常简单的问题:

为什么在以下代码中无法删除SelectizeInput中最后输入的值?例如。我输入了“ a”,“ b”和“ c”,之后我想将它们全部从SelectizeInput中删除。为什么无法从SelectizeInput中删除最后一个restg值?

    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)

1 个答案:

答案 0 :(得分:1)

您只需要将ignoreNULL = FALSE添加到observeEvent中,以使其“反应”为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)

更新

找到此链接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)