[R] Create variable by name

Peter Langfelder peter.langfelder at gmail.com
Wed Oct 6 18:47:13 CEST 2010


On Wed, Oct 6, 2010 at 9:32 AM, Ralf B <ralf.bierig at gmail.com> wrote:
> Can one create a variable through a function by name
>
> createVariable <- function(name) {
>        outputVariable = name
>        name <- NULL
> }
>
> after calling
>
> createVariable("myVar")
>
> I would like to have a variable myVar initialized with NULL in my
> environment. Is this possible?

This dirty trick works for me:

createVariable <- function(name) {eval(parse(text = paste(name, "<<- NULL"))) }

Example:

> abcd
Error: object 'abcd' not found
> createVariable("abcd")
> abcd
NULL

Peter



More information about the R-help mailing list