magrittr

repository·main·Indexed 21 days ago

https://github.com/tidyverse/magrittr

A set of operators for R, most notably the pipe operator (%>%), designed to improve code readability by structuring data operations in a linear, left-to-right sequence. Includes the argument placeholder (.) for flexible function calls and the exposition operator (%#%) for exposing variables within data objects.

Tokens
850
Snippets
6
Records
6
Agent score
26%

What's inside magrittr

  1. Re-use the placeholder for attributes

    main

    You can use the . placeholder multiple times in a right-hand side expression.

    Important Behavior: If the placeholder only appears inside nested expressions, magrittr applies the 'first-argument rule' (automatically passing the LHS to the first argument of the first function). To force the use of the placeholder for specific arguments in nested expressions, enclose the right-hand side in braces {}.

    • x %>% f(y = nrow(.), z = ncol(.)) is equivalent to f(x, y = nrow(x), z = ncol(x)) (due to the first-argument rule).
    • x %>% {f(y = nrow(.), z = ncol(.))} is equivalent to f(y = nrow(x), z = ncol(x)) (overruling the rule with braces).
    x %>% {f(y = nrow(.), z = ncol(.))}
  2. Use basic piping with %>%

    main

    The %>% operator pipes the left-hand side value into the first argument of the function on the right-hand side. This allows for structuring data operations from left-to-right, avoiding nested function calls and temporary variables.

    • 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)))
    the_data <-
      read.csv('/path/to/data/file.csv') %>%
      subset(variable_a > x) %>%
      transform(variable_c = variable_a/variable_b) %>%
      head(100)
  3. Install magrittr

    main

    You can install magrittr as part of the full tidyverse suite, as a standalone package from CRAN, or via GitHub using pak or devtools.

    # 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")
  4. Build unary functions using the dot

    main

    A pipeline starting with the . returns a function. This allows you to build new functions by composing existing ones using the pipe.

    f <- . %>% cos %>% sin 
    # is equivalent to 
    f <- function(.) sin(cos(.))
  5. Use the argument placeholder (.)

    main

    The dot . acts as a placeholder for the left-hand side value. This is useful when you want to pass the piped value to an argument other than the first one.

    • x %>% f(y, .) is equivalent to f(y, x)
    • x %>% f(y, z = .) is equivalent to f(y, z = x)
    # Example of using the placeholder for a non-first argument
    x %>% f(y, z = .)
  6. Expose variables with the %$% operator

    main

    The %$% operator (exposition operator) allows you to expose the variables within a data object (like a data frame) to the right-hand side expression. This is useful for functions that do not have a formal data argument but operate on variables within a data structure.

    iris %>%
      subset(Sepal.Length > mean(Sepal.Length)) %$% 
      cor(Sepal.Length, Sepal.Width)
    
    data.frame(z = rnorm(100)) %$% 
      ts.plot(z)