My question is the following. I am trying to create a reactable with R, and I basically want the search bar to be able to global search for multiple strings separated by a whitespace; so for example, I want to be able to insert in the search bar the following: "FOO BAR", and be able to get all the rows that contains (in every order, and in every column) both FOO, and BAR; the terms does not need to be in the same column, but need to be in the same row.
I am struggling to do so; I am following several examples online and found this online: https://github.com/glin/reactable/issues/222 in which a user propose a method (see code below from that question on github) that actually works well concatenating different string with an OR.
library(reactable)
data <- as.data.frame(datasets::Titanic)
myFilterMethod <- JS('function(rows, columnIds, filterValue) {
/*
pattern defines a RegEx text based on the users input.
In this users input, all occurences of spacebar are replaced by an OR sign.
This is done so that if at least one his keywords is true, the row is displayed
However, if the expression ends with an OR sign, the OR sign should be removed.
If this is not done, then expressions ending in a spacebar will end in an OR sign
and will always give true. This is what the second replace does.
*/
const pattern = new RegExp(filterValue.replace(/ /g, "|").replace(/\|$/, ""))
return rows.filter(function(row) {
return columnIds.some(function(columnId) {
return pattern.test(row.values[columnId])
// Use the pattern defined above to test the rows
// and return those that pass the patternn
})
})
}')
reactable(
data, searchable = TRUE,
searchMethod = myFilterMethod)
How can I create a similar thing but concatenating string with AND instead that with OR?
2
Answers
The following idea should work:
The Javascript function
reduce
is your friend here:N.B. I searched for exact matches here (
==
), if you want some flexibility to use some sort of partial matching, you can incorporateregex
in the.some()
function.Update
To allow for a case insensitive ‘live’ search, adapt the code as follows:
This allows for partial matching from the beginning of the string. That is a search string of
ma ch y 3
will matchMale | Child | Yes | 3rd
. It is case insensitive. Depending on your final needs, you may need to further adapt the regular expression.An approach similar to your given one using
regex
searching: An array of the filter values is defined. Then we loop over this array for getting the boolean values which indicates matching in some column and finallyevery()
is used in order to combine it into one boolean.