magrittr

Improve the readability of R code with the pipe

https://github.com/tidyverse/magrittr

Science Score: 10.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    3 of 31 committers (9.7%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (18.8%) to scientific vocabulary

Keywords

pipe r

Keywords from Contributors

grammar data-manipulation visualisation package-creation unit-testing tidy-data rcpp r-packages csv c-plus-plus-20
Last synced: 6 months ago · JSON representation

Repository

Improve the readability of R code with the pipe

Basic Info
Statistics
  • Stars: 964
  • Watchers: 53
  • Forks: 159
  • Open Issues: 27
  • Releases: 5
Topics
pipe r
Created about 12 years ago · Last pushed almost 3 years ago
Metadata Files
Readme Contributing License Code of conduct

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/"
)
library(magrittr)
```

# magrittr 


[![CRAN status](https://www.r-pkg.org/badges/version/magrittr)](https://cran.r-project.org/package=magrittr)
[![Codecov test coverage](https://codecov.io/gh/tidyverse/magrittr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/tidyverse/magrittr?branch=main)
[![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)


## Overview

The magrittr package offers a set of operators which make your code more
readable by:

* structuring sequences of data operations left-to-right (as opposed to 
  from the inside and out),
* avoiding nested function calls, 
* minimizing the need for local variables and function definitions, and
* making it easy to add steps anywhere in the sequence of operations.

The operators pipe their left-hand side values forward into expressions that
appear on the right-hand side, i.e. one can replace `f(x)` with 
`x %>% f()`, where `%>%` is the (main) pipe-operator. When coupling 
several function calls with the pipe-operator, the benefit will become
more apparent. Consider this pseudo example:

```{r, eval = FALSE}
the_data <-
  read.csv('/path/to/data/file.csv') %>%
  subset(variable_a > x) %>%
  transform(variable_c = variable_a/variable_b) %>%
  head(100)
```

Four operations are performed to arrive at the desired data set, and they 
are written in a natural order: the same as the order of execution. Also, no 
temporary variables are needed. If yet another operation is required, it is 
straightforward to add to the sequence of operations wherever it may be needed.

If you are new to magrittr, the best place to start is the 
[pipes chapter](https://r4ds.had.co.nz/pipes.html) in R for data science.

## Installation

```{r, eval = FALSE}
# The easiest way to get magrittr is to install the whole tidyverse:
install.packages("tidyverse")

# Alternatively, install just magrittr:
install.packages("magrittr")

# Or the development version from GitHub:
# install.packages("devtools")
pak::pak("tidyverse/magrittr")
```

## Usage

### Basic piping
  
* `x %>% f` is equivalent to `f(x)`
* `x %>% f(y)` is equivalent to `f(x, y)`
* `x %>% f %>% g %>% h` is equivalent to `h(g(f(x)))`

Here, "equivalent" is not technically exact: evaluation is non-standard,
and the left-hand side is evaluated before passed on to the right-hand side
expression. However, in most cases this has no practical implication.

### The argument placeholder

* `x %>% f(y, .)` is equivalent to `f(y, x)`
* `x %>% f(y, z = .)` is equivalent to `f(y, z = x)`
 
### Re-using the placeholder for attributes

It is straightforward to use the placeholder several times
in a right-hand side expression. However, when the placeholder
only appears in a nested expressions magrittr will still apply
the first-argument rule. The reason is that in most cases this
results more clean code. 

`x %>% f(y = nrow(.), z = ncol(.))` is equivalent to 
   `f(x, y = nrow(x), z = ncol(x))`

The behavior can be
overruled by enclosing the right-hand side in braces:

`x %>% {f(y = nrow(.), z = ncol(.))}` is equivalent to 
   `f(y = nrow(x), z = ncol(x))`

### Building (unary) functions

Any pipeline starting with the `.` will return a function which can later
be used to apply the pipeline to values. Building functions in magrittr 
is therefore similar to building other values.

```{r}
f <- . %>% cos %>% sin 
# is equivalent to 
f <- function(.) sin(cos(.)) 
```

### Pipe with exposition of variables

Many functions accept a data argument, e.g. `lm` and `aggregate`, which
is very useful in a pipeline where data is first processed and then passed
into such a function. There are also functions that do not have a data 
argument, for which it is useful to expose the variables in the data.
This is done with the `%$%` operator:

```{r exposition}
iris %>%
  subset(Sepal.Length > mean(Sepal.Length)) %$%
  cor(Sepal.Length, Sepal.Width)

data.frame(z = rnorm(100)) %$%
  ts.plot(z)
