knitr::opts_chunk$set( # collapse = TRUE, fig.align = "center", comment = "#>", fig.path = "man/figures/", message = FALSE, warning = FALSE ) # options(width = 400)
{knapplyr}
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.
`%||%` <- knapplyr:::`%||%` `%{NULL}%` <- knapplyr:::`%{NULL}%` `%{}%` <- knapplyr:::`%{}%` `%{NA}%` <- knapplyr:::`%{NA}%` `%{""}%` <- knapplyr:::`%{""}%`
library(dplyr) foos <- c("`%||%`", "`%{NULL}%`", "`%{}%`", "`%{NA}%`", '`%{""}%`') exs <- c('NULL %||% "common default NULL replacement"', 'NULL %{NULL}% "alternate NULL replacement"', 'character(0) %{}% "default empty replacement"', 'NA %{NA}% "default NA replacement"', '"" %{""}% "default empty string replacement"') tibble(`{knapply} Function` = foos, Example = exs) %>% mutate(Result = glue::glue("`{purrr::map_chr(Example, ~ eval(parse(text = .x)))}`"), Example = glue::glue('`{Example}`')) %>% knitr::kable(format = "html", escape = FALSE)
.map*()
-ers.map <- knapplyr:::.map .map_chr <- knapplyr:::.map_chr .map_lgl <- knapplyr:::.map_lgl .map_int <- knapplyr:::.map_int .map_dbl <- knapplyr:::.map_dbl
l1 <- list(a = 1:3, b = -1:-3) l1 .map(l1, as.character) .map_chr(l1, paste, collapse = ", ") .map_int(l1, sum) .map_dbl(l1, function(x) sum(x) * 2.5) .map_lgl(l1, function(x) all(x > 0))
.map2*()
-ers.map2 <- knapplyr:::.map2 .map2_chr <- knapplyr:::.map2_chr .map2_lgl <- knapplyr:::.map2_lgl .map2_int <- knapplyr:::.map2_int .map2_dbl <- knapplyr:::.map2_dbl
l2 <- list(a = 4:6, b = -4:-6) l2 .map2(l1, l2, c) .map2_chr(l1, l2, paste, collapse = " | ") .map2_int(l1, l2, min) .map2_dbl(l1, l2, function(x, y) sort(c(x, y))[[1L]]) .map2_lgl(l1, l2, function(x, y) all(c(x, y) > 0))
.set_names <- knapplyr:::.set_names
n <- .set_names("named") n
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.