defmacro define a macro that uses R expression replacement
strmacro define a macro that uses string replacement
Usage
defmacro(..., expr)
strmacro(..., expr, strexpr)
Arguments
...
macro argument list
expr
R expression defining the macro body
strexpr
character string defining the macro body
Details
defmacro and strmacro create a macro from the expression
given in expr, with formal arguments given by the other
elements of the argument list.
A macro is similar to a function definition except for handling of
formal arguments. In a function, formal arguments are simply
variables that contains the result of evaluating the expressions
provided to the function call. In contrast, macros actually modify
the macro body by replacing each formal argument by the
expression (defmacro) or string (strmacro) provided to
the macro call.
For defmacro, the special argument name DOTS will be
replaced by ... in the formal argument list of the macro so
that ... in the body of the expression can be used to obtain
any additional arguments passed to the macro. For strmacro you
can mimic this behavior providing a DOTS="" argument. This is
illustrated by the last example below.
Macros are often useful for creating new functions during code execution.
Value
A macro function.
Note
Note that because [the defmacro code] works on the parsed expression,
not on a text string, defmacro avoids some of the problems of
traditional string substitution macros such as strmacro and the C
preprocessor macros. For example, in
mul <- defmacro(a, b, expr={a*b})
a C programmer might expect
mul(i, j + k) to expand (incorrectly) to i*j + k. In fact it
expands correctly, to the equivalent of i*(j + k).
For a discussion of the differences between functions
and macros, please Thomas Lumley's R-News article (reference below).
Author(s)
Thomas Lumley wrote defmacro. Gregory R. Warnes
greg@warnes.net enhanced it and created
strmacro.
References
The original defmacro code was directly taken from: