knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
The goal of composer is to permit readable and convenient function composition. Its main features are :
compose2 which is a more flexible version of purrr::compose,
which supports formula notation among other features.. which applies these composed functions on the fly, and
produces code quite similar to standard pipe infused tidyverse code.composer_vector and its method [[.composer_vector which wraps
compose2 permits
very compact transformationsdevtools::install_github("moodymudskipper/composer")
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 methodStandard 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 methodwhile [.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 = ";)"]]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.