I am trying to compare the %>%
with the native |>
, but I am not too sure why sometimes it only works for %>%
. Here is an example:
library(magrittr)
library(stringr)
# Create the sample vector:
sample_value <- c(1, 2, 3, NA, 5)
It works when using pipe operator and its placeholder (.) from the magrittr package:
# With pipe operator and its placeholder (.):
sample_value %>%
replace(list = is.na(.), values = 4) |>
str_flatten_comma()
I get expected result: [1] "1, 2, 3, 4, 4, 5"
But when I use the native pipe operator |>
and its placeholder _
, it didn’t work:
# With native pipe operator and its placeholder (_):
sample_value |>
replace(list = is.na(_), values = 4) |>
str_flatten_comma()
I get a warning:
Error in str_flatten_comma(replace(sample_value, list = is.na("_"), values = 4)) :
invalid use of pipe placeholder (<input>:3:0)
I am not sure what has caused the issue and I’d appreciate your thoughts. Thank you.