https://github.com/UchidaMizuki/adverbial
Enhanced Partialised and Composed Functions
https://github.com/UchidaMizuki/adverbial
Last synced: 4 months ago
JSON representation
Enhanced Partialised and Composed Functions
- Host: GitHub
- URL: https://github.com/UchidaMizuki/adverbial
- Owner: UchidaMizuki
- License: other
- Created: 2022-04-30T04:30:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-17T04:54:14.000Z (6 months ago)
- Last Synced: 2025-07-23T13:34:03.273Z (4 months ago)
- Language: R
- Homepage:
- Size: 63.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - UchidaMizuki/partialised - Partialised Functions (R)
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# adverbial (former partialised)
[](https://CRAN.R-project.org/package=adverbial)
[](https://github.com/UchidaMizuki/adverbial/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/UchidaMizuki/adverbial)
adverbial provides `new_partialised()` and `new_composed()`, which extend
`partial()` and `compose()` functions of purrr to make it easier to extract and
replace arguments and functions, and has additional adverbial functions such as
`as_step()` for step-by-step data processing.
## Installation
You can install the development version of adverbial from [GitHub](https://github.com/) with:
``` r
# the released version from CRAN:
install.packages("adverbial")
# the development version from GitHub:
# install.packages("devtools")
devtools::install_github("UchidaMizuki/adverbial")
```
## Examples
```{r example-load}
library(adverbial)
```
### Enhanced partialised functions
`new_partialised()` is an enhanced version of `partial()` from purrr.
It allows you to extract and replace arguments of the function.
```{r example-partialised}
dist <- function(x, y) {
sqrt(x ^ 2 + y ^ 2)
}
pdist <- new_partialised(
dist,
list(x = 3)
)
pdist
pdist(y = 4)
# Get partialised arguments
pdist[]
pdist$x
pdist$y
pdist$x <- 6
pdist(y = 8)
pdist$y <- 8
pdist()
```
### Enhanced composed functions
`new_composed()` is an enhanced version of `compose()` from purrr.
It allows you to extract and replace functions in the composition.
```{r example-composed}
square <- function(x) x^2
cdist <- new_composed(
list(
square = square,
sum = sum,
sqrt = sqrt
),
dir = "forward"
)
cdist
cdist(1:10)
# Get composed functions
cdist[]
cdist$sum <- new_partialised(sum, list(na.rm = TRUE))
cdist(c(1:10, NA))
```
### Step-by-step data processing
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
`step_by_step()` defines a step-by-step data processing pipeline by passing a
character vector with step names and descriptions.
`as_step()` converts an existing function into a step function that can be used
in a pipeline.
Generated functions check if a step is correct for objects created with
`step-by-step()` and act as a normal function for other objects.
With `as_step(f)` (without passing a second argument) you can add another
function to step-by-step data processing.
`end_step()` is a function that can be used to end the step-by-step data
processing pipeline and return the result.
```{r example-step-by-step}
# Define a step-by-step data processing pipeline
dist_calculator <- step_by_step(c(
square_step = "Square the input",
sum_step = "Sum the squares",
sqrt_step = "Take the square root"
))
# Define the steps
square_step <- as_step(function(x) x^2, "square_step")
sum_step <- as_step(sum, "sum_step")
sqrt_step <- as_step(sqrt, "sqrt_step")
square_step
sum_step
sqrt_step
dist <- dist_calculator(c(1:10, NA))
dist
dist <- dist |>
square_step() |>
sum_step(na.rm = TRUE) |>
sqrt_step()
dist
end_step(dist)
```