From murdoch.duncan at gmail.com  Sat Jun  4 13:26:08 2011
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sat, 04 Jun 2011 07:26:08 -0400
Subject: [Rd] S4 Slot assignment within function
In-Reply-To: <1307135016628-3572077.post@n4.nabble.com>
References: <1307135016628-3572077.post@n4.nabble.com>
Message-ID: <4DEA1650.50602@gmail.com>

On 11-06-03 5:03 PM, mcguirebc wrote:
> Is there a simple way to assign values to S4 slots from within a function?
>
> Doing this doesn't work:
>
>> assign_slot<-function(x){
>
> assign("OBJECT at slot",x,envir=parent.env(environment())
>
> }
>
>> assign_slot(x)
>
> All I get from this is a new object with the name OBJECT at slot, the slot
> assignment of OBJECT doesn't change.
>
> I have thought about solutions such as eval(parse()) to pull this off, but
> would prefer not to ugly up the code.
>
> Thoughts??
>
> I have searched rather thoroughly, but it is possible I overlooked
> something. If I did, apologies.

I haven't tried this, but I would expect

OBJECT at slot <<- x

to work, assuming OBJECT already exists somewhere outside the function 
but in its environment.

Duncan Murdoch



[Rd] S4 Slot assignment within function
Duncan Murdoch