knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The goal of composer is to permit readable and convenient function composition. Its main features are :

installation

devtools::install_github("moodymudskipper/composer")

composer_vector

We can define a composer_vector from any regular vector or list

library(composer)
vec <- c("A: hello there", "B: hi handsome")
vec <- co(vec)
vec
class(vec)

Or define it directly

identical(vec, co("A: hello there", "B: hi handsome"))

[.composer_vector method

Standard subsetting works with [ and preserves class

vec <- vec[c(1,2,1)]
vec
class(vec)

We can use predicate formulas or functions as additional arguments that will be applied on the data filtered on the first argument

vec[,~startsWith(.,"A")]
vec[startsWith = "A"]
vec[1:2, startsWith = "A"]

[[.composer_vector method

while [.composer_vector doesn't break any code as we only functionalities relatated to a new ... argument, [[.composer_vector is completely different and will be used for transformations.

Using [[ with numeric arguments, we extract a substring by character indices (wraps stringr::str_sub)

vec[[4, -1]]

We can apply the transformation on selected elements.

vec[[4,-1,.p = 2:3]]
vec[[4,-1,.p = c(TRUE, FALSE)]] # recycled
vec[[4,-1,.p = ~startsWith(.,"A")]]

Using [[ with a character argument, we extract a substring using regular expressions (wraps stringr::str_extract)

vec[["h."]]
vec[["(?<=\\s).*(?=\\s)"]]

Using [[ with two character arguments, we operate a replacement (wraps stringr::str_replace_all)

vec[["e|a|o","X"]]

Using [[ with formula or function arguments, or lists of arguments named like functions, we apply those in sequence on our vector.

vec[[toupper]]
vec[[4,-1, ~toupper(.), paste = ";)"]]


moodymudskipper/composer documentation built on May 17, 2019, 3 p.m.