R/alt_assign.R
alt_assign.Rd
These alternate assignment operators can be used to perform
multiple assignment (also known as destructuring assignment). These are
imported from the {zeallot} package (see the corresponding help page at zeallot::operator for complete description). They also performs a dplyr::collect()
allowing to get results from dplyr extensions like {dtplyr} for data.tables, or {dbplyr} for databases. Finally these two assignment operators also make sure that the preferred data frame object is returned by using default_dtx()
.
value %->% x
x %<-% value
# Default S3 method
collect(x, ...)
These operators invisibly return value
. collect.default()
simply
return x
.
These assignation operator are overloaded to get interesting
properties in the context of {tidyverse} pipelines and to make sure to always
return our preferred data frame object (data.frame, data.table, or tibble).
Thus, before being assigned, value
is modified by calling
dplyr::collect()
on it and by applying default_dtx()
.
# The alternate assignment operator performs three steps:
# 1) Collect results from dbplyr or dtplyr
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
library(data.table)
#>
#> Attaching package: ‘data.table’
#> The following objects are masked from ‘package:dplyr’:
#>
#> between, first, last
library(dtplyr)
library(svBase)
dtt <- data.table(x = 1:5, y = rnorm(5))
dtt |>
mutate(x2 = x^2) |>
select(x2, y) ->
res
print(res)
#> x2 y
#> <num> <num>
#> 1: 1 -2.437263611
#> 2: 4 -0.005571287
#> 3: 9 0.621552721
#> 4: 16 1.148411606
#> 5: 25 -1.821817661
class(res) # This is a data frame
#> [1] "data.table" "data.frame"
dtt |>
lazy_dt() |>
mutate(x2 = x^2) |>
select(x2, y) ->
res
print(res)
#> Source: local data table [5 x 2]
#> Call: setcolorder(copy(`_DT1`)[, `:=`(x2 = x^2)][, `:=`("x", NULL)],
#> c("x2", "y"))
#>
#> x2 y
#> <dbl> <dbl>
#> 1 1 -2.44
#> 2 4 -0.00557
#> 3 9 0.622
#> 4 16 1.15
#> 5 25 -1.82
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
class(res) # This is NOT a data frame
#> [1] "dtplyr_step_call" "dtplyr_step"
# Same pipeline, but assigning with %->%
dtt |>
lazy_dt() |>
mutate(x2 = x^2) |>
select(x2, y) %->%
res
print(res)
#> # A data.trame: [5 × 2]
#> x2 y
#> <dbl> <dbl>
#> 1 1 -2.44
#> 2 4 -0.00557
#> 3 9 0.622
#> 4 16 1.15
#> 5 25 -1.82
class(res) # res is the preferred data frame (data.table by default)
#> [1] "data.trame" "data.frame"
# 2) Convert data frame in the chosen format using default_dtx()
dtf <- data.frame(x = 1:5, y = rnorm(5))
class(dtf)
#> [1] "data.frame"
res %<-% dtf
class(res) # A data.table by default
#> [1] "data.trame" "data.frame"
# but it can be changed with options("SciViews.as_dtx)
# 3) If the zeallot syntax is used, make multiple assignment
c(X, Y) %<-% dtf # Variables of dtf assigned to different names
X
#> [1] 1 2 3 4 5
Y
#> [1] -0.2473253 -0.2441996 -0.2827054 -0.5536994 0.6289820
# The %->% is meant to be used in pipelines, otherwise it does the same