magrittr包被定义为一个高效的管道操作工具包,通过管道的连接方式,让数据或表达式的传递更高效,使用操作符%>%,可以直接把数据传递给下一个函数调用或表达式。magrittr包的主要目标有2个,第一是减少代码开发时间,提高代码的可读性和维护性;第二是让你的代码更短,再短,短短短…
magrittr包,主要定义了4个管道操作符,分另是%>%, %T>%, %$% 和 %<>%。其中,操作符%>%是最常用的,其他3个操作符,与%>%类似,在特殊的使用场景会起到更好的作用。当正确掌握这几个操作符后,你一定会爱不释手的,快去把所有的代码都重构吧,砍掉原来大段冗长的代码是一件多么令人激动的事情啊。
magrittr的项目主页:https://github.com/smbache/magrittr
不使用管道会怎样
library(magrittr)
foo_foo_1 <- hop(foo_foo, through = forest)
foo_foo_2 <- scoop(foo_foo_1, up = field_mice)
foo_foo_3 <- bop(foo_foo_2, on = head)
- 不必要的中间变量
- 每次修改要做很多次
diamonds <- ggplot2::diamonds
diamonds2 <- diamonds %>%
dplyr::mutate(price_per_carat = price / carat)
pryr::object_size(diamonds)
#> 3.46 MB
pryr::object_size(diamonds2)
#> 3.89 MB
pryr::object_size(diamonds, diamonds2)
#> 3.89 MB
重写初始对象
foo_foo <- hop(foo_foo, through = forest)
foo_foo <- scoop(foo_foo, up = field_mice)
foo_foo <- bop(foo_foo, on = head)
- 不利于调试
- 对象的多次重写
函数组合
bop(
scoop(
hop(foo_foo, through = forest),
up = field_mice
),
on = head
)
使用管道
foo_foo %>%
hop(through = forest) %>%
scoop(up = field_mice) %>%
bop(on = head)
原理:
my_pipe <- function(.) {
. <- hop(., through = forest)
. <- scoop(., up = field_mice)
bop(., on = head)
}
my_pipe(foo_foo)
不适用管道的情况
Your pipes are longer than (say) ten steps. In that case, create intermediate objects with meaningful names. That will make debugging easier, because you can more easily check the intermediate results, and it makes it easier to understand your code, because the variable names can help communicate intent.
You have multiple inputs or outputs. If there isn’t one primary object being transformed, but two or more objects being combined together, don’t use the pipe.
You are starting to think about a directed graph with a complex dependency structure. **Pipes are fundamentally linear **and expressing complex relationships with them will typically yield confusing code.
magrittr 中的其他工具
rnorm(100) %>%
matrix(ncol = 2) %>%
plot() %>%
str()
#> NULL
rnorm(100) %>%
matrix(ncol = 2) %T>%
plot() %>%
str()
#> num [1:50, 1:2] -0.387 -0.785 -1.057 -0.796 -1.756 ...
head(mtcars )
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mtcars %$%
cor(disp, mpg)
#> [1] -0.848
> mtcars <- mtcars %>%
+ transform(cyl = cyl * 2)
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 48 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 48 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 32 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 48 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 64 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 48 225 105 2.76 3.460 20.22 1 0 3 1
>
> mtcars %<>% transform(cyl = cyl * 2)
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 96 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 96 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 64 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 96 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 128 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 96 225 105 2.76 3.460 20.22 1 0 3 1
>