Examples

Working with basic functions

Density plot:

rnorm(100) %>>%
  density(kernel = "cosine") %>>%
  plot(col = "blue")

Plot some graphics, run a linear regression, and see the summary of the estimates of linear coefficients.

mtcars %>>%
  (~ par(mfrow=c(1,2))) %>>%
  (~ plot(mpg ~ cyl, data = .)) %>>%
  (~ plot(mpg ~ wt, data = .)) %>>%
  (lm(mpg ~ cyl + wt, data = .)) %>>%
  summary() %>>%
  (coefficients)

The Pipe version:

Pipe(mtcars)$
  .(~ par(mfrow=c(1,2)))$
  .(~ plot(mpg ~ cyl, data = .))$
  .(~ plot(mpg ~ wt, data = .))$
  .(lm(mpg ~ cyl + wt, data = .))$
  summary()$
  .(coefficients)

Working with dplyr

dplyr package (CRAN,GitHub) provides a group of functions that make data transformation much easier. These operators are fully compatible with dplyr and provide higher performance than its default pipe operator.

The following code demonstrates piping with dplyr functions.

library(dplyr)
library(pipeR)

mtcars %>>%
  select(mpg,cyl,disp,hp) %>>%
  filter(mpg <= median(mpg)) %>>%
  mutate(rmpg = mpg / max(mpg)) %>>%
  group_by(cyl) %>>%
  do(data.frame(mean=mean(.$rmpg),median=median(.$rmpg)))

. in the last call is introduced by do(...) rather than %>>%, which also makes it clear the scope of symbols.

Equivalently, Pipe object can do the same thing.

Pipe(mtcars)$
  select(mpg,cyl,disp,hp)$
  filter(mpg <= median(mpg))$
  mutate(rmpg = mpg / max(mpg))$
  group_by(cyl)$
  do(data.frame(mean=mean(.$rmpg),median=median(.$rmpg))) []

Working with rlist

rlist package (CRAN,GitHub) is a set of tools for working with list objects storing non-tabular non-relational, or non-structured data as opposed to data frames. The major functions in this package are intentionally designed to work with pipeline.

The following example deals with the list of developers with names, ages, interests, and programming language they use and the number of years they have been using them.

library(rlist)
library(pipeR)
devs <- 
  list(
    p1=list(name="Ken",age=24,
      interest=c("reading","music","movies"),
      lang=list(r=2,csharp=4,python=3)),
    p2=list(name="James",age=25,
      interest=c("sports","music"),
      lang=list(r=3,java=2,cpp=5)),
    p3=list(name="Penny",age=24,
      interest=c("movies","reading"),
      lang=list(r=1,cpp=4,python=2)))

The following code queries the name of each developer who likes music and uses R, and put stack the results in a data frame.

devs %>>% 
  list.filter("music" %in% interest & "r" %in% names(lang)) %>>%
  list.select(name,age) %>>%
  list.stack

or equivalently using Pipe object:

Pipe(devs)$
  list.filter("music" %in% interest & "r" %in% names(lang))$
  list.select(name,age)$
  list.stack() []

The following example groups the developers by age and for each group it classifies the developers into interest classes and count the number of people in each class.

devs %>>%
  list.group(age) %>>%
  list.map(group ~ group %>>%
      list.class(interest) %>>%
      list.mapv(int ~ length(int)))

or equivalently using Pipe object:

Pipe(devs)$
  list.group(age)$
  list.map(group ~ 
    Pipe(group)$
      list.class(interest)$
      list.mapv(int ~ length(int))$
      value
    )$
  value