[R] New unique name

Thomas Lumley tlumley at u.washington.edu
Mon Apr 19 16:22:25 CEST 2004


On Mon, 19 Apr 2004, Erich Neuwirth wrote:

> Since I have not been precise enough, let me explain in more detail.
> I am executing some R code in the COM server.
> For making function calls with parameters existing as ranges in Excel,
> I need to assign values for the arguments of the function call to
> temporary R variables.
> This is the only way of transferring large matrices quickly from Excel
> to R.
> Then, I construct a string which is the function call to be executed.
> The string contains the names of the newly created R variables
> for the function arguments.
> This string then is sent to R and executed as a command.

I used the following for local variables in a macro

gensym<-function(base=".v.",envir=parent.frame()){
   repeat{
        nm<-paste(base,paste(sample(letters,7,replace=TRUE),
                collapse=""),sep=".")
        if (!exists(nm,envir=envir))
                break
    }
    as.name(nm)
}


You wouldn't need the as.name() conversion, since you want a string.

A sufficiently clever person could come up with code that this could
conflict with (eg, if your code relied on the non-existence of
.v..agdefge), but in normal circumstances it should be fine.

	-thomas




More information about the R-help mailing list