Learn R Programming

commandr (version 1.0.1)

setStage: Define a Stage Class

Description

This function defines new derivatives of the Stage class. It is a wrapper around setClass and thus has a similar interface.

Usage

setStage(name, dispname = name, intype = "ANY", outtype = intype, where = topenv(parent.frame()))

Arguments

name
The name of the stage, i.e., the role string
dispname
The name for display in a user interface
intype
The class of the data that protocols of this stage accept as input
outtype
The class of the data that protocols of this stage accept as output
where
The environment in which to define the stage class

Value

The name of the role

Details

Calling setStage defines two classes:
  • A derivative of Stage that represents this stage
  • A derivative of Protocol from which all protocols that implement this stage derive

For example, if we had a stage named “average”, calling setStage would create the classes StageAverage and ProtoAverage. The function also defines a generic, named by the name argument, that performs a protocol of this stage. There is a method that takes an object of type intype as first argument and a method name as its second. Additional arguments are passed to the perform method of the protocol. In our prior example, there would be a generic called average and method average,numeric if intype was given as “numeric”.

It also defines a generic of the form nameProto that serves as an accessor for protocols of this stage. A method is defined for Pipeline and outtype, so that one could retrieve our “average” protocol with averageProto(pipeline) or averageProto(result). Similarly, a replacement generic and methods are defined.

See Also

The Stage class; setProtocol for defining a new type of protocol

Examples

Run this code
## simplest definition
setStage("average")
## add a display name and specialize to numeric input
setStage("average", "Average Vector", intype = "numeric")
setProtocol("mean", fun = mean, parent = "average")
setProtocol("quantile", representation = list(probs = "numeric"),
            fun = quantile, parent = "average")
setProtocol("range", representation = list(low = "numeric", high = "numeric"), 
            fun = function(x, low = 0, high = Inf) x[x >= low & x <= high],
            parent = setStage("trim", intype = "numeric"))

## Class Stage derivative
showClass("StageAverage")
## Class Protocol derivative
showClass("ProtoAverage")

## generic defined
showMethods("average")

# try this generic method
d <- c(1, 2, 4)
average(d, "mean")

## create a pipeline
p <- Pipeline("trim", "average")
res <- perform(p, d)
res
## generic *Proto
averageProto(p)
averageProto(res)

Run the code above in your browser using DataLab