Skip to content

Instantly share code, notes, and snippets.

@rkingdc
Created February 28, 2019 20:44
Show Gist options
  • Save rkingdc/19466f30f2e819ca8a88e7f9a45181f2 to your computer and use it in GitHub Desktop.
Save rkingdc/19466f30f2e819ca8a88e7f9a45181f2 to your computer and use it in GitHub Desktop.
Binning Columns in Remote Tables: make rquery join function
.make_db_rquery_join_fn <- function(data, tbl_cuts, db=db, column_to_cut = 'column_to_cut'){
# test rquery connection options
dbopts <- rquery::rq_connection_tests(db)
# create rquery option connection
rqdb <- rquery::rquery_db_info(connection = db,
is_dbi = TRUE,
connection_options = dbopts)
bin_choices <- data %.>%
rquery::select_columns(., c("id", as.character(column_to_cut), 'k_dummy')) %.>%
rquery::natural_join(., tbl_cuts, jointype = "LEFT", by = 'k_dummy') %.>%
rquery::select_rows_se(., qe(cut >= column_to_cut)) %.>%
rquery::project(., groupby = "id", cut_ = min(cut))
annotation_ops <- rquery::natural_join(data, bin_choices, jointype = "LEFT", by = "id")
FUN = function(){
# oprtation will function as long as table and column names are the same
# it's even database agnostic
rquery::materialize(db = rqdb, optree = annotation_ops)
}
return(FUN)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment