英文:
More intuitive filtering of a single R Shiny datatable column using multiple strings
问题
我有一个类似这样的R Shiny应用程序:
library(shiny)
library(DT)
# 示例数据
data <- data.frame(col1 = c("syncope", "fall", "dizziness", "headache", "back pain"),
col2 = c(1, 2, 3, 4, 5))
# Shiny应用程序
ui <- fluidPage(
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
DT::datatable(data, filter = 'top', options = list(search = list(regex = TRUE)))
})
}
shinyApp(ui, server)
我能够编写它以允许使用多个条件进行过滤,感谢这个问题。但是,正则表达式并不是非常直观。用户需要输入 sync|dizz 来筛选显示syncope和dizziness的行。或者输入 p[ae] 来筛选back pain和syncope。
是否可能以这样的方式编写代码,以便您可以使用空格或单词'OR'来分隔两个字符串,从而实现这一点呢?例如,您可以输入 sync dizz,或者可能是 sync OR dizz。这对用户来说会更直观。
请注意,我是指每个列上方的搜索框(蓝色箭头),而不是类似问题中提到的通用搜索框(红色箭头)。
英文:
I have an R Shiny app like this:
library(shiny)
library(DT)
# Example data
data <- data.frame(col1 = c("syncope", "fall", "dizziness", "headache", "back pain"),
col2 = c(1, 2, 3, 4, 5))
# Shiny app
ui <- fluidPage(
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
DT::datatable(data, filter = 'top',options = list(search = list(regex = TRUE)))
})
}
shinyApp(ui, server)
I was able to code it to this way to allow filtering with multiple conditions, thanks to this question. However, Regex isn't really that intuitive. Someone would have to enter sync|dizz to filter down to the rows that show syncope and dizziness. Or p[ae] to filter on back pain and syncope.
Is it possible to code things in such a way that you could separate 2 strings with a space, or with the word 'OR', to achieve this instead? For instance, you could enter sync dizz, or maybe sync OR dizz. This would be more intuitive for users.
Please note that I am referring to the search boxes that are above each column (blue arrow), not the general Search box that was referred to in a similar question (red arrow).
答案1
得分: 0
以下是翻译好的内容:
这里是对@Stéphane Laurent的出色答案的另一种方法,可能适合您:
对您的代码唯一的更改是将感兴趣的列转换为因子:data$col1 <- as.factor(data$col1)
然后,您将获得一个下拉列表,还可以键入并按回车键以获取更多条目:
library(shiny)
library(DT)
# 示例数据
data <- data.frame(col1 = c("syncope", "fall", "dizziness", "headache", "back pain"),
col2 = c(1, 2, 3, 4, 5))
data$col1 <- as.factor(data$col1)
# Shiny 应用程序
ui <- fluidPage(
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
DT::datatable(data, filter = 'top', options = list(search = list(regex = TRUE)))
})
}
shinyApp(ui, server)
英文:
Here is an alternative approach to the excellent answer of @Stéphane Laurent that could be an option for you:
The only change to your code is to transform the column of interest to factor: data$col1 <- as.factor(data$col1)
The you will get a dropdown list, also typing and hitting enter for more entries is possible:
library(shiny)
library(DT)
# Example data
data <- data.frame(col1 = c("syncope", "fall", "dizziness", "headache", "back pain"),
col2 = c(1, 2, 3, 4, 5))
data$col1 <- as.factor(data$col1)
# Shiny app
ui <- fluidPage(
DT::dataTableOutput("mytable")
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
DT::datatable(data, filter = 'top',options = list(search = list(regex = TRUE)))
})
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论