magrittr::%>%
get_help()
docs
The combined symbol %>%
is an operator in the {magrittr}
package, which is part of the {tidyverse}
. This operator is referred to as “pipe,” but it should not be confused with the symbol |
(on backslash key), which is also known as pipe.
The %>%
operator “sends in” input to a given function. It provides a convenient approach to constructing pipelines, where the output from one command can be seamlessly provided into the next command.
To use the pipe, you need to either first load the {tidyverse}
, or one of the core {tidyverse}
libraries.
# Load the tidyverse (most common)
library(tidyverse)
# Or load a core tidyverse library such as dplyr
library(dplyr)
# Non-pipe version of code:
some_function(argument)
# Pipe version of code:
%>%
argument some_function()
#############################
# Non-pipe version of code:
some_function(argument1, argument2)
# Pipe version of code:
%>%
argument1 some_function(argument2)
############################
# Non-pipe version of code:
some_other_other_function(some_other_function(some_function(argument1, argument2)))
# Or, a different way to write that non-pipe code:
<- some_function(argument1, argument2)
output1 <- some_other_function(output1)
output2 <- some_other_other_function(output2)
output3
# Pipe version of code:
%>%
argument1 some_function(argument2) %>%
some_other_function() %>%
some_other_other_function()
Some examples below use the carnivores
dataset. Learn more about this dataset with get_help("carnivores")
.
# Show the carnivores dataset
carnivores## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Calculate the natural log of 5
# Non-pipe version of code:
log(5)
## [1] 1.609438
# Pipe version of code:
5 %>%
log()
## [1] 1.609438
# Calculate the log of 5 in base 7
# Non-pipe version of code:
log(5, 7)
## [1] 0.8270875
# Pipe version of code:
5 %>%
log(7)
## [1] 0.8270875
# Calculate the log of 5 in base 7, and THEN calculate the sqrt of the result.
# Non-pipe version of code:
sqrt( log(5, 7) )
## [1] 0.9094435
# Or, a different non-pipe version:
<- log(5,7)
step1 sqrt(step1)
## [1] 0.9094435
# Pipe version of code:
5 %>%
log(7) %>%
sqrt()
## [1] 0.9094435
# Use dplyr::mutate to add a new column to carnivores called `new_col`
# Non-pipe version of code. _This is NOT recommended._
mutate(carnivores, new_col = 7)
## # A tibble: 9 × 5
## name genus awake brainwt new_col
## <chr> <fct> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 7
## 2 Cheetah Acinonyx 11.9 NA 7
## 3 Dog Canis 13.9 0.07 7
## 4 Gray seal Haliochoerus 17.8 0.325 7
## 5 Jaguar Panthera 13.6 0.157 7
## 6 Lion Panthera 10.5 NA 7
## 7 Northern fur seal Callorhinus 15.3 NA 7
## 8 Red fox Vulpes 14.2 0.0504 7
## 9 Tiger Panthera 8.2 NA 7
# Pipe version of code. _This IS recommended._
%>%
carnivores mutate(new_col = 7)
## # A tibble: 9 × 5
## name genus awake brainwt new_col
## <chr> <fct> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 7
## 2 Cheetah Acinonyx 11.9 NA 7
## 3 Dog Canis 13.9 0.07 7
## 4 Gray seal Haliochoerus 17.8 0.325 7
## 5 Jaguar Panthera 13.6 0.157 7
## 6 Lion Panthera 10.5 NA 7
## 7 Northern fur seal Callorhinus 15.3 NA 7
## 8 Red fox Vulpes 14.2 0.0504 7
## 9 Tiger Panthera 8.2 NA 7
# Use dplyr::mutate to add a new column to carnivores called `new_col`
# and then use dplyr::select to keep only `name` and `new_col` columns.
# Non-pipe version of code. _This is NOT recommended._
select(mutate(carnivores, new_col = 7), name, new_col)
## # A tibble: 9 × 2
## name new_col
## <chr> <dbl>
## 1 Arctic fox 7
## 2 Cheetah 7
## 3 Dog 7
## 4 Gray seal 7
## 5 Jaguar 7
## 6 Lion 7
## 7 Northern fur seal 7
## 8 Red fox 7
## 9 Tiger 7
# Or, a different non-pipe version (also NOT recommended):
<- mutate(carnivores, new_col = 7)
carnivores2 select(carnivores2, name, new_col)
## # A tibble: 9 × 2
## name new_col
## <chr> <dbl>
## 1 Arctic fox 7
## 2 Cheetah 7
## 3 Dog 7
## 4 Gray seal 7
## 5 Jaguar 7
## 6 Lion 7
## 7 Northern fur seal 7
## 8 Red fox 7
## 9 Tiger 7
# Pipe version of code. _This IS recommended._
%>%
carnivores mutate(new_col = 7) %>%
select(name, new_col)
## # A tibble: 9 × 2
## name new_col
## <chr> <dbl>
## 1 Arctic fox 7
## 2 Cheetah 7
## 3 Dog 7
## 4 Gray seal 7
## 5 Jaguar 7
## 6 Lion 7
## 7 Northern fur seal 7
## 8 Red fox 7
## 9 Tiger 7