Pipe() creates a Pipe object where built-in symbols are designed for building pipeline.
$ chains functions by first-argument piping and always returns a Pipe object..(...) evaluates an expression with . or by lambda expression, or for side effect, or simply extract a named element. The usage is exactly the same with x %>>% (...).$value or [] extracts the final value of the Pipe object.Pipe as first-argument to a function:
Pipe(rnorm(100,mean=10))$
log()$
diff()$
plot(col="red")
Pipe by lambda expression
Pipe(mtcars)$
.(lm(mpg ~ ., data = .))$
summary() []
Pipe(mtcars)$
.(df ~ lm(mpg ~ ., data = df))$
summary() []
Pipe for extracting element
Pipe(mtcars)$
.(mpg)$
summary() []
Pipe for side effect
Pipe(iris)$
.(~ cat(length(.), "columns","\n"))$
.(~ plot(.))$
summary()
Subsetting Pipe object
Pipe(mtcars)["mpg"]
Extracting from Pipe object
Pipe(mtcars)[["mpg"]]
Assigning to Pipe object's element
plist <- Pipe(list(a=1,b=2))
plist$a <- 0
plist$b <- NULL
plist <- Pipe(list(a=1,b=2))
plist["a"] <- 2
plist[["b"]] <- 3
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, as shown above.
Pipe can also be stored as function.
f1 <- Pipe(rnorm(100))$plot
f1(col="red")
f1(col="green")
When the arguments are supplied, plot() will be evaluated. Although Pipe is lazy but its value is determined at first evaluation.