[R] Macros and functions

John Maindonald u9801539 at leonard.anu.edu.au
Mon Nov 11 02:07:42 CET 2002

The following failed to make it to r-help when Murray
Jorgensen sent his message a little time ago.
[It seems to have been an issue of the machine from which
my message originated.]

Typing in statements from the command line transfers to the
computer a macro that was perhaps in the user's head.

Putting together small sequences of carefully thought out code,
in which key components have been carefully thought out and tested,
has to be, for most of us, better than trying to make it all up
on the run. An exception is necessary for those unusual people
who who (akin to the authors of novels who do the word-processing
in their minds and then gleefully use a typewriter to commit it
to paper) can hold and rearrange in their mind whole pages of code.

For chunks of code that will be executed frequently, perhaps the
advice should be to think carefully, at an early point in the
exercise, about the trade-offs between macro execution and use
of a function.  Will the same code be used at future sessions?
Which mode of operation makes it easiest to document code, input
and output data, and the sequence of steps followed?  Are there
subtleties in one or other mode of operation that would be best

Novices may be wise to get thoroughly comfortable with a command
line/source script mode of operation, before launching far into
the use of functions.  An issue here is that much of what many
novices find quite subtle (e.g., some aspects of the syntax and
semantics of functions) becomes entirely natural and obvious for
experienced users.

Compared to the use of the underscore ("_") for assignment, a
careful use of macros may actually be quite virtuous!

John Maindonald

>Murray Jorgensen wrote -
>R is a function-oriented language and functions have the advantage that
>they control unintended side effects of computations on the workspace.
>Still, there are times when I am using R when I want to make a large
>number of changes to the worksheet repeatedly and where the easiest way
>   seems to be to keep pasting in the same chunk of code, or to put it on
>a file and use source("filename"). So effectively R does have macros as
>well as functions.
>Question: is the use of macros in this way 'bad practice' to be
>deprecated, and if so why? (If I decide that it is bad practice I will
>probably go on being bad, but I will try not to pass on bad habits to my

John Maindonald             email: john.maindonald at anu.edu.au
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Bioinformation Science, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list