Skip to contents

%$>% construct operator allows taking apart the elements of a data to build a new data type more quickly. This is done by data masking and access to the original object by the . pronoun from magrittr. Each line is a component of the new data. If the line is an assignment, the new component name is the assigned variable name, otherwise, its position is determined by how many previous variables have been assigned. See examples.

Usage

data %$>% code

Arguments

data

[R object] Any object, specifically data or results of previous pipes.

code

[individual bindings and R commands] Instructions wrapped in curly braces to encapsualte the context of the pipe.

Value

list with fields specified by any assignments inside code.

See also

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

Examples

# use the pipe to build data in steps:
testdf <- tibble::tribble(
  ~x, ~y,
  3, 2,
  5, 9,
  12, 8
)
testdf %$>% {
  minx <- min(x)
  miny <- min(y)
  minxy <- min(minx, miny)
}
#> $minx
#> [1] 3
#> 
#> $miny
#> [1] 2
#> 
#> $minxy
#> [1] 2
#> 

# use the dot pronoun to refer to the entire data
c(5, 9, 10) %$>% {
  min <- min(.)
  max <- max(.)
  whole <- .
}
#> $min
#> [1] 5
#> 
#> $max
#> [1] 10
#> 
#> $whole
#> [1]  5  9 10
#> 

# use curly braces for multiline instructions
testdf %$>% {
  minxy <- {
    minx <- min(x)
    miny <- min(y)
    min(minx, miny)
  }
  maxall <- max(.)
  minall <- min(.)
}
#> $minxy
#> [1] 2
#> 
#> $maxall
#> [1] 12
#> 
#> $minall
#> [1] 2
#> 

# Result of unnamed instructions are used as is
testdf %$>% {
  xval <- x
  list(minx = min(x), miny = min(y), 12)
}
#> $xval
#> [1]  3  5 12
#> 
#> $minx
#> [1] 3
#> 
#> $miny
#> [1] 2
#> 
#> [[4]]
#> [1] 12
#>