在具有多个条目的R闪亮数据表中搜索列
我在R Shiny中有一个数据表,其中一列关键字"包含某些行的多个条目,并用逗号分隔.我希望这些多个条目可以单独搜索.数据表的默认搜索功能将这些条目视为一个很长的单项.
I have a datatable in R Shiny, and one of the columns, Keywords, contains multiple entries for some rows, separated by a comma. I would like these multiple entries to be individually searchable. The default search feature for datatable treats these entries as one, long, single item.
例如,关键字"列的第2行的值为"Keyword1,Keyword2".我希望用户能够搜索"Keyword1"或"Keyword2"并找到第2行.当前,数据表中的默认搜索栏将此项视为一项:"Keyword1,Keyword2",仅允许人们搜索将关键字1,关键字2"作为一个联合项,而不是两个单独的值.
For example, row 2's value for the column Keywords is "Keyword1, Keyword2". I would like the user to be able to search for either "Keyword1" OR "Keyword2" and find row 2. Currently, the default search bar in datatable treats this entry as one item: "Keyword1, Keyword2" and only allows people to search for "Keyword1, Keyword2" as one, joint item, not two separate values.
这是问题的一个小例子,可重现
Here is a small, reproducible example of the problem
library(shiny)
library(DT)
## Create an example dataset with 3 rows and 1 column
dat <- matrix(c("Keyword1", "Keyword1, Keyword2", "Keyword2"), nrow = 3, ncol = 1)
colnames(dat) <- "Keywords"
dat <- data.frame(dat)
ui <- shinyUI(
DT::dataTableOutput('ex1')
)
server <- shinyServer(function(input, output) {
output$ex1 <- DT::renderDataTable(
DT::datatable(dat, filter = "top")
)
})
shinyApp(ui = ui, server = server)
也许您可以尝试以下方法:
Perhaps you can try something like this:
keys <- c("Keyword1", "Keyword1, Keyword2", "Keyword2")
grepl(paste("Keyword1", collapse = "|"), keys)
#output
[1] TRUE TRUE FALSE
grepl(paste("Keyword2", collapse = "|"), keys)
#output
[1] FALSE TRUE TRUE
由于我没有要复制的任何代码,所以我想出了这个示例,您可以使用用户输入以闪亮的方式扩展到数据表.
Since I do not have any code to reproduce, I just came up with this example which you can extend to the datatable in shiny using the user inputs.
根据您的情况,您可以执行以下操作:
In your case you can do something like:
filter <- grepl(paste(input$keys, collapse = "|"), data$keywords)
我假设 keys
是用户输入框的 ID
,而 keywords
是数据表 data中的列代码>.然后,您可以使用
filter
相应地对数据进行子集化.
I am assuming keys
is the ID
for the user input box and keywords
is the column in the datatable data
. You can then use filter
to subset your data accordingly.
问题与样本数据集有关,并且假设与您的情况相同,那就是具有过滤器的列是 Factor
.如果使用 as.character()
将列转换为 Character
,则您的代码将正常工作.当您在过滤器中搜索 keyword1
时,只会导致其中包含 keyword1
的那些行.
The problem, atleast with the sample dataset and assuming the same in your case, is that the column which has a filter is a Factor
. If you convert the column to a Character
using as.character()
, your code will work fine. When you search for keyword1
in the filter, it will result in only those rows that have keyword1
in them.
library(shiny)
library(DT)
## Create an example dataset with 3 rows and 1 column
dat <- matrix(c("Keyword1", "Keyword1, Keyword2", "Keyword2"), nrow = 3, ncol = 1)
colnames(dat) <- "Keywords"
dat <- data.frame(dat)
dat$Keywords <- as.character(dat$Keywords)
ui <- shinyUI(
DT::dataTableOutput('ex1')
)
server <- shinyServer(function(input, output) {
output$ex1 <- DT::renderDataTable(
DT::datatable(dat, filter = "top")
)
})
shinyApp(ui = ui, server = server)