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 do this:
setGeneric("setData", function(this,fcn,k){
standardGeneric("setData") })
setClass(
"test",
representation(f="numeric", t="numeric")
)
setMethod("setData","test",
function(this,fcn,k){
this@t <- as.numeric(seq(-k,k))/(2*k+1)
this@f <- sapply(t,FUN=fcn)
}
)
#-----------------------------------------------------------
tst <- new("test")
fcn <- function(u){ sin(2*pi*u) }
setData(tst,fcn,100)
tst@t # it's still empty because of pass by value
How can this be handled?
Many thanks,
Michael
[[alternative HTML version deleted]]
by returning the object ?
setMethod("setData","test",
function(this,fcn,k){
this at t <- as.numeric(seq(-k,k))/(2*k+1)
this at f <- sapply(this at t,FUN=fcn) #changed!
return(this) #changed!
}
)
#-----------------------------------------------------------> tst <- new("test")
> fcn <- function(u){ sin(2*pi*u) }
> New <- setData(tst,fcn,5)
> New at t
[1] -0.4545455 -0.3636364 -0.2727273 -0.1818182 -0.0909091 0.0000000
0.0909091 0.1818182 0.2727273 0.3636364 0.4545455
On Wed, Jun 9, 2010 at 2:28 PM, michael meyer <mjhmeyer at googlemail.com>
wrote:> 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 do this:
>
> setGeneric("setData", function(this,fcn,k){
standardGeneric("setData") })
>
> setClass(
> ?"test",
> ?representation(f="numeric", t="numeric")
> )
> setMethod("setData","test",
> ?function(this,fcn,k){
> ? ?this at t <- as.numeric(seq(-k,k))/(2*k+1)
> ? ?this at f <- sapply(t,FUN=fcn)
> ?}
> )
>
> #-----------------------------------------------------------
> tst <- new("test")
> fcn <- function(u){ sin(2*pi*u) }
> setData(tst,fcn,100)
> tst at t ? # it's still empty because of pass by value
>
>
> How can this be handled?
>
>
> Many thanks,
>
> Michael
>
> ? ? ? ?[[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.
>
--
Joris Meys
Statistical consultant
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control
tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
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 uses assignment outside
functions, there is another possibility: environment objects. They are
passed by reference and there are tricks to use them in this situation,
but you have to be extremely careful because you break the rules for
functional programming.
Don't forget generics like "Data<-" to assign in R. See for
instance
help("names<-"). This is the most conventional and easier way to do
this.
Best,
Philippe Grosjean
On 09/06/10 14:28, michael meyer wrote:> 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 do this:
>
> setGeneric("setData", function(this,fcn,k){
standardGeneric("setData") })
>
> setClass(
> "test",
> representation(f="numeric", t="numeric")
> )
> setMethod("setData","test",
> function(this,fcn,k){
> this at t<- as.numeric(seq(-k,k))/(2*k+1)
> this at f<- sapply(t,FUN=fcn)
> }
> )
>
> #-----------------------------------------------------------
> tst<- new("test")
> fcn<- function(u){ sin(2*pi*u) }
> setData(tst,fcn,100)
> tst at t # it's still empty because of pass by value
>
>
> How can this be handled?
>
>
> Many thanks,
>
> Michael
>
> [[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.
>
>