Learn R Programming

svMisc (version 1.2.3)

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

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

# 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

Run this code
# NOT RUN {
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))
# }

Run the code above in your browser using DataLab