On 10/3/07, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > On 10/3/2007 7:56 AM, (Ted Harding) wrote: > > Hi Folks, > > > > The question I'm asking, regarding the use of function > > definitions in the context described below, is whether > > there are subtle traps or obscure limitations I should > > watch out for. It is probably a rather naive question... > > > > Quite often, one has occasion to execute interactively > > a lot of R commands in which, from time to time, one has > > occasion to repeat exactly a sequence of commands which > > one has entered earlier. These commands would only refer > > to variables which have been created at the "top level" of > > the program and which exist at the time the sequence of > > commands is entered. > > > > So it would be convenient to refer to such a sequence of > > commands as a "named block" -- just give its name, and > > they are executed. > > > > In my experiments, wrapping the first occurrence of such > > a sequence in a function definition seems to work, e.g. > > the first time they are needed: > > > > block1 <- function(){ > > sequence of commands that you would have enetered > > for execution at this point > > } > > block1() > > > > This first call to block1() seems to work OK, in my tests, > > PROVIDED, of course, > > a) The variables it uses and assigns to exist already; > > b) all internal "<-" assignments are written "<<-". > > Then, of course, the next time that block is needed, > > you can call block1() again. > > > > But can this usage of function definition give rise > > to problems? R scoping can be a bit tricky! And I > > think I am perhaps being naive ... > > Doing this is a bad idea in the long run. Once you have several of > these little snippets defined, you'll forget the details of what each of > them does, and then the "side effects" of writing global variables will > come back and bite you really badly. >
There is a version of defmacro in the source code of the Rcmdr package that supports local variables. Any variable beginning with .. (two dots) is regarded to be local. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.