{"id":13654039,"url":"https://github.com/tidyverse/magrittr","last_synced_at":"2025-05-14T07:08:47.664Z","repository":{"id":12887846,"uuid":"15564525","full_name":"tidyverse/magrittr","owner":"tidyverse","description":"Improve the readability of R code with the pipe","archived":false,"fork":false,"pushed_at":"2023-03-08T13:37:44.000Z","size":5330,"stargazers_count":962,"open_issues_count":26,"forks_count":158,"subscribers_count":53,"default_branch":"main","last_synced_at":"2025-05-10T11:52:29.890Z","etag":null,"topics":["pipe","r"],"latest_commit_sha":null,"homepage":"https://magrittr.tidyverse.org","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tidyverse.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2014-01-01T13:30:01.000Z","updated_at":"2025-04-28T19:23:13.000Z","dependencies_parsed_at":"2022-08-07T07:00:49.062Z","dependency_job_id":"9e843f6b-25a9-4d56-bc3a-0a3db14fa375","html_url":"https://github.com/tidyverse/magrittr","commit_stats":{"total_commits":288,"total_committers":31,"mean_commits":9.290322580645162,"dds":0.6666666666666667,"last_synced_commit":"21093d06e43649a72b865811188e77fc8ad87780"},"previous_names":["smbache/magrittr"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fmagrittr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fmagrittr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fmagrittr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tidyverse%2Fmagrittr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tidyverse","download_url":"https://codeload.github.com/tidyverse/magrittr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254049573,"owners_count":22006089,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pipe","r"],"created_at":"2024-08-02T02:01:22.359Z","updated_at":"2025-05-14T07:08:42.648Z","avatar_url":"https://github.com/tidyverse.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/\"\n)\nlibrary(magrittr)\n```\n\n# magrittr \u003ca href='https:/magrittr.tidyverse.org'\u003e\u003cimg src='man/figures/logo.png' align=\"right\" height=\"139\" /\u003e\u003c/a\u003e\n\n\u003c!-- badges: start --\u003e\n[![CRAN status](https://www.r-pkg.org/badges/version/magrittr)](https://cran.r-project.org/package=magrittr)\n[![Codecov test coverage](https://codecov.io/gh/tidyverse/magrittr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidyverse/magrittr?branch=main)\n[![R-CMD-check](https://github.com/smbache/magrittr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/smbache/magrittr/actions/workflows/R-CMD-check.yaml)\n\u003c!-- badges: end --\u003e\n\n## Overview\n\nThe magrittr package offers a set of operators which make your code more\nreadable by:\n\n* structuring sequences of data operations left-to-right (as opposed to \n  from the inside and out),\n* avoiding nested function calls, \n* minimizing the need for local variables and function definitions, and\n* making it easy to add steps anywhere in the sequence of operations.\n\nThe operators pipe their left-hand side values forward into expressions that\nappear on the right-hand side, i.e. one can replace `f(x)` with \n`x %\u003e% f()`, where `%\u003e%` is the (main) pipe-operator. When coupling \nseveral function calls with the pipe-operator, the benefit will become\nmore apparent. Consider this pseudo example:\n\n```{r, eval = FALSE}\nthe_data \u003c-\n  read.csv('/path/to/data/file.csv') %\u003e%\n  subset(variable_a \u003e x) %\u003e%\n  transform(variable_c = variable_a/variable_b) %\u003e%\n  head(100)\n```\n\nFour operations are performed to arrive at the desired data set, and they \nare written in a natural order: the same as the order of execution. Also, no \ntemporary variables are needed. If yet another operation is required, it is \nstraightforward to add to the sequence of operations wherever it may be needed.\n\nIf you are new to magrittr, the best place to start is the \n[pipes chapter](https://r4ds.had.co.nz/pipes.html) in R for data science.\n\n## Installation\n\n```{r, eval = FALSE}\n# The easiest way to get magrittr is to install the whole tidyverse:\ninstall.packages(\"tidyverse\")\n\n# Alternatively, install just magrittr:\ninstall.packages(\"magrittr\")\n\n# Or the development version from GitHub:\n# install.packages(\"devtools\")\npak::pak(\"tidyverse/magrittr\")\n```\n\n## Usage\n\n### Basic piping\n  \n* `x %\u003e% f` is equivalent to `f(x)`\n* `x %\u003e% f(y)` is equivalent to `f(x, y)`\n* `x %\u003e% f %\u003e% g %\u003e% h` is equivalent to `h(g(f(x)))`\n\nHere, \"equivalent\" is not technically exact: evaluation is non-standard,\nand the left-hand side is evaluated before passed on to the right-hand side\nexpression. However, in most cases this has no practical implication.\n\n### The argument placeholder\n\n* `x %\u003e% f(y, .)` is equivalent to `f(y, x)`\n* `x %\u003e% f(y, z = .)` is equivalent to `f(y, z = x)`\n \n### Re-using the placeholder for attributes\n\nIt is straightforward to use the placeholder several times\nin a right-hand side expression. However, when the placeholder\nonly appears in a nested expressions magrittr will still apply\nthe first-argument rule. The reason is that in most cases this\nresults more clean code. \n\n`x %\u003e% f(y = nrow(.), z = ncol(.))` is equivalent to \n   `f(x, y = nrow(x), z = ncol(x))`\n\nThe behavior can be\noverruled by enclosing the right-hand side in braces:\n\n`x %\u003e% {f(y = nrow(.), z = ncol(.))}` is equivalent to \n   `f(y = nrow(x), z = ncol(x))`\n\n### Building (unary) functions\n\nAny pipeline starting with the `.` will return a function which can later\nbe used to apply the pipeline to values. Building functions in magrittr \nis therefore similar to building other values.\n\n```{r}\nf \u003c- . %\u003e% cos %\u003e% sin \n# is equivalent to \nf \u003c- function(.) sin(cos(.)) \n```\n\n### Pipe with exposition of variables\n\nMany functions accept a data argument, e.g. `lm` and `aggregate`, which\nis very useful in a pipeline where data is first processed and then passed\ninto such a function. There are also functions that do not have a data \nargument, for which it is useful to expose the variables in the data.\nThis is done with the `%$%` operator:\n\n```{r exposition}\niris %\u003e%\n  subset(Sepal.Length \u003e mean(Sepal.Length)) %$%\n  cor(Sepal.Length, Sepal.Width)\n\ndata.frame(z = rnorm(100)) %$%\n  ts.plot(z)\n```\n\n## Code of Conduct\n\nPlease note that the magrittr project is released with a [Contributor Code of Conduct](https://magrittr.tidyverse.org/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidyverse%2Fmagrittr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftidyverse%2Fmagrittr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftidyverse%2Fmagrittr/lists"}