I suggest to use loops for such operations. It is much more versatile, IMHO.
An example httpdf table (also to answer the comment of RxT):
httpdf <- tibble(
payload = c(
"the createor is nice",
"try to create something to select",
"never catch a dropping knife",
"drop it like it's hot",
NA,
"totaly unrelated" ),
other_optional_columns = 1:6 )
I use sapply to loop over the search query and apply each string as an individual pattern to str_detect. This returns a matrix with one column per search query sting and one line per subject string, which can be collapsed to return a logical vector of your desire.
queries1 <-
httpdf[
sapply(
c("create", "drop", "select"),
str_detect,
string = httpdf$payload ) %>%
rowSums( na.rm = TRUE ) != 0, ]
And of course it can be wrapped in a function to use inside a tidyverse filter:
## function
str_detect_mult <-
function( subject, query ) {
sapply(
query,
str_detect,
string = subject ) %>%
rowSums( na.rm = TRUE ) != 0
}
## tidy code
queries1 <- httpdf %>% filter( str_detect_mult( payload, c("create", "drop", "select") ) )
Easily handle word boarders if you want exact word matches (the "\\b" matches a word border and is joined to the start and end of the string):
str_detect_mult_exact <-
function( subject, query ) {
sapply(
query,
function(.x)
str_detect(
subject,
str_c("\\b",.x,"\\b") ) ) %>%
rowSums( na.rm = TRUE ) != 0
}
Easily handle multiple matches (e.g. if you want only lines matching exactly one of the strings, i.e. XOR):
str_detect_mult_xor <-
function( subject, query ) {
sapply(
query,
str_detect,
string = subject ) %>%
rowSums( na.rm = TRUE ) == 1
}
Also works in base R:
## function
str_detect_mult <-
function( subject, query ) {
rowSums(sapply(
query,
grepl,
x = subject ), na.rm = TRUE ) != 0
}
## tidy code
queries1 <- httpdf[ str_detect_mult( httpdf$payload, c("create", "drop", "select") ), ]