Skip to content

Instantly share code, notes, and snippets.

@mndrake
Last active August 24, 2016 00:26
Show Gist options
  • Save mndrake/16dba08498fd371cd7806ddc63d01199 to your computer and use it in GitHub Desktop.
Save mndrake/16dba08498fd371cd7806ddc63d01199 to your computer and use it in GitHub Desktop.
extension methods for dplyr remote tables
library(dplyr)
library(nycflights13)
conn <- nycflights13_sqlite()
flights_sql <- tbl(conn, 'flights')
weather_sql <- tbl(conn, 'weather')
`[.tbl_sql` <- function(x, condition) {
x %>% select_(condition)
}
sum.tbl_sql <- function(x, na.rm = FALSE) {
names <- colnames(x)
if (length(names) > 1) {
stop("sum needs a single column for remote tables")
}
x %>% rename_(x = as.name(names[1])) %>% summarize(sum(x)) %>% collect %>% as.numeric
}
sum(flights_sql['dep_delay'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment