I am doing a filter()
using %in%
but the way dplyr translates the query seems incorrect. In fact, the %in%
operator works fine with more than one value, but it doesn't when only a single element is present.
In my original scenario the filtering values are dynamic, thus I would like to have a function that works in both cases.
my_db <- src_mysql(dbname = "dplyr",
host = "dplyr.csrrinzqubik.us-east-1.rds.amazonaws.com",
port = 3306,
user = "dplyr",
password = "dplyr")
tbl(my_db, "dplyr") %>% filter(carrier %in% c("UA","AA")) #works
tbl(my_db, "dplyr") %>% filter(carrier %in% c("UA")) #doesn't work
My question is a duplicate of multiple selectInput values create unexpected dplyr (postgres) behavior. Seems like this issue is well-known too
I can't offer any insights into why your code fails. But until someone can provide a better solution, here is a simple work-around that provides "a function that works in both cases".
my.carriers <- c("UA","AA")
my.carriers <- c("UA")
if (length(my.carriers)>1) {
tbl(my_db, "dplyr") %>% filter(carrier %in% my.carriers)
} else {
tbl(my_db, "dplyr") %>% filter(carrier == my.carriers)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With