S4generics: Wrapper for writing S4 generics and methods

Description Usage Arguments Details Examples

Description

These are two wrappers around setGeneric and setMethod. A relevant difference is that generics and methods are stored in the environment in which %g% and %m% are called and not in the top-environment. Furthermore both functions have side effects in that they will call globalVariables for the arguments and name of the generic.

Usage

1
2
3
lhs %g% rhs

lhs %m% rhs

Arguments

lhs

see details

rhs

the body as an expression

Details

The Syntax for the left hand side:
[<valueClass>:]<genericName>(<argList>)
- valueClass optional, is the class of the return value (see setGeneric)
- genericName the name of the generic function
- argList are name = value or name ~ type expressions. Name-Value expressions are just like in a function definition. Name-Type expressions are used to define the signature of a method (see setMethod). See %type% and the examples how to work with them.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# A new generic function and a method:
numeric : generic(x) %g% standardGeneric("generic") 
generic(x ~ numeric) %m% x
generic(1)

# Polymorphic methods in an object:
Object <- function() {
  numeric : generic(x) %g% standardGeneric("generic") 
  generic(x ~ numeric) %m% x
  retList("Object")
}
Object()$generic(1)

# Class Unions:
## This generic allows for return values of type numeric or character:
'numeric | character' : generic(x) %g% standardGeneric("generic")

## This method also allows for numeric or character as argument:
generic(x ~ character | numeric) %m% x
generic(1)
generic("")

aoos documentation built on May 2, 2019, 3:47 p.m.