Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter by a string containing variables in dbplyr [duplicate]

Tags:

r

dplyr

dbplyr

I normally use filter with grepl in dplyr, but when using dbplyr. I get an error that grepl is not a recognized function. My guess is that it can't translate to SQL server. What is a way around this with dbplyr

Here is a reproducible example

library(dbplyr)
library(nycflights13)

## Working chunk
con <-DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbWriteTable(con, "flights", flights)
DBI::dbGetQuery(con, "SELECT origin, flight 
FROM flights WHERE origin like '%jf%'")
## End working chunk

## The below code does not work 
flights <- tbl(con,"flights")
flights %>% 
  select(origin, flight) %>% 
  filter(grepl('jf', origin))
like image 519
Alex Avatar asked Sep 07 '17 12:09

Alex


1 Answers

I found the solution from this answer effective.

Here is the code that works for your case:

dplyr::tbl(con, "flights") %>% 
    filter(origin %like% '%jf%') %>%
    collect()
like image 159
nickv Avatar answered Sep 20 '22 23:09

nickv