Skip to contents

%to% allows a single object to be passed through multiple commands, and for the results to be returned in a single dataframe.

Usage

obj %to% blocks

Arguments

obj

Object to be passed to multiple instructions

blocks

formula-like instructions wrapped in a pair of braces {}.

Value

Dataframe with column names being the formula LHS values, and a single row containing the results of each column's respective instructions.

Passing object to multiple instructions:

The syntax to use for this purpose is to have the object on the LHS of the %to% operator, and wrap all instructions in a braces pair that effectively acts as multiple lines of regular R code. However, each line needs to be named, in a formula syntax fashion:

obj %to% {
 inst1 ~ inst1expressions(.)
 inst2 ~ inst2expressions(.)
}

The right side of the formula is to be specified similar to how instructions in purrr:map are, that is, by the ., .x syntax.

If multiple lines are needed for each instruction, one can simply wrap them in yet another braces pair.

See also

Other result assemblers: %$>%(), %->%(), %<-%(), conserve(), control()

Examples

# returning multiple analysis on same object
testvec <- c(1, 2, 3, 7)
testvec %to% {
  mean ~ mean(.)
  sd ~ sd(.)
}
#> # A tibble: 1 × 2
#>    mean    sd
#>   <dbl> <dbl>
#> 1  3.25  2.63

# use nested braces for longer expressions
testvec %to% {
  mean2 ~ {
    x <- . + 3
    mean(x * .)
  }
  sd ~ sd(.)
}
#> # A tibble: 1 × 2
#>   mean2    sd
#>   <dbl> <dbl>
#> 1  25.5  2.63

# vector results are stored as lists in the dataframe
testvec %to% {
  itself ~ .
  itsmean ~ mean(.)
}
#> # A tibble: 1 × 2
#>   itself    itsmean
#>   <I<list>>   <dbl>
#> 1 <dbl [4]>    3.25

testvec %to% {
  itself ~ .
  itsextra ~ c(2, .)
}
#> # A tibble: 1 × 2
#>   itself    itsextra 
#>   <I<list>> <I<list>>
#> 1 <dbl [4]> <dbl [5]>