Re: [R] OOP and passing by value

From: Joris Meys <jorismeys_at_gmail.com>
Date: Wed, 09 Jun 2010 16:00:33 +0200

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 and the likes, there are indeed far better solutions than this one. I am -obviously- not an expert on those. And honestly, I personally don't like the idea of a function changing my object. So in conclusion, I shouldn't even have sent my last mail I guess...

Cheers
Joris

On Wed, Jun 9, 2010 at 3:12 PM, Henrik Bengtsson <hb_at_stat.berkeley.edu> wrote:
> 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 such as R.oo and proto for complete solutions.
>
> Using substitute(this) is not safe when doing nested calling, and
> assigning to a global environment is not something you really want to
> do.
>
> /Henrik
>
> On Wed, Jun 9, 2010 at 3:05 PM, Joris Meys <jorismeys_at_gmail.com> wrote:
>> 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))
>>    this_at_t <- as.numeric(seq(-k,k))/(2*k+1)
>>    this_at_f <- sapply(this_at_t,FUN=fcn)
>>    assign(Name,this,.GlobalEnv)
>>  }
>> )
>>
>> #-----------------------------------------------------------
>> tst <- new("test")
>> fcn <- function(u){ sin(2*pi*u) }
>> setData(tst,fcn,5)
>> tst_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:36 PM, Joris Meys <jorismeys_at_gmail.com> wrote:
>>> 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
>>>
>>
>>
>>
>> --
>> 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
>>
>> ______________________________________________
>> 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

______________________________________________
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.
Received on Wed 09 Jun 2010 - 14:02:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 14:20:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive