[R] New unique name

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 19 11:46:05 CEST 2004


On Mon, 19 Apr 2004, Erich Neuwirth wrote:

> In some languages there is a function
> gensym()
> which returns a new unique name (in the current environment).
> This is quite helpful when one has to do temporary assignments.
> I could not find such a function in R.
> Is there one?

Not that I know of.  `In the current environment' is problematic, since R
has many environments and lexical scoping so different functions can have
different environments.  It's better to pick your own:  
ErichNeuwirth190404a looks unlikely to be used by anyone else!
basename(tempfile("ENeuwirth")) also looks like a good start.

Normally this is done within the body of a function, and all one needs to 
do is to choose a symbol name not used in that function (although we do 
see occasional problem with the use of `x' causing masking). 
An alternative is to use a nested function for the computations, or to set 
up a temporary environment (most conveniently via local()).  (If perchance 
you meant the local frame, then using local() is the R equivalent.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list