Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created October 10, 2024 01:09
Show Gist options
  • Save njtierney/b857cd5bc10f9cf01a161431f3f72707 to your computer and use it in GitHub Desktop.
Save njtierney/b857cd5bc10f9cf01a161431f3f72707 to your computer and use it in GitHub Desktop.
pipe-quote-demo

Demonstrating how %>% differs to |> in R. One is a function call, the other parses the code into nested functions.

library(magrittr)
library(lobstr)
1:10 |> sum()
#> [1] 55
1:10 %>% sum()
#> [1] 55

quote(1:10 %>% prod(1:10) %>% sum())
#> 1:10 %>% prod(1:10) %>% sum()
quote(1:10 |> prod(1:10) |> sum())
#> sum(prod(1:10, 1:10))
ast(1:10 %>% prod(1:10) %>% sum())
#> █─`%>%` 
#> ├─█─`%>%` 
#> │ ├─█─`:` 
#> │ │ ├─1 
#> │ │ └─10 
#> │ └─█─prod 
#> │   └─█─`:` 
#> │     ├─1 
#> │     └─10 
#> └─█─sum
ast(1:10 |> prod(1:10) |> sum())
#> █─sum 
#> └─█─prod 
#>   ├─█─`:` 
#>   │ ├─1 
#>   │ └─10 
#>   └─█─`:` 
#>     ├─1 
#>     └─10

Created on 2024-10-10 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 Patched (2024-07-08 r86915)
#>  os       macOS Sonoma 14.5
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Australia/Hobart
#>  date     2024-10-10
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.3      2024-06-21 [1] CRAN (R 4.4.0)
#>  crayon        1.5.3      2024-06-20 [1] CRAN (R 4.4.0)
#>  digest        0.6.36     2024-06-23 [1] CRAN (R 4.4.0)
#>  evaluate      0.24.0     2024-06-10 [1] CRAN (R 4.4.0)
#>  fastmap       1.2.0      2024-05-15 [1] CRAN (R 4.4.0)
#>  fs            1.6.4.9000 2024-06-26 [1] Github (r-lib/fs@714990b)
#>  glue          1.7.0      2024-01-09 [1] CRAN (R 4.4.0)
#>  htmltools     0.5.8.1    2024-04-04 [1] CRAN (R 4.4.0)
#>  knitr         1.48       2024-07-07 [1] CRAN (R 4.4.0)
#>  lifecycle     1.0.4      2023-11-07 [1] CRAN (R 4.4.0)
#>  lobstr      * 1.1.2      2022-06-22 [1] CRAN (R 4.4.0)
#>  magrittr    * 2.0.3      2022-03-30 [1] CRAN (R 4.4.0)
#>  reprex        2.1.1      2024-07-06 [1] CRAN (R 4.4.0)
#>  rlang         1.1.4      2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.27       2024-05-17 [1] CRAN (R 4.4.0)
#>  rstudioapi    0.16.0     2024-03-24 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.4.0)
#>  withr         3.0.1      2024-07-31 [1] CRAN (R 4.4.0)
#>  xfun          0.46       2024-07-18 [1] CRAN (R 4.4.0)
#>  yaml          2.3.10     2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Users/nick/Library/R/arm64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@njtierney
Copy link
Author

# base vs magrittr
iris %>% lm(Sepal.Width ~ ., data = .)
# same as
iris |> lm(Sepal.Width ~ ., data = _)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment