How to create my personal pipe in base R using the new native pipe and pipebind

Viewed 52

I want to be able to use >> instead of |>. => to convert approach 3 into 4 below while preserving/improving performance.

Sys.setenv("_R_USE_PIPEBIND_"=TRUE)
library(magrittr)
xt <- extract <- `[`; dv <- divide <- `/`; num <- `as.numeric`; y <- 5000
txt <- paste0(scales::label_comma(accuracy = 1)(rpois(2,y)))

microbenchmark::microbenchmark(

txt %>% sub(",", "", .) %>% num() %>% .[2]/2 - y*0.5, #approach 1

txt |> (\(.) sub(",", "", .) )() |> num() |> (\(.) .[2]/2 - y*0.5)(), #approach 2

txt |>. => sub(",", "", .) |> num() |> . => xt(.,2) |> . => dv(.,2) - y*0.5, #approach 3

#txt >> sub(",", "", .) |> num() >> xt(.,2) >> dv(.,2) - y*0.5, #approach 4

times = 100L

)

Unit: microseconds
    
     expr    min      lq     mean  median      uq     max neval
approach 1 9.701 10.3510 24.29601 31.6005 33.851  53.402   100
approach 2 7.901 10.1005 23.33513 29.9020 31.902 128.901   100
approach 3 6.701  7.3010 16.44111  9.0010 28.950  36.501   100

Any thoughts? Thanks.

0 Answers
Related