subsettable: Define a function as being 'subsettable' using $ operator

Description Usage Arguments Examples

Description

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.

Usage

1
2
3
4
5
## S3 method for class 'subsettable_type'
x$name

## S3 method for class 'subsettable_which'
x$name

Arguments

x

A subsettable_type function.

name

The value to use for the type= argument.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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

# This function can be used as usual:
foo(rnorm(50), type = "histogram")
# ... but also this way:
foo$histogram(rnorm(50))
foo$boxplot(rnorm(50))

Example output

Attaching package:svMiscThe following object is masked frompackage:utils:

    ?

function (x, type = c("histogram", "boxplot"), ...) 
{
    type <- match.arg(type, c("histogram", "boxplot"))
    switch(type, histogram = hist(x, ...), boxplot = boxplot(x, 
        ...), stop("unknow type"))
}
attr(,"class")
[1] "function"         "subsettable_type"

svMisc documentation built on Oct. 12, 2021, 1:08 a.m.