
Alternate assignment (multiple and/or collect results from dplyr)
Source: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()
.
Arguments
- value
The object to be assigned.
- x
A name, or a name structure for multiple (deconstructing) assignment, or any object that does not have a specific [dplyr::collect[]) method for
collect.default()
.- ...
further arguments passed to the method (not used for the default one)
Details
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()
.
Examples
# 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 -1.400043517
#> 2: 4 0.255317055
#> 3: 9 -2.437263611
#> 4: 16 -0.005571287
#> 5: 25 0.621552721
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 -1.40
#> 2 4 0.255
#> 3 9 -2.44
#> 4 16 -0.00557
#> 5 25 0.622
#>
#> # 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)
#> x2 y
#> <num> <num>
#> 1: 1 -1.400043517
#> 2: 4 0.255317055
#> 3: 9 -2.437263611
#> 4: 16 -0.005571287
#> 5: 25 0.621552721
class(res) # res is the preferred data frame (data.table by default)
#> [1] "data.table" "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.table" "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] 1.1484116 -1.8218177 -0.2473253 -0.2441996 -0.2827054
# The %->% is meant to be used in pipelines, otherwise it does the same