All Projects → tidyverse → Magrittr

tidyverse / Magrittr

Licence: other
Improve the readability of R code with the pipe

Programming Languages

r
7636 projects

Labels

Projects that are alternatives of or similar to Magrittr

contact
This is a command line application for contact management with pipes in mind using a file based storage.
Stars: ✭ 27 (-96.78%)
Mutual labels:  pipe
Caterpillar
Caterpillar is the ultimate logging system for Deno, Node.js, and Web Browsers. Log levels are implemented to the RFC standard. Log entries can be filtered and piped to various streams, including coloured output to the terminal, the browser's console, and debug files. You can even write your own transforms.
Stars: ✭ 330 (-60.62%)
Mutual labels:  pipe
Rust cmd lib
Common rust command-line macros and utilities, to write shell-script like tasks in a clean, natural and rusty way
Stars: ✭ 604 (-27.92%)
Mutual labels:  pipe
ntee
Portable Unix shell command 'tee', with some extras - read from standard input and write to standard output and files
Stars: ✭ 22 (-97.37%)
Mutual labels:  pipe
NumberToPersian
Convert numbers to Persian letters in Angular Pipe
Stars: ✭ 18 (-97.85%)
Mutual labels:  pipe
Pipeline
Pipeline is a package to build multi-staged concurrent workflows with a centralized logging output.
Stars: ✭ 433 (-48.33%)
Mutual labels:  pipe
pv
Unix Pipe Viewer (pv) utility in Node.js
Stars: ✭ 20 (-97.61%)
Mutual labels:  pipe
Bash My Aws
Bash-my-AWS provides simple but powerful CLI commands for managing AWS resources
Stars: ✭ 782 (-6.68%)
Mutual labels:  pipe
Netcat
💻 Netcat client and server modules written in pure Javascript for Node.js.
Stars: ✭ 315 (-62.41%)
Mutual labels:  pipe
Tosheets
Send your stdin to google sheets
Stars: ✭ 536 (-36.04%)
Mutual labels:  pipe
composition-logger
The most optimal way to visualize/debug functional compositions 🔍
Stars: ✭ 15 (-98.21%)
Mutual labels:  pipe
angular-clean-code
My personal best practices when I'm working with Angular.
Stars: ✭ 39 (-95.35%)
Mutual labels:  pipe
Dnpipes
Distributed Named Pipes
Stars: ✭ 452 (-46.06%)
Mutual labels:  pipe
prox
A Scala library for working with system processes
Stars: ✭ 93 (-88.9%)
Mutual labels:  pipe
Angular Pipes
Useful pipes for Angular
Stars: ✭ 652 (-22.2%)
Mutual labels:  pipe
observable ish
Observable state and events for browser and Flutter.
Stars: ✭ 26 (-96.9%)
Mutual labels:  pipe
Pipe
🎷 一款小而美的博客平台,专为程序员设计。
Stars: ✭ 3,898 (+365.16%)
Mutual labels:  pipe
Ipt
Interactive Pipe To: The Node.js cli interactive workflow
Stars: ✭ 783 (-6.56%)
Mutual labels:  pipe
Ngx Mask
Angular Plugin to make masks on form fields and html elements.
Stars: ✭ 772 (-7.88%)
Mutual labels:  pipe
Ttyplot
a realtime plotting utility for terminal/console with data input from stdin
Stars: ✭ 532 (-36.52%)
Mutual labels:  pipe

magrittr

CRAN status Codecov test coverage R build status

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:

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 in R for data science.

Installation

# 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")
devtools::install_github("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.

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:

iris %>%
  subset(Sepal.Length > mean(Sepal.Length)) %$%
  cor(Sepal.Length, Sepal.Width)
#> [1] 0.3361992

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. By contributing to this project, you agree to abide by its terms.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].