Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Native pipe placeholder

I realize this question has been asked before but it’s not clicking. There’s really no placeholder?

Example:

my_mtcars <- mtcars %>% mutate(bla = c(1:nrow(.)))
my_mtcars$bla[10] <- NA
my_mtcars$bla[15] <- NA

Works:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# magritr pipe to return NA rows while debugging a df
my_mtcars %>% filter(!complete.cases(.)) %>% glimpse

Does not work:

# native piple equivilent
my_mtcars |> filter(!complete.cases(.)) |> glimpse()

What’s the ‘right’ way to do what I’m trying to do with the native pipe?

>Solution :

The native R pipe does not use dot. It always inserts into the first argument. To get the effect of dot define a function or if it is at the beginning combine it yourself repeating the input (or break it up into two pipelines and do the same — not shown since doesn’t apply here).

library(dplyr)

mtcars |>
  (\(x) filter(x, complete.cases(x)))() |>
  summary()

or

f <- function(x) filter(x, complete.cases(x))
mtcars |> f() |> summary()

or

filter(mtcars, complete.cases(mtcars)) |> summary()

Note that you can do this with only base R — the Bizarro pipe which is not really a pipe.

mtcars ->.;
  filter(., complete.cases(.)) ->.;
  summary(.)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading