Pipe: Create a Pipe object that stores a value and allows command...

Description Usage Arguments Details Value Examples

Description

Create a Pipe object that stores a value and allows command chaining with $.

Usage

1
Pipe(value = NULL)

Arguments

value

value to pipe (default is NULL)

Details

Pipe() function creates a Pipe object that provides object-like command chaining mechanism, which avoids using external operator and can be cleaner than operator-based pipline.

Pipe() creates a Pipe object that allows using $ to perform first-argument piping, call .() to evaluate an expression with . or symbol defined by lambda expression, for side effect, or simply extract an element from the stored value. $value or [] ends a pipeline and extracts its final value.

The functionality of Pipe object fully covers that of the pipe operator %>>% and provides more features. For example, Pipe object supports directly subsetting $value by [...], extracting element by [[...]], and assigning value by $item <-, [...] <-, and [[...]] <-.

A typical usage of Pipe object is to start with Pipe() and end with $value or [].

print() and str() are implemented for Pipe object. Use header = FALSE to suppress Pipe header message in printed results. Use options(Pipe.header = FASLE) to suppress it globally.

If the Pipe object is used in more than one pipelines, a recommended usage is to name the object specially so that it is easy to distinguish the Pipe object from the value it stores. For example, it can start with p.

Value

Pipe object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
## Not run: 
# Pipe as first-argument using $
Pipe(rnorm(100))$mean()
Pipe(rnorm(100))$plot(col="red")

# Extract the value from the Pipe object using []
Pipe(rnorm(100))$c(4,5) []

# Pipe to an exrepssion with . or symbol defined in
# lambda expression to represent the object
Pipe(rnorm(100))$.(1 + .) []
Pipe(rnorm(100))$.(x ~ 1 + x) []

# Pipe for side effect
Pipe(rnorm(100))$
  .(~ cat("number:",length(.),"\n"))$
  summary()

Pipe(rnorm(100))$
  .(~ x ~ cat("number:",length(x),"\n"))$
  summary()

# Assignment
Pipe(rnorm(100))$
  .(~ x)$
  mean()

Pipe(rnorm(100))$
  .(~ x <- length(.))$
  mean()

Pipe(rnorm(100))%
  .(x <- c(min(.),max(.)))$
  mean()

# Extract element with \code{.(name)}
Pipe(mtcars)$lm(formula = mpg ~ cyl + wt)$.(coefficients)

# Command chaining
Pipe(rnorm(100,mean=10))$
  log()$
  diff()$
  plot(col="red")

Pipe(rnorm(100))$
  density(kernel = "rect")$
  plot(col = "blue")

# Store an continue piping
pipe1 <- Pipe(rnorm(100,mean=10))$log()$diff()
pipe1$plot(col="red")

# Subsetting, extracting, and assigning

p <- Pipe(list(a=1,b=2))
p["a"]
p[["a"]]
p$a <- 2
p["b"] <- NULL
p[["a"]] <- 3
p[length(.)] # . = p$value

# Data manipulation with dplyr
library(dplyr)
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))) []

# Graphics with ggvis
library(ggvis)
Pipe(mtcars)$
  ggvis(~ mpg, ~ wt)$
  layer_points()

# Data manipulation with rlist
library(rlist)
Pipe(list(1,2,3))$
  list.map(. + 1)$
  list.filter(. <= 5)$
  list.sort(.) []

# Lazy evaluation
p1 <- Pipe(mtcars)$
  ggvis(~ mpg, ~ wt)
p1$layer_points()
p1$layer_bars()

# Stored Pipe
f1 <- Pipe(rnorm(100))$plot
f1(col="red")
f1(col="green")

## End(Not run)

pipeR documentation built on May 1, 2019, 7:13 p.m.