[R] OOP and passing by value

2010-06-09 Thread michael meyer
Greetings, I love the R system and am sincerely grateful for the great effort the product and contributors are delivering. My question is as follows: I am trying to use S4 style classes but cannot write functions that modify an object because paramter passing is by value. For example I want to

Re: [R] OOP and passing by value

2010-06-09 Thread Joris Meys
by returning the object ? setMethod(setData,test, function(this,fcn,k){ t...@t - as.numeric(seq(-k,k))/(2*k+1) t...@f - sapply(t...@t,FUN=fcn) #changed! return(this) #changed! } ) #---

Re: [R] OOP and passing by value

2010-06-09 Thread Joris Meys
In case you want the function setData to change the object itself (which is often a dangerous idea!), you can use instead : setMethod(setData,test, function(this,fcn,k){ Name - deparse(substitute(this)) t...@t - as.numeric(seq(-k,k))/(2*k+1) t...@f - sapply(t...@t,FUN=fcn)

Re: [R] OOP and passing by value

2010-06-09 Thread Henrik Bengtsson
Pass an object of, or containing an, environment. Then whenever you modify any object inside the environment, the changes will remain also when exiting from the function(s). This has been used by many for quite some time and is the standard way to do it, if you need this feature. See packages

Re: [R] OOP and passing by value

2010-06-09 Thread Philippe Grosjean
I think you should be interested by section 5.4 (non-local assignments; closures) in Chamber's Software for Data Analysis book published in 2008 by Springer (http://www.springer.com/statistics/computanional+statistics/book/978-0-387-75935-7). Besides the solution proposed in the book, which

Re: [R] OOP and passing by value

2010-06-09 Thread Joris Meys
Thanks for the correction. Personally, I never really use that kind of assignments, and the deparse(substitute()) construct I only use in self-defined plotting functions in simple scripts to be run on series of equivalent datasets by me, myself and I. When writing a package with nested functions

Re: [R] OOP and passing by value

2010-06-09 Thread michael meyer
Thanks to all replies. I was able to get it running with the R.oo package. I hope this reply makes it to the proper thread. Michael [[alternative HTML version deleted]] __ R-help@r-project.org mailing list