0

Why the ., to indicate the first object that was passed to the function, works with %>% operator but not with |> operator?

> a <- 1:10
> a[2] <- NA
> library(tidyr)
> a %>% replace(is.na(.),11)
 [1]  1 11  3  4  5  6  7  8  9 10
> a |> replace(is.na(.),11)
Error: object '.' not found
robertspierre
  • 3,218
  • 2
  • 31
  • 46
  • 6
    Native pipe uses `_` for the argument placeholder. You can check `?"|>"` for more. – margusl Aug 10 '23 at 10:19
  • 1
    The magrittr and base pipes are superficially similar but actually have very different implementations. The result is that the base pipe provides less 'extra' features. See this article for more info: https://www.tidyverse.org/blog/2023/04/base-vs-magrittr-pipe/ – wurli Aug 10 '23 at 10:19
  • @margusl `?|>` gives me `Error: unexpected '|>' in "?|>"` – robertspierre Aug 10 '23 at 10:21
  • 1
    @robertspierre, you have to write exactly `?"|>"`, notice that the actual function (`|>`) is quoted. – mhovd Aug 10 '23 at 10:23
  • Yes, `?"|>"` works. However, `a |> replace(is.na(_),11)` gives `invalid use of pipe placeholder (:1:0)` – robertspierre Aug 10 '23 at 10:24
  • 3
    Check @wurli's link for more differences (can't use `_` in nested calls, argument must be named). And also https://stackoverflow.com/q/67633022/646761 for some excellent examples. – margusl Aug 10 '23 at 10:34
  • 2
    When you nest `is.na()` inside `replace()`, that's a nested call and the base pipe placeholder will not work. – Gregor Thomas Aug 10 '23 at 12:18

0 Answers0