[R] problem with assign and get
William Dunlap
wdunlap at tibco.com
Mon Feb 27 19:21:42 CET 2012
Sarah Gosless said
> But frequently there are better options than the
> use of get/assign for solving particular problems.
I tend to be more extreme say recommend that you forget
you ever heard of the get and assign functions.
First, out-of-the-local-evaluation-environment assignments
make for confusing code. You can usually do what you
need to do by having functions that return values.
Replacement functions, defined with
`something<-` <- function(x, ..., value) { x$something<-value; x }
and used as
something(x) <- newSomething
can be used to alter an existing object.
Second, if you decide that you need to do an assignment
outside of the current evironment, you ought to know which
environment you want to use. It is often .GlobalEnv.
Then you can use
yourEnvironment[["itemName"]]
instead of
get("itemName", envir=yourEnvironment)
and
yourEnvironment[["itemName"]] <- itemValue
instead of
assign("itemName", itemValue, envir=yourEnvironment)
The advantages of the [[ syntax are (a) it forces you
to decide where the data should be and (b) it lets you
use natural R syntax to modify objects. E.g.,
rownames(yourEnvironment[["itemName"]]) <- c("R1","R2")
will change the rownames of "itemName" in the environment
called yourEnvironment.
At somepoint you may decide to use a list instead of
an environment to hold your objects, in which case the
[[ syntax still works without any changes.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Sarah Goslee
> Sent: Monday, February 27, 2012 9:58 AM
> To: Jannis
> Cc: r-help at r-project.org
> Subject: Re: [R] problem with assign and get
>
> Hi,
>
> On Mon, Feb 27, 2012 at 12:41 PM, Jannis <bt_jannis at yahoo.de> wrote:
> > Dear list members,
> >
> >
> > does anyone have an idea why the following construction does not work but
> > gives the following error message:
> >
> > assign('test', array(1:10, dim=c(10,10)))
> > dimnames(get('test')) <- list(1:10,1:10)
> >
> >
> > Error in dimnames(get("test")) <- list(1:10, 1:10) :
> > target of assignment expands to non-language object
>
> There's no object to assign dimnames to. Think about it
> this way - what do you expect the dimnames of to be
> changed? "test" has no dimnames; get("test") would
> print the object named "test", but how could you change
> the dimnames of a displayed object?
>
> If you want to change the dimnames of the object named
> "test", you first need to use get("test") to assign that object
> to a new object, then change the dimnames, then assign
> the changed object to the desired name.
>
> myobject <- get("test")
> dimnames(myobject) <- list(1:10, 1:10)
> assign("test", myobject)
>
> Note: I'm assuming your small reproducible example (thank
> you!) is a surrogate for something more complex, so that
> dimnames(test) <- list(1:10, 1:10) is not an acceptable
> solution. But frequently there are better options than the
> use of get/assign for solving particular problems.
>
> Sarah
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list