繁体   English   中英

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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM