# NOT RUN {
## examples for a simple class with two numeric slots.
## (Run example(setMethod) to see the class and function definitions)
# }
# NOT RUN {
## methods for plotting track objects
##
## First, with only one object as argument, plot the two slots
## y must be included in the signature, it would default to "ANY"
setMethod("plot", signature(x="track", y="missing"),
function(x, y, ...) plot(x@x, x@y, ...)
)
## plot numeric data on either axis against a track object
## (reducing the track object to the cumulative distance along the track)
## Using a short form for the signature, which matches like formal arguments
setMethod("plot", c("track", "numeric"),
function(x, y, ...) plot(cumdist(x@x, x@y), y, xlab = "Distance",...)
)
## and similarly for the other axis
setMethod("plot", c("numeric", "track"),
function(x, y, ...) plot(x, cumdist(y@x, y@y), ylab = "Distance",...)
)
t1 <- new("track", x=1:20, y=(1:20)^2)
plot(t1)
plot(qnorm(ppoints(20)), t1)
## Now a class that inherits from "track", with a vector for data at
## the points
setClass("trackData", contains = c("numeric", "track"))
tc1 <- new("trackData", t1, rnorm(20))
## a method for plotting the object
## This method has an extra argument, allowed because ... is an
## argument to the generic function.
setMethod("plot", c("trackData", "missing"),
function(x, y, maxRadius = max(par("cin")), ...) {
plot(x@x, x@y, type = "n", ...)
symbols(x@x, x@y, circles = abs(x), inches = maxRadius)
}
)
plot(tc1)
## Without other methods for "trackData", methods for "track"
## will be selected by inheritance
plot(qnorm(ppoints(20)), tc1)
## defining methods for primitive function.
## Although "[" and "length" are not ordinary functions
## methods can be defined for them.
setMethod("[", "track",
function(x, i, j, ..., drop) {
x@x <- x@x[i]; x@y <- x@y[i]
x
})
plot(t1[1:15])
setMethod("length", "track", function(x)length(x@y))
length(t1)
## Methods for binary operators
## A method for the group generic "Ops" will apply to all operators
## unless a method for a more specific operator has been defined.
## For one trackData argument, go on with just the data part
setMethod("Ops", signature(e1 = "trackData"),
function(e1, e2) callGeneric(e1@.Data, e2))
setMethod("Ops", signature(e2 = "trackData"),
function(e1, e2) callGeneric(e1, e2@.Data))
## At this point, the choice of a method for a call with BOTH
## arguments from "trackData" is ambiguous. We must define a method.
setMethod("Ops", signature(e1 = "trackData", e2 = "trackData"),
function(e1, e2) callGeneric(e1@.Data, e2@.Data))
## (well, really we should only do this if the "track" part
## of the two arguments matched)
tc1 +1
1/tc1
all(tc1 == tc1)
# }
Run the code above in your browser using DataLab