9.5 The pipe operator

The tidyverse provides a function %>% which pipes the output of one function as the input of the next function.

This way, different functions from the tidyverse can be linked together into a clean piece of code.

If we want to produce a one-liner out of the 3 lines of code of the previous exercise, we can do it the following way:

gtf_final <- rename(gtf, chromosome=chr) %>%
  filter(strand=="+" & chromosome=="chr4") %>%
  select(gene_symbol, gene_type)

Note that, in filter and select, you do not need to specify the first parameter (the data), as it is automatically looking for the output of the previous one!

If you want to learn more about the pipe, you can for example refer to that page.

Note that we can also link the data manipulation output to ggplot: the output of the last command will then be used as an input to ggplot:

rename(gtf, chromosome=chr) %>%
  filter(strand=="+" & chromosome=="chr4") %>%
  select(gene_symbol, gene_type) %>% 
  ggplot(aes(x=gene_type)) + geom_bar()