[R] how to pass object "members" in functions

Duncan Murdoch murdoch.duncan at gmail.com
Wed Dec 22 13:14:32 CET 2010


On 22/12/2010 6:20 AM, Paul Rigor wrote:
> Hello,
> This is an R-syntax question when attempting to manipulate/access objects
> when passed to a function.
>
> I have a function attempting to just print values attached to an argument
> object. For example,
>
> printThis<- function(obj, parm2, parm3) {
>     print(obj.stuff1)
>     print(obj.stuff2)
> }
>
> where I've assigned stuff1 and stuff2 to the actual object passed as such
>
> actualObject.stuff1<- c("list","of","something")
> actualObject.stuff2<- c("list","of","something other thing")
> printThis(actualObject) # actual call to the function above

You must be thinking of some other language.  actualObject.stuff1 is a 
completely separate variable from actualObject.  In R, you use $ to 
indicate a member of a list or environment, @ to indicate a slot in a 
formal object, and variations on [] to select elements of arrays and such.

Duncan Murdoch

>
>
> But I'm getting the following error when calling the printThis method on
> actualObject.
>     "object 'obj.stuff1' not found"
>
> How do I access all objects attached to a variable.  I think I'm
> misunderstanding this "." (dot) where it's most likely just an acceptable
> way of naming variables in R.
>
> So what's the best way to create an object such that I can access what I've
> assumed to be object properties?
>
> Thanks,
> Paul
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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