build_factory: Easily Build Function Factories

Description Usage Arguments Value Examples

View source: R/build_factory.R

Description

Easily Build Function Factories

Usage

1
build_factory(fun, ..., .pass_dots = FALSE, .internal_variables = NULL)

Arguments

fun

A function to turn into a factory.

...

Arguments for the factory function. Things on the RHS will be evaluated before building your factory unless explicitly quoted with quote. See examples.

.pass_dots

A logical indicating whether the factory should accept additional arguments (...) to pass on to methods. In order for this to work, the manufactured function *must* also include dots, and the input fun must indicate where those dots are used.

.internal_variables

A named list of additional code to run to create additional variables used by the factory.

Value

A function factory.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
y <- 2
power <- build_factory(
  fun = function(x) {
    x^exponent
  },
  exponent
)
square <- power(y)
square(2)
y <- 7
square(2)

base_bins <- build_factory(
  .internal_variables = list(
    nclass_fun = switch(
      type,
      Sturges = nclass.Sturges,
      scott = nclass.scott,
      FD = nclass.FD,
      stop("Unknown type", call. = FALSE)
    )
  ),
  fun = function(x) {
    (max(x) - min(x) / nclass_fun(x))
  },
  type
)
base_bins("Sturges")

jonthegeek/factory documentation built on June 4, 2020, 1:41 p.m.