From luke at stat.uiowa.edu  Fri Jan 23 23:28:17 2009
From: luke at stat.uiowa.edu (luke at stat.uiowa.edu)
Date: Fri, 23 Jan 2009 16:28:17 -0600 (CST)
Subject: [Rd] overwriting '<-' and infinite recursions
In-Reply-To: <a0428ec90901231348u7a23facdl4c4cd26ec16fdc96@mail.gmail.com>
References: <a0428ec90901231241u561a5848re009b252bf7d1808@mail.gmail.com> 
	<alpine.DEB.1.00.0901231517430.6710@luke-inspiron>
	<a0428ec90901231348u7a23facdl4c4cd26ec16fdc96@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0901231621130.3628@itasca2.wildberry.org>

On Fri, 23 Jan 2009, Yi Zhang wrote:

> Thanks, Luke. I would certainly be very happy to see any non-intrusive
> solution. The problem I'm dealing with is: I'm creating a new class
> which references some external resource (file or database) and I want
> to make the reference counting work. An example (-> means references):
> Initially we have symbol a -> object oa of my class -> file fa.
> Suppose then R code "b<-a" is executed. With no modification to the
> '<-' function, R would make another symbol b and bind it to oa.
> Imagine next "b[1]<-0" is executed. Here comes the trouble: R only
> knows to duplicate oa to another ob; the modification will be done to
> the underlying shared file fa. Of course, I have necessary code for
> overriding "[<-" and make that modification. The whole thing is
> because I'm not aware of any sharing of the external resource at the R
> level. That's the motivation for me to
> overwrite '<-' in the first place. Any potential alternative solutions?

I'm still fuzzy about what you are trying to accomplish.  You have
some code, say openFA(), that produces the oa value with

    a <- openOA()

Then you could do

    b <- a

and then

    b[1] <- 0

or

    a[1] <- 0

Do you want these to have the same effect, do you want a[1] <- 0 to do
one thing if b <- a has happened and another if not, should one signal
an error, ...?

>From what you have written so far it would seem that messing with <-
would't really help as

    d <- list(a)

and

    f <- fuction(b) { ... }
    f(a)

would create similar issues that would not be addressed.

luke


>
> On Fri, Jan 23, 2009 at 4:25 PM,  <luke at stat.uiowa.edu> wrote:
>> While it _may_ be possible to make this work in current R (I don't
>> know if it is) this is a Really Bad Idea as it will affect every other
>> piece of R code run on the system. It also may not work at all in
>> future versions of R (assignment is sufficiently core functionality
>> that it may not be implemented via a function at all in some
>> circumstances).  Locked bindings are locked for a reason: we as
>> developers can assume that their values are what we intend them to be.
>> If those values are changed then that assumption fails and some things
>> may stop working.
>>
>> If you explain what your needs are someone on the list can probably
>> help you find a more effective and less intrusive way of doing it.
>>
>> luke
>>
>>
>> On Fri, 23 Jan 2009, Yi Zhang wrote:
>>
>>> Hello all,
>>>
>>> I'm having a problem when overwriting the '<-' function and was told
>>> I'd better post it here for help. The reason why I need to overwrite
>>> it is complicated and not easy to tell in a few words; but this seems
>>> the only clean option other than hacking R's core source code. My code
>>> looks like:
>>>
>>> # in .onLoad of a package; or if you want to test, put it in a function
>>> env <- as.environment('package:base')
>>> unlockBinding('<-',env)
>>> assign('<-', newAssign, envir=env)
>>> .Internal(lockBinding(as.name('<-'), env))
>>> #not using lockBinding directly because it calls '<-'
>>>
>>> It gave me this error: Error: evaluation nested too deeply: infinite
>>> recursion / options(expressions=)?
>>>
>>> Any suggestion is appreciated!
>>>
>>> --
>>> Yi
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>>
>>
>> --
>> Luke Tierney
>> Chair, Statistics and Actuarial Science
>> Ralph E. Wareham Professor of Mathematical Sciences
>> University of Iowa                  Phone:             319-335-3386
>> Department of Statistics and        Fax:               319-335-3017
>>   Actuarial Science
>> 241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
>> Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu
>>
>
>
>
>

-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



[Rd] overwriting '<-' and infinite recursions
luke at stat.uiowa.edu