This device uses expression-ifelse(,,) to simulate the more powerful per-row block-if(){}else{}. The difference is expression-ifelse(,,) can choose per-row what value to express, whereas block-if(){}else{} can choose per-row where to assign multiple values. By simulation we mean: a sequence of quoted mutate expressions are emitted that implement the transform. These expressions can then be optimized into a minimal number of no-dependency blocks by extend_se for efficient execution. The idea is the user can write legible code in this notation, and the translation turns it into safe and efficient code suitable for execution either on data.frames or at a big data scale using RPostgreSQL or sparklyr.

if_else_op(
source,
testexpr,
...,
thenexprs = NULL,
elseexprs = NULL,
env = parent.frame()
)

Arguments

source optree relop node or data.frame. character containing the test expression. force later arguments to bind by name. named character then assignments (altering columns, not creating). named character else assignments (altering columns, not creating). environment to look to.

Value

operator tree or data.frame.

Details

Note: ifebtest_* is a reserved column name for this procedure.

if_else_block

Examples


if (requireNamespace("DBI", quietly = TRUE) && requireNamespace("RSQLite", quietly = TRUE)) {
# Example: clear one of a or b in any row where both are set.
my_db <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
d <- rq_copy_to(
my_db,
'd',
data.frame(i = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
a = c(0, 0, 1, 1, 1, 1, 1, 1, 1, 1),
b = c(0, 1, 0, 1, 1, 1, 1, 1, 1, 1),
edited = NA),
temporary=TRUE, overwrite=TRUE)

optree <- d %.>%
if_else_op(.,
testexpr = qe((a+b)>1),
thenexprs = qae(a %:=% 0,
b %:=% 0,
edited %:=% 1),
elseexprs = qae(edited %:=% 0))
cat(format(optree))

sql <- to_sql(optree, my_db)
cat(sql)

print(DBI::dbGetQuery(my_db, sql))

DBI::dbDisconnect(my_db)
}
#> mk_td("d", c(
#>   "i",
#>   "a",
#>   "b",
#>   "edited")) %.>%
#>  extend(.,
#>   ifebtest_1 := (a + b) > 1) %.>%
#>  extend(.,
#>   a := ifelse(ifebtest_1, 0, a),
#>   b := ifelse(ifebtest_1, 0, b),
#>   edited := ifelse(ifebtest_1, 1, edited)) %.>%
#>  extend(.,
#>   edited := ifelse(!(ifebtest_1), 0, edited)) %.>%
#>  drop_columns(.,
#>    c('ifebtest_1'))
#> SELECT
#>  i,
#>  a,
#>  b,
#>  edited
#> FROM (
#>  SELECT
#>   i,
#>   a,
#>   b,
#>   ( CASE WHEN ( ( NOT ( ifebtest_1 ) ) ) THEN ( 0 ) WHEN NOT ( ( NOT ( ifebtest_1 ) ) ) THEN ( edited ) ELSE NULL END )  AS edited
#>  FROM (
#>   SELECT
#>    i,
#>    ifebtest_1,
#>    ( CASE WHEN ( ifebtest_1 ) THEN ( 0 ) WHEN NOT ( ifebtest_1 ) THEN ( a ) ELSE NULL END )  AS a,
#>    ( CASE WHEN ( ifebtest_1 ) THEN ( 0 ) WHEN NOT ( ifebtest_1 ) THEN ( b ) ELSE NULL END )  AS b,
#>    ( CASE WHEN ( ifebtest_1 ) THEN ( 1 ) WHEN NOT ( ifebtest_1 ) THEN ( edited ) ELSE NULL END )  AS edited
#>   FROM (
#>    SELECT
#>     i,
#>     a,
#>     b,
#>     edited,
#>     ( a + b ) > 1  AS ifebtest_1
#>    FROM (
#>     SELECT
#>      i,
#>      a,
#>      b,
#>      edited
#>     FROM
#>      d
#>     ) tsql_87780361090366401233_0000000000
#>    ) tsql_87780361090366401233_0000000001
#>   ) tsql_87780361090366401233_0000000002
#> ) tsql_87780361090366401233_0000000003
#>     i a b edited
#> 1   1 0 0      0
#> 2   2 0 1      0
#> 3   3 1 0      0
#> 4   4 0 0      1
#> 5   5 0 0      1
#> 6   6 0 0      1
#> 7   7 0 0      1
#> 8   8 0 0      1
#> 9   9 0 0      1
#> 10 10 0 0      1