截止到发稿时,magrittr在CRAN上的图是这样的。

library("microbenchmark")
library("magrittr")
library("ggplot2")
library("pipeR")
set.seed(234634)
fmagrittr <- function(d) {
d %>% sin() %>% cos() %>% tan() %>% sqrt()
}
fmagrittrdot <- function(d) {
d %>% sin(.) %>% cos(.) %>% tan(.) %>% sqrt(.)
}
fsemicolon <- function(d) {
d ->.; sin(.) ->.; cos(.) ->.; tan(.) ->.; sqrt(.)
}
fpiper <- function(d) {
d %>>% sin() %>>% cos() %>>% tan() %>>% sqrt()
}
fpipeline <- function(d) {
pipeline({
d
sin()
cos()
tan()
sqrt()
}
)
}
bm <- microbenchmark(
fmagrittr(7),
fmagrittrdot(7),
fsemicolon(7),
fpiper(7),
fpipeline(7),
control=list(warmup=100L,
order='random'),
times=10000L
)
highcut <- quantile(bm$time,probs=0.95)
ggplot(data=as.data.frame(bm),aes(x=time,color=expr)) +
geom_density(adjust=0.3) +
facet_wrap(~expr,ncol=1,scales = 'free_y') +
scale_x_continuous(limits = c(min(bm$time),highcut))
上述代码形成的图如下。

从上述简单测试结果来看:
因为“你会看到有许多 R 代码,都是为了急于解决一个紧迫的问题而编写的。 因此,那些代码不是很优雅、快速或者容易理解。 而大多数用户并不修改他们代码中的这些缺点”。这句话是Hadley Wickham说的。N个方法,先实现一个,拿到结果才是最重要的,代码优化可以稍后进行。
附本次R语言运行环境。
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936 LC_CTYPE=Chinese (Simplified)_China.936
[3] LC_MONETARY=Chinese (Simplified)_China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese (Simplified)_China.936
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] pipeR_0.6.1.3 ggplot2_2.2.0 magrittr_1.5
[4] microbenchmark_1.4-2.1 RevoUtilsMath_10.0.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.8 digest_0.6.10 assertthat_0.1 grid_3.3.2 plyr_1.8.4
[6] gtable_0.2.0 scales_0.4.1 lazyeval_0.2.0 RevoUtils_10.0.2 labeling_0.3
[11] tools_3.3.2 munsell_0.4.3 colorspace_1.3-1 tibble_1.2 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3161445/ ,如需转载,请注明出处,否则将追究法律责任。