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 class 'subsettable_type'
x$name
# S3 method for class 'subsettable_which'
x$name
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: 0x5616f0993dc8>
#> 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))