R/sql_expr_set.R
sql_expr_set.Rd
Build a query that applies a SQL expression to a set of columns.
sql_expr_set(source, cols, expr)
source | incoming rel_op tree or data.frame. |
---|---|
cols | character, columns to operate in. If a named array names are where results are landed, values names of value columns. |
expr | character or list of character and names, expression to apply to columns "." stands for column value to use. |
rel_op node or data.frame (depending on input).
# WARNING: example tries to change rquery.rquery_db_executor option to RSQLite and back. if (requireNamespace("DBI", quietly = TRUE) && requireNamespace("RSQLite", quietly = TRUE)) { my_db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") RSQLite::initExtension(my_db) old_o <- options(list("rquery.rquery_db_executor" = list(db = my_db))) d <- rq_copy_to(my_db, 'd', data.frame(AUC = c(NA, 0.5, NA), R2 = c(1.0, 0.9, NA), delta = 3, cat = c("a", NA, "c"), stringsAsFactors = FALSE)) # example op_tree <- d %.>% sql_expr_set(., qc(AUC, R2), ". + 1") cat(format(op_tree)) sql <- to_sql(op_tree, my_db) cat(sql) print(DBI::dbGetQuery(my_db, sql)) # ex2 names (but not marked as names) op_tree2 <- d %.>% sql_expr_set(., qc(AUC, R2), ". + 1 + delta") cat(to_sql(op_tree2, my_db)) # ex3 names (also so marked) op_tree3 <- d %.>% sql_expr_set(., qc(AUC, R2), list(". + 1 +", as.name("delta"))) cat(to_sql(op_tree3, my_db)) # cleanup options(old_o) DBI::dbDisconnect(my_db) }#> mk_td("d", c( #> "AUC", #> "R2", #> "delta", #> "cat")) %.>% #> sql_node(., #> sql_expr_set(AUC, R2; . + 1)) #> SELECT #> `AUC` + 1 AS `AUC`, #> `R2` + 1 AS `R2`, #> delta AS `delta`, #> cat AS `cat` #> FROM ( #> SELECT #> `AUC`, #> `R2`, #> `delta`, #> `cat` #> FROM #> `d` #> ) tsql_98041527705993187917_0000000000 #> AUC R2 delta cat #> 1 NA 2.0 3 a #> 2 1.5 1.9 3 <NA> #> 3 NA NA 3 c #> SELECT #> `AUC` + 1 + delta AS `AUC`, #> `R2` + 1 + delta AS `R2`, #> delta AS `delta`, #> cat AS `cat` #> FROM ( #> SELECT #> `AUC`, #> `R2`, #> `delta`, #> `cat` #> FROM #> `d` #> ) tsql_29089998058038316191_0000000000 #> SELECT #> `AUC` + 1 + `delta` AS `AUC`, #> `R2` + 1 + `delta` AS `R2`, #> delta AS `delta`, #> cat AS `cat` #> FROM ( #> SELECT #> `AUC`, #> `R2`, #> `delta`, #> `cat` #> FROM #> `d` #> ) tsql_74079201325635949115_0000000000