magrittr pipelines stop()-ing with vctrs' %0%?

I'm curious if anyone has used this pattern with success?
And are there any pitfalls folks might see here?

Pipeline terminating with %0% stop()

Take an arbitrary dplyr pipeline that might yield zero rows, and in those cases you want to return a special object that's also returned if there are any errors encountered along the pipeline:

  data %>%
    f1() %>%
    f2() %>%
    f3() %0% stop()
}, error = function(e) {
  ## perhaps warn here.

Note that the last step of the pipeline is %0% stop() (using vctrs's %0%).

This appears to be a very compact 'last step checker' of a pipeline, which could also be written like so (amongst other ways):

    f(3) %>% function(x) if (nrow(x) == 0) stop() else x  ## functionally same as above?

Have other folks ever used the compact pattern?
Some light testing suggests it works just fine, but perhaps I'm missing some nuanced pitfalls?

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.