7

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))
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
Alex
  • 2,603
  • 4
  • 40
  • 73
  • 1
    What flavor of SQL? Wrapping the call in `do` sometimes works. – alistaire Sep 07 '17 at 12:58
  • MS SQL Server. Can you explain `do` or link to an example? – Alex Sep 07 '17 at 13:05
  • 1
    Something like `iris %>% do(mutate(., is_setosa = grepl('set', Species)))` It's probably not the best way to do things, though; I'm sure there's a more native solution. – alistaire Sep 07 '17 at 13:20

3 Answers3

8

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()
nickv
  • 81
  • 1
  • 4
2

I frequently use grepl with a pipe to match multiple values. For postgresql If you want to match multiple values similar to will also work:

dplyr::tbl(con, "flights") %>% 
    filter(origin %similar to% '(JF|LG)%') %>%
    collect()
mrjoh3
  • 437
  • 2
  • 11
1

Im not quiet sure what your asking but have u tried any at functions?

eg..

mtcars %>% mutate_at(vars(matches("cyl")), funs("123" = .+1))
chrk623
  • 76
  • 1
  • 5