Last chance! 50% off unlimited learning
Sale ends in
$
.$
.
Pipe(value = NULL)
NULL
)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
.
## 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)
Run the code above in your browser using DataLab