含特殊字符的DT过滤器在含SelectizeInput的Shiny应用中无法重置的原因
解决DT列过滤器含特殊字符无法清除的问题
问题现象
使用Shiny的DT组件展示数据表时,若某列包含<1.3、>20这类带特殊字符的值,选中这类值作为列过滤器后:
- 点击选中项右侧的小“x”无法移除该过滤条件
- 点击列过滤器框的环形清除按钮,也无法清除该条件
- 若同时选中含特殊字符和普通字符的过滤值,点击清除全部按钮只会移除普通字符的过滤条件,特殊字符的条件会残留
问题根源
页面中存在selectizeInput组件时,其内部逻辑会与DT的input插件产生冲突,导致特殊字符的过滤条件无法被正常清除。
解决方案
将UI中的selectizeInput替换为shinyWidgets包的pickerInput组件,即可解决该冲突问题。
修复后的完整代码
library(DT) library(shiny) library(shinyWidgets) # 新增加载shinyWidgets包 # UI ui <- navbarPage(tabPanel( titlePanel("DT Filters with Characters"), DTOutput("filtered_table"), actionButton("mybutton", "testing"), pickerInput("test1", label = "nothing", choices = c("1")) # 替换selectizeInput为pickerInput )) # Server server <- function(input, output, session) { names <- sample(c("Mike", "Dave", "Anna", "Sara", "John"), 11, replace = TRUE) results <- c("<1.3", ">20", "&50", "\"quoted\"", "'single'","semi;colon", "slash/", "\\backslash", "equal=sign","#hash", "comma,separated") data <- data.frame(Name = names, Result = results, stringsAsFactors = TRUE) output$filtered_table <- renderDT({ datatable( data, plugins = "input", rownames = TRUE, filter = "top", options = list(pageLength = 10) ) }) } shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者Dave916
相关产品推荐
相关产品推荐

