0

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

为什么在以下代码中无法删除最后输入的值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)
4

1 回答 1

2

您只需要添加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)
于 2018-07-13T09:02:36.323 回答