[R] New unique name

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon Apr 19 13:52:13 CEST 2004


On Mon, 19 Apr 2004, Barry Rowlingson wrote:

> (Ted Harding) wrote:
>
> > A bit sledgehammer for nut, but as least it meets your needs!
>
>   You could even use uuidgen to create a universally unique ID, and then
> use make.names to R-ify it:
>
>  > make.names(system("uuidgen",intern=T))
> [1] "aa33d26c.88a5.4eab.94ba.5073c4718ffe"
>  > make.names(system("uuidgen",intern=T))
> [1] "d5aea7a0.81e9.4690.8d48.d74fd2b50a83"
>  > make.names(system("uuidgen",intern=T))
> [1] "X2570d3e0.6b07.42be.a9c6.3701ac82b4f0"
>
>   Of course the requirement was to make an object with a name that
> didn't already exist, and there's no guarantee that you haven't already
> created an object called "X2570d3e0.6b07.42be.a9c6.3701ac82b4f0" for
> some reason.
>
>   So that's no good.
>
>   How about getting an alphabetically-sorted list of all the current
> objects and then using the last one augmented with something:
>
> makeUnique <- function(){
>   allObjects=sort(ls(pos=seq(1:length(search()))))
>   paste(allObjects[length(allObjects)],"-temp",sep='')
> }
>
>   I'm sure someone can come up with a bigger sledgehammer, or a reason
> why this wont always be unique. What's the longest name of an object in
> R? That'll break it... Pah.

There is more to the environment than the search path, which is what makes
this tricky.  I suspect Erich really only wanted to avoid objects in the
current frame (which is not on the search path except for interactive use
of R), but he didn't tell us too precisely.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list