[R] New unique name

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Mon Apr 19 13:00:35 CEST 2004

On 19-Apr-04 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?

If you are running R on a Unix-like system (such as Linux) then
you could use the "mktemp" function (see 'man mktemp').

In R,

  system("mktemp -u tmpXXXXXX")

will return a unique identifier (and will not, because of the
"-u" flag, create the file).

The identifier will be of the form (e.g.) "tmpdPT6Nw", i.e. with
the 6 X's replaced by random characters.

A bit sledgehammer for nut, but as least it meets your needs!

(Strictly speaking, uniqueness in repeated use is not absolutely
guaranteed with the "-u" option, since strict uniqueness depends
on 'mktemp' checking for an existing file with the same name.
However, since there are some (26+26+10)^6 > 10^10 combinations
to randomly choose from, you should be pretty safe.)

Best wishes,

E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 19-Apr-04                                       Time: 12:00:35
------------------------------ XFMail ------------------------------

More information about the R-help mailing list