The macro package contains a function to preprocess R scripts, and output a resolved code file. Pre-processor commands are implemented as special comments.
The macro package attempts to create a macro language for R that is similar to the SAS® macro language.
There is only one function in the package:
msource. This function acts as a substitute for the Base R
source function. The difference is that msource first runs
a pre-processor to resolve macro statements. The resolved macro statements
are emitted to a separate file. The msource function then sources that
file normally.
By default, the pre-processor output is written to a temporary location, and deleted when the operation is complete. If desired, you may also supply a path to save this file to a location of your choosing.
Macro commands are implemented as a special form of R comment. The special
comments begin with "#%". For example, a macro assignment is written as
#%let a <- 1.
Here are the macro features supported by the system:
Macro Comments: A comment just for macro statements.
Macro Variables: Initialize macro variables to be used as text replacement tokens.
Macro Conditionals: Emit code conditionally in pre-processing.
Macro Include: Insert code from external files into your program.
Built-In Macro Functions: A small number of essential built-in macro functions.
Macro Do Loops: Emits a block of code repeatedly.
User-Defined Macro Functions: Custom macro functions to enhance reuse and reduce redundancy.
Macro Line Continuation: A line continuation operator that allows you to write macro commands that span more than one line.
The above features give you a simple yet flexible way to perform meta-programming. In this way, the macro package can be useful in several situations, notably for code generation.
See the msource function documentation for additional details.
Useful links: