Learn R Programming

replyr (version 0.2.0)

letp: Wrap expr for magrittr pipeline execution with name substitutions specified in alias.

Description

letp implements a mapping from desired names (names used directly in the expr code) to names used in the data. letp is a specialization of let for use in magrittr pipelines, please see let for details.

Usage

letp(alias, expr, .)

Arguments

alias
mapping from free names in expr to target names to use
expr
magrittr pipeline to prepare for execution
.
argument from magrittr pipeline (do not assign to this)

Value

result of expr executed in calling environment

Details

letp is a variation of let needed only for inline code placed immediately after %>%, as in the example below. expr must start with " . %>% " and should not attempt assignments or other environment sensitive side-effects.

See Also

replyr_mapRestrictCols let

Examples

Run this code

library('dplyr')
d <- data.frame(Sepal_Length=c(5.8,5.7),
                Sepal_Width=c(4.0,4.4),
                Species='setosa',
                rank=c(1,2))

mapping = list(RankColumn='rank',GroupColumn='Species')
d %>% letp(alias=mapping,
         expr={
           . %>% mutate(RankColumn=RankColumn-1)
         })

# letp is only for transient pipelines, to save pipes use let:

f <- let(mapping,
         . %>% mutate(RankColumn=RankColumn-1)
)
d %>% f

Run the code above in your browser using DataLab