```

## Code of Conduct

Please 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.

Owner

  • Name: tidyverse
  • Login: tidyverse
  • Kind: organization

The tidyverse is a collection of R packages that share common principles and are designed to work together seamlessly

GitHub Events

Total
  • Issues event: 3
  • Watch event: 10
  • Issue comment event: 3
  • Fork event: 2
Last Year
  • Issues event: 3
  • Watch event: 10
  • Issue comment event: 3
  • Fork event: 2

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 288
  • Total Committers: 31
  • Avg Commits per committer: 9.29
  • Development Distribution Score (DDS): 0.667
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
smbache s****n@s****k 96
Lionel Henry l****y@g****m 88
hadley h****m@g****m 61
unknown s****b@P****l 6
Stefan Holst Milton Bache s****b@d****m 3
Anton Antonov t****v@g****m 3
Juan Sebastian Casallas c****s@i****u 3
David LeBauer d****r@g****m 2
Mara Averick m****k@g****m 2
Marie-Helene Burle m****2@s****a 2
Rick Saporta r****a@g****m 2
mark padgham m****m@e****m 1
leerssej l****j 1
i i****s@s****g 1
Will Beasley w****y@h****m 1
Aaron Schumacher a****r@g****m 1
Bernie Gray b****3@g****m 1
Catherine Blatter c****r@u****h 1
David Chudzicki d****z@g****m 1
Davis Vaughan d****s@r****m 1
Gabor Csardi c****r@g****m 1
HughParsonage h****e@g****m 1
Jeff Newmiller j****l@d****s 1
Jim Hester j****r@g****m 1
Kevin Ushey k****y@g****m 1
Patrick Kennedy p****e@u****u 1
Robert Krzyzanowski t****b@g****m 1
Romain Francois r****n@r****m 1
Salim B s****m@p****e 1
Stefano Romano s****3@g****m 1
and 1 more...

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 82
  • Total pull requests: 31
  • Average time to close issues: 6 months
  • Average time to close pull requests: 7 months
  • Total issue authors: 65
  • Total pull request authors: 21
  • Average comments per issue: 3.4
  • Average comments per pull request: 2.68
  • Merged pull requests: 18
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 2
  • Pull requests: 2
  • Average time to close issues: 5 days
  • Average time to close pull requests: N/A
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 0.5
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • lionel- (6)
  • hadley (4)
  • ggrothendieck (3)
  • iago-pssjd (3)
  • balwierz (2)
  • moodymudskipper (2)
  • ramiromagno (2)
  • SimonDedman (2)
  • klmr (2)
  • hackereye (1)
  • HLasse (1)
  • vspinu (1)
  • bdhumb (1)
  • Enchufa2 (1)
  • cathblatter (1)
Pull Request Authors
  • lionel- (8)
  • batpigandme (3)
  • krlmlr (2)
  • luisDVA (2)
  • dmurdoch (1)
  • kevinushey (1)
  • sbgraves237 (1)
  • ahcyip (1)
  • ajschumacher (1)
  • cathblatter (1)
  • mpadge (1)
  • DavisVaughan (1)
  • prosoitos (1)
  • hadley (1)
  • yfarjoun (1)
Top Labels
Issue Labels
feature (9) bug (4) wontfix (3) documentation (1) tidy-dev-day :nerd_face: (1)
Pull Request Labels
tidy-dev-day :nerd_face: (1)

Packages

  • Total packages: 2
  • Total downloads:
    • cran 1,282,467 last-month
  • Total docker downloads: 212,161,705
  • Total dependent packages: 2,700
    (may contain duplicates)
  • Total dependent repositories: 12,803
    (may contain duplicates)
  • Total versions: 10
  • Total maintainers: 1
cran.r-project.org: magrittr

A Forward-Pipe Operator for R

  • Versions: 6
  • Dependent Packages: 2,700
  • Dependent Repositories: 12,803
  • Downloads: 1,282,467 Last month
  • Docker Downloads: 212,161,705
Rankings
Dependent repos count: 0.0%
Dependent packages count: 0.0%
Downloads: 0.1%
Stargazers count: 0.3%
Forks count: 0.4%
Average: 3.0%
Docker downloads count: 17.3%
Maintainers (1)
Last synced: 6 months ago
proxy.golang.org: github.com/tidyverse/magrittr
  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.7%
Dependent repos count: 5.9%
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.4.0 depends
  • covr * suggests
  • knitr * suggests
  • rlang * suggests
  • rmarkdown * suggests
  • testthat * suggests