R 4.1.0 has added a native pipe operator |> to base R. If you are using the
magrittr pipe %>% and want to switch to |> in your R scripts (you do not
have to), I have a quick function formatR::tidy_pipe() that can help you
substitute %>% with |> in the RStudio editor. All you need to do is open the
R script in RStudio, and run formatR::tidy_pipe() in the R console. Then you
will see %>%’s are converted to |>:
'%>%' %>%
c('->', '|>') %>%
paste(collapse = ' ')
This function only works with the R script currently opened in the RStudio editor, but it is definitely possible to remove this limitation (see the end of this post).
You can also use the function formatR::tidy_source() to reformat code,
including substituting %>% with |>, e.g.,
formatR::tidy_source('your-code.R', pipe = TRUE)
Note that this function does a lot other reformatting, too. If you are not familiar with it, you may take a look at its documentation.
P.S. The word “tidy” in formatR has nothing to do with Tidyverse. It came from 2007 and means reformatting.
An exercise
If you are interested in helping with extending formatR::tidy_pipe(), below is
the basic idea about how it works under the
hood:
-
Use
getParseData(parse(text = code))to get the data about the code.> formatR:::parse_data( "'%>%' %>% c('->', '|>') %>% paste(collapse = ' ')" ) line1 col1 line2 col2 id token text 1 1 1 1 5 1 STR_CONST '%>%' 2 1 7 1 9 2 SPECIAL %>% 4 1 11 1 11 4 SYMBOL_FUNCTION_CALL c 5 1 12 1 12 5 '(' ( .... -
Find the symbols
%>%in the data, and their row/column indices. -
Substitute the
%>%strings in the original code with|>using the indices.
The 3rd step can be a little tricky if you use substr<- but I have an idea to
make it work. Please feel free to open a pull request on
GitHub if you are interested in contributing
(it is okay if you are unable to figure out everything—we can talk on GitHub).
Thanks!

Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).