Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

compound

Assignment pipe


Description

Pipe an object forward into a function or call expression and update the lhs object with the resulting value.

Usage

lhs %<>% rhs

Arguments

lhs

An object which serves both as the initial value and as target.

rhs

a function call using the magrittr semantics.

Details

The assignment pipe, %<>%, is used to update a value by first piping it into one or more rhs expressions, and then assigning the result. For example, some_object %<>% foo %>% bar is equivalent to some_object <- some_object %>% foo %>% bar. It must be the first pipe-operator in a chain, but otherwise it works like %>%.

See Also

Examples

iris$Sepal.Length %<>% sqrt

x <- rnorm(100)

x %<>% abs %>% sort

is_weekend <- function(day)
{
   # day could be e.g. character a valid representation
   day %<>% as.Date
   
   result <- day %>% format("%u") %>% as.numeric %>% is_greater_than(5)
   
   if (result)
     message(day %>% paste("is a weekend!"))
   else
     message(day %>% paste("is not a weekend!"))
   
   invisible(result)
}

magrittr

A Forward-Pipe Operator for R

v2.0.1
MIT + file LICENSE
Authors
Stefan Milton Bache [aut, cph] (Original author and creator of magrittr), Hadley Wickham [aut], Lionel Henry [cre], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.