| Title: | Utilities for Working with R's Operators |
|---|---|
| Description: | Provides a collection of utilities that allow programming with R's operators. Routines allow classifying operators, translating to and from an operator and its underlying function, and inverting some operators (e.g. comparison operators), etc. All methods can be extended to custom infix operators. |
| Authors: | Christopher Brown [aut, cre], Decision Patterns [cph] |
| Maintainer: | Christopher Brown <[email protected]> |
| License: | GPL-2 | file LICENSE |
| Version: | 1.6.3 |
| Built: | 2026-06-01 10:40:55 UTC |
| Source: | https://github.com/decisionpatterns/operator.tools |
tests whether an object can be coerced to a operator, optionally an operator of 'types'.
can.operator(x, ...)can.operator(x, ...)
x |
object; to test |
... |
additional arguments
|
logical
fun2name compares a function (body) to all defined functions. If an
identical match is found to a defined function, that function is returned.
NB. This does not search through S4 methods.
fun2name(f) name2fun(x)fun2name(f) name2fun(x)
f |
function |
x |
name; more specifically, an object to be converted into a name and eval'd
|
name2fun simply converts its argument to a name and than evals it
to produce a function definition
fun2name: character (name of function) name2fun: function
inverse is a S3 generic method for inverting an R operator in the
mathematical sense. Presently, inverses are defined for relational
operators, i.e. changing > to <= etc.
inverse(x, ...)inverse(x, ...)
x |
object representing an R operator |
... |
additional arguments |
Arguments will be checked against the defined list of inverses, If an entry exists, the corresponding inverse is returned.
inverse returns the inverse in the same form as the x
argument. Thus, if a name is provided, a name is returned. If a function is
provided, a function is returned.
Christopher Brown
http://en.wikipedia.org/wiki/Inverse_mathematics.
operators especially operators(type="relational"))
## Not run: inverse( as.name( '!=' ) ) inverse( `==` ) ## End(Not run)## Not run: inverse( as.name( '!=' ) ) inverse( `==` ) ## End(Not run)
These S4 Methods are utilies for working with operators. In R, operators are functions with special syntax.
is.operator(x, ...)is.operator(x, ...)
x |
object to be tested or coerced. Can be |
... |
additional arguments passed to |
is.operator tests whether the object is one of the defined
operators.
can.operator tests whether the object can be coerced to an operator.
as.operator coerced the object to an operator.
Optionally, you can specify one of the that it tests for a specific type of operator. See details, below.
An operator is R function with special syntax.
( See ??operator for examples of each. )
is.operator tests whether the argumenst is an operator.
as.operator coerces x to a operator, otherwise fails.
can.operator test whether the object can be coerced to an operator.
All functions can accepts a types argument which is passed to
link{operators}. By specifying one or more types, these functions
test using those types only.
New operators can be "registered" using setOperator.
is.operator and can.operator return logical.
as.operator returns the argument coerced to the concommitant R
function.
Christopher Brown
## Not run: is.operator( `+` ) is.operator( 'xyzzy' ) is.operator( `+`, types="arithmetic" ) is.operator( `+`, types="relational" ) can.operator( `+` ) can.operator( 'xyzzy' ) can.operator( `+`, types="arithmetic" ) can.operator( `+`, types="relational" ) as.operator( `+` ) as.operator( '+' ) as.operator( as.name('+') ) ## End(Not run)## Not run: is.operator( `+` ) is.operator( 'xyzzy' ) is.operator( `+`, types="arithmetic" ) is.operator( `+`, types="relational" ) can.operator( `+` ) can.operator( 'xyzzy' ) can.operator( `+`, types="arithmetic" ) can.operator( `+`, types="relational" ) as.operator( `+` ) as.operator( '+' ) as.operator( as.name('+') ) ## End(Not run)
Commonly created NOT-IN operator
x %!in% tablex %!in% table
x |
object on the lhs |
table |
object/list on the rhs |
Given an operator or its name/symbol, return the type of operator.
operator.type(op)operator.type(op)
op |
An operator either as a name/symbol or function. |
The operator is first checked against all operators that have been
registered with the setOperator command. If there is a match,
its type is returned. If no matching operator is found, op is matched
against unregistered operators that have been defined with the
%any%-syntax. If a match is found, UNREGISTERED is returned.
The list of operators are maintained in .Options\$operators and be
altered suing the setOperator command.
A character value.
For registered operators, the registered type is returned. For Base R
operators, the types come from Syntax.
For operators defined with the %any%-syntax but, not registered using
setOperator, "UNREGISTERED" is returned.
NULL is returned otherwise.
Christopher Brown
operators, setOperator. Syntax
## Not run: operator.type( `+` ) operator.type( `<=` ) e <- quote( A +B ) operator.type( e[[1]] ) operator.type( as.name('+') ) ## End(Not run)## Not run: operator.type( `+` ) operator.type( `<=` ) e <- quote( A +B ) operator.type( e[[1]] ) operator.type( as.name('+') ) ## End(Not run)
operators returns the names of defined operators. Argument
types can be used to select operators of a specified type(s) or
GROUPING(s). See Details for specifics.
operators(types = "REGISTERED")operators(types = "REGISTERED")
types |
A character vector with the types of operators to return. The
types may one or more of: 'namespace', 'component', 'indexing', 'sequence',
'arithmetic', 'relational', 'logical', 'tilde', 'assignment', 'help',
'user', or user-defined type specified in a call to
By default, only registered operators are returned. This is purely for
performance reasons as an exhausting search for See Syntax.for the core R operators
|
character vector of unique operator names.
The right arrow assignment operators, -> and ->> is not
an operator but a syntatic variant. Consequently, it does not behave
properly as an operator. They are omitted from the operator list as they
are not correctly identified as primitives or functions by the R language.
Christopher Brown
https://cran.r-project.org/doc/manuals/R-lang.html https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14310
Syntax, setOperator,
setOperators, and the help files on the individial operators.
## Not run: operators() operators( types="arithmetic" ) operators( types=c("arithmetic","logical" ) ) operators( types='ALL' ) operators( types='REG' ) operators( types='UNREG' ) operators( types='SPECIAL' ) ## End(Not run)## Not run: operators() operators( types="arithmetic" ) operators( types=c("arithmetic","logical" ) ) operators( types='ALL' ) operators( types='REG' ) operators( types='UNREG' ) operators( types='SPECIAL' ) ## End(Not run)
rel.type gets the relational type of a relational operator. The
relational type is one of 'gt', 'lt', 'eq',
'ne'.
rel.type(x)rel.type(x)
x |
An operators expressed as a |
A relational operator is an operate that relates the relationship between arguments. The core relational operators are: >, >=, <, <=, ==, !=,
The relational.type is a simple roll-up of these operators. > and >= are gt,
etc. The value is retrieved from .Options$operators[[x]][['rel.type']] and
can be defined for relational operators using setOperator.
A relational type provides an indication of nature of the relational operator.
character value of the operator. One of: 'gt',
'lt', 'eq', 'ne'.
Christopher Brown
## Not run: rel.type( `==` ) rel.type( as.name('==') ) ## End(Not run)## Not run: rel.type( `==` ) rel.type( as.name('==') ) ## End(Not run)
removeOperator unregisted an operator by removing it from the list of
operators found at .Options$operators . All operator attributes are
that have been set will be lost.
removeOperator(x)removeOperator(x)
x |
|
Warns if the operator has not been registered.
None. Used for side-effects.
Christopher Brown
setOperators for registering Operators.
# Unregister ? as an operator. ## Not run: removeOperator( '?' ) ## End(Not run)# Unregister ? as an operator. ## Not run: removeOperator( '?' ) ## End(Not run)
setOperator registers a user-defined operator as a given type.
Subsequently, this operator can be treated as a member of a class of
operators.
setOperator(name, type = "user", ...) setOperators(...)setOperator(name, type = "user", ...) setOperators(...)
name |
A character vector containing the names of one or more functions which will be registered. |
type |
The type of operator. See Details. |
... |
Attributes for the operator(s). |
setOperators scans defined functions looking for any that have been
defined by the user using the special any syntax. If found, these
are registered with setOperator and given the default type='user'.
setOperator registers a single operator similar to the way that
setMethod registers a method. The definition for these operators are
defined by .Options$operators.
setOperators scans the environments for user-defined operators. If
found and not already registered, these are registered by
setOperator. Registered operators are much more efficient than
unregisted ones, so it is often advantageous to register the operators. When
... is supplied, these attributes are set for all unregistered
operators.
Operators are allowed to have attributes. The one required attribute is
type, which is just a character value that serves to classification
the operator. On package load, All operators from base R are assigned a
core type as specified in Syntax. These are: namespace,
component, indexing, sequence, arithmetic, arithmetic, relational, logical,
tilde, assignment, help.
Users may use one of these types or assign a type of their own choosing. The
type is largely unrestricted, but cannot be one of the reserved
operator groupings: ALL, REG(ISTERED), UNREG(ISTERED), SPECIAL or user.
These have special meaning as described in operators. Users
are encouraaged to make their own types in lower case.
None. This function exists for assigning a operator to
options('operators').
Christopher Brown
## Not run: setOperator( '%!in%', 'relational' ) operators( type='relational' ) ## End(Not run)## Not run: setOperator( '%!in%', 'relational' ) operators( type='relational' ) ## End(Not run)