使用多个字符串更直观地筛选单个 R Shiny 数据表列

huangapple go评论98阅读模式
英文:

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 来筛选显示syncopedizziness的行。或者输入 p[ae] 来筛选back painsyncope

是否可能以这样的方式编写代码,以便您可以使用空格或单词'OR'来分隔两个字符串,从而实现这一点呢?例如,您可以输入 sync dizz,或者可能是 sync OR dizz。这对用户来说会更直观。

请注意,我是指每个列上方的搜索框(蓝色箭头),而不是类似问题中提到的通用搜索框(红色箭头)。

使用多个字符串更直观地筛选单个 R Shiny 数据表列

英文:

I have an R Shiny app like this:

library(shiny)
library(DT)

# Example data
data &lt;- data.frame(col1 = c(&quot;syncope&quot;, &quot;fall&quot;, &quot;dizziness&quot;, &quot;headache&quot;, &quot;back pain&quot;),
                   col2 = c(1, 2, 3, 4, 5))

# Shiny app
ui &lt;- fluidPage(
  DT::dataTableOutput(&quot;mytable&quot;)
)

server &lt;- function(input, output) {
  output$mytable &lt;- DT::renderDataTable({
    DT::datatable(data, filter = &#39;top&#39;,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).

使用多个字符串更直观地筛选单个 R Shiny 数据表列

答案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)

使用多个字符串更直观地筛选单个 R Shiny 数据表列

英文:

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 &lt;- 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 &lt;- data.frame(col1 = c(&quot;syncope&quot;, &quot;fall&quot;, &quot;dizziness&quot;, &quot;headache&quot;, &quot;back pain&quot;),
                   col2 = c(1, 2, 3, 4, 5))

data$col1 &lt;- as.factor(data$col1)

# Shiny app
ui &lt;- fluidPage(
  DT::dataTableOutput(&quot;mytable&quot;)
)

server &lt;- function(input, output) {
  output$mytable &lt;- DT::renderDataTable({
    DT::datatable(data, filter = &#39;top&#39;,options = list(search = list(regex = TRUE)))
  })
}

shinyApp(ui, server)

使用多个字符串更直观地筛选单个 R Shiny 数据表列

huangapple
  • 本文由 发表于 2023年2月9日 03:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定