Is the R command return() incompatible with magrittr's pipe operator?

The magrittr pipe operator seems to have a problem with the R command 'return'.

foo <- function(x) {
  if (x > 1) {1 %>% return()}
  999
}

foo(2)
# 999

foo1 <- function(x) {
  if (x > 1) {return(1)}
  999
}

foo1(2)
# 1

Are there other R functions that are incompatible with magrittr's pipe operator? What is special about 'return' that makes it incompatible?

The new |> operator is explicitly incompatible with return().

foo2 <- function(x) {
  if (x > 1) {1 |> return()}
  999
}

foo2(2)
# Error: function 'return' not supported in RHS call of a pipe

I've experienced this myself. Not sure why...I guess return isn't exactly a function.

I haven't experienced any other function that's incompatible, so I would feel free to pipe away.

I think there will be plenty of functions incompatible with %>% (and presumably |>, too), but I haven't got a definitive reason for why.

ggplot2::ggsave() springs to mind as one example.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.