loading...

In case a textual argument allows for selecting the result, for instance, if plot() allows for several charts that you can choose with a type= or which=, making the function 'subsettable' also allows to indicate fun$variant(). See examples.

# S3 method for subsettable_type
$(x, name)

# S3 method for subsettable_which
$(x, name)

Arguments

x

A subsettable_type function.

name

The value to use for the type= argument.

Examples

foo <- structure(function(x, type = c("histogram", "boxplot"), ...) {
  type <- match.arg(type, c("histogram", "boxplot"))
  switch(type,
    histogram = hist(x, ...),
    boxplot = boxplot(x, ...),
    stop("unknow type")
  )
}, class = c("function", "subsettable_type"))
foo
#> function(x, type = c("histogram", "boxplot"), ...) {
#>   type <- match.arg(type, c("histogram", "boxplot"))
#>   switch(type,
#>     histogram = hist(x, ...),
#>     boxplot = boxplot(x, ...),
#>     stop("unknow type")
#>   )
#> }
#> <environment: 0x55a32b637178>
#> attr(,"class")
#> [1] "function"         "subsettable_type"

# This function can be used as usual:
foo(rnorm(50), type = "histogram")

# ... but also this way:
foo$histogram(rnorm(50))

foo$boxplot(rnorm(50))