Using the pipe operator in R with Plotly

With the release of Plotly 4.0 using the pipe %>% operator is a lot more intuitive when using plot_ly().

Quick Introduction

For those new to the pipe operator from the magrittr package here’s a quick introduction. In essence, the pipe operator takes the argument on the left hand side of the operator and inserts it (after evaluation if an expression) as the first argument of the expression appearing on the right hand side of the operator i.e.

x %>% F is the same as F(x)
x %>% G %>% F is the same as F(G(x))

Here are some examples…

Creating a pipeline

The %>% operator comes in handy when chaining different operations together to create a pipeline.

Adding visualizations to the pipeline

The above example can be taken a step further by adding data visualization to the pipeline.

Piping and plotly

Adding plotly to a pipeline using the pipe operator is easy.


Using the pipe operator with dplyr verbs and plotly makes for some powerful pipelines and easy to read code.

For more details visit the following resources: