README.md

{knapplyr}

Lifecycle GitHub last
commit Codecov test
coverage AppVeyor build
status Travis-CI Build
Status License: GPL
v3 Depends GitHub code size in
bytes HitCount

Utility functions to facilitate robust R programming that are heavily-inspired by {purrr}.

{knapplyr} is meant to serve as a controlled environment for development and testing. The functions are not so much intended to be imported elsewhere, but instead to make it easy to have sane, tested, dependency-free utilities that can be easily used as internal, utility functions in other packages.

This is mainly for personal purposes, but if you’d like to use it, just add the /R/knapply-utils.R file to your package’s /R folder. The functions will then be accessible just like any of your package’s other functions.

Default-ers

{knapply} Function Example Result `%||%` `NULL %||% "common default NULL replacement"` `common default NULL replacement` `%{NULL}%` `NULL %{NULL}% "alternate NULL replacement"` `alternate NULL replacement` `%{}%` `character(0) %{}% "default empty replacement"` `default empty replacement` `%{NA}%` `NA %{NA}% "default NA replacement"` `default NA replacement` `%{""}%` `"" %{""}% "default empty string replacement"` `default empty string replacement`

.map*()-ers

l1 <- list(a = 1:3, b = -1:-3)
l1
#> $a
#> [1] 1 2 3
#> 
#> $b
#> [1] -1 -2 -3
.map(l1, as.character)
#> $a
#> [1] "1" "2" "3"
#> 
#> $b
#> [1] "-1" "-2" "-3"
.map_chr(l1, paste, collapse = ", ")
#>            a            b 
#>    "1, 2, 3" "-1, -2, -3"
.map_int(l1, sum)
#>  a  b 
#>  6 -6
.map_dbl(l1, function(x) sum(x) * 2.5)
#>   a   b 
#>  15 -15
.map_lgl(l1, function(x) all(x > 0))
#>     a     b 
#>  TRUE FALSE

.map2*()-ers

l2 <- list(a = 4:6, b = -4:-6)
l2
#> $a
#> [1] 4 5 6
#> 
#> $b
#> [1] -4 -5 -6
.map2(l1, l2, c)
#> $a
#> [1] 1 2 3 4 5 6
#> 
#> $b
#> [1] -1 -2 -3 -4 -5 -6
.map2_chr(l1, l2, paste, collapse = " | ")
#>                       a                       b 
#>       "1 4 | 2 5 | 3 6" "-1 -4 | -2 -5 | -3 -6"
.map2_int(l1, l2, min)
#>  a  b 
#>  1 -6
.map2_dbl(l1, l2, function(x, y) sort(c(x, y))[[1L]])
#>  a  b 
#>  1 -6
.map2_lgl(l1, l2, function(x, y) all(c(x, y) > 0))
#>     a     b 
#>  TRUE FALSE
n <- .set_names("named")
n
#>   named 
#> "named"


knapply/knapplyr documentation built on Nov. 4, 2019, 3:54 p.m.