r - R闪亮:无法删除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
,以便它“反应”到 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)
推荐阅读
- reactjs - 在 DOM 主机中自定义 React 渲染器的目的是什么?
- html - Pure CSS Hamburger 菜单在消失前调整视口大小时显示
- google-analytics - BigQuery + Google Analytics:计算 SKU 购买的数量。UNNEST 不工作
- typescript - 绑定到函数的 Typescript 泛型类型与绑定到泛型函数的类型
- javascript - 是否可以在不使用反应的情况下将 chrome 扩展弹出窗口注入当前网页?
- python - fctables.com 中的网页抓取
- c++ - 使Windows32 API成员扩展为A,或在vs2019中更改字符集(c++)
- android - OutlineTextField , TextField 在 Jetpack Compose 1.0.0-alpha02 中不起作用
- node.js - error connecting mongodb to nodeapp but works fine on index page
- laravel - 如何通过 Laravel 中的第一个模型从第二个模型中获取搜索数据?