Hi Folks,
I'm wondering if there's a secret trick to achieve the following.
I have some big code for analysis related to a named variable,
which will be one of several in the columns of a dataframe, and
I would like to be able to choose between a run for just one of
these variables, or a run which loops over them all.
So, for a single run, I could have the following kind of thing
in a file code.R to be sourced by source("code.R"):
# VarName <- "Var1"
VarName <- "Var2"
# VarName <- "Var3"
# VarName <- "Var4"
### CUT OUT LOOP
# VarNames <-
c("Var1","Var2","Var3","Var4")
# for( VarName in VarNames ) {
<< Lots of code related to analysis of variable VarName >>
### CUT OUT END OF LOOP
# }
which would do the single case for Var2. A run for a different VarName
would be done by editing the first block to select the new VarName.
Now, when I want to run the loop over all VarNames, I could of course
likewise edit the file so as to uncomment the code which follows the
"CUT OUT ..." lines. But I would prefer to avoid having to do the
latter, and am therefore wondering if there is some way to conditionally
skip over those bits of code. The "logic" I'm looking for (which
is
of course syntactically wrong) is exemplified by:
LOOP <- TRUE
# LOOP <- FALSE
# VarName <- "Var1"
VarName <- "Var2"
# VarName <- "Var3"
# VarName <- "Var4"
### CONDITIONALLY CUT OUT LOOP
VarNames <-
c("Var1","Var2","Var3","Var4")
if(LOOP) {
for( VarName in VarNames ) {
}
<< Lots of code related to analysis of variable VarName >>
### CONDITIONALLY CUT OUT END OF LOOP
if(LOOP) {
}
}
Instinct hints that there may be a way involving eval() and
parse() and all that, but I have no skills in that domain!
(The reason for wanting to do this is that the start of the
loop is already a long way down the file, and the end of the
loop a long way further down the file, whereas the settings
of VarName for the single case, and of LOOP for the loop, can
be placed at the start of the file where editing is much easier
and quicker and less error-prone).
With thanks,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Oct-08 Time: 11:53:04
------------------------------ XFMail ------------------------------