https://github.com/knapply/knapplyr
https://github.com/knapply/knapplyr
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/knapply/knapplyr
- Owner: knapply
- License: gpl-3.0
- Created: 2019-10-04T12:19:51.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-19T20:01:59.000Z (over 6 years ago)
- Last Synced: 2025-01-16T03:14:25.807Z (over 1 year ago)
- Language: R
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output:
github_document:
html_preview: true
always_allow_html: yes
editor_options:
chunk_output_type: console
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
# collapse = TRUE,
fig.align = "center",
comment = "#>",
fig.path = "man/figures/",
message = FALSE,
warning = FALSE
)
# options(width = 400)
```
# `{knapplyr}`
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://github.com/knapply/knapplyr/commits/master)
[](https://codecov.io/gh/knapply/knapplyr?branch=master)
[](https://ci.appveyor.com/project/knapply/knapplyr)
[](https://travis-ci.org/knapply/knapplyr)
[](https://www.gnu.org/licenses/gpl-3.0)
[](https://www.r-project.org/)
[](https://github.com/knapply/knapplyr)
[](http://hits.dwyl.io/knapply/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.
```{r, echo=FALSE}
`%||%` <- knapplyr:::`%||%`
`%{NULL}%` <- knapplyr:::`%{NULL}%`
`%{}%` <- knapplyr:::`%{}%`
`%{NA}%` <- knapplyr:::`%{NA}%`
`%{""}%` <- knapplyr:::`%{""}%`
```
# Default-ers
```{r, echo=FALSE}
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
```{r, echo=FALSE}
.map <- knapplyr:::.map
.map_chr <- knapplyr:::.map_chr
.map_lgl <- knapplyr:::.map_lgl
.map_int <- knapplyr:::.map_int
.map_dbl <- knapplyr:::.map_dbl
```
```{r}
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
```{r, echo=FALSE}
.map2 <- knapplyr:::.map2
.map2_chr <- knapplyr:::.map2_chr
.map2_lgl <- knapplyr:::.map2_lgl
.map2_int <- knapplyr:::.map2_int
.map2_dbl <- knapplyr:::.map2_dbl
```
```{r}
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))
```
```{r, echo=FALSE}
.set_names <- knapplyr:::.set_names
```
```{r}
n <- .set_names("named")
n
```