Nathaniel Smith wrote:
> On Tue, Feb 3, 2009 at 10:56 PM, laurent <lgaut...@gmail.com> wrote:
>> On Tue, 2009-02-03 at 13:30 -0800, Nathaniel Smith wrote:
>>> why does EnvironmentSexp_ass_subscript (==
>>> rpy2.rinterface.SexpEnvironment.__setitem__) copy the value it is
>>> storing?
>>>
>>>   PROTECT(sexp_copy = Rf_duplicate(sexp));
>>>   Rf_defineVar(sym, sexp_copy, rho_R);
>> That's from the early development days, when I was getting very frequent
>> segfaults. That was to be on the safe side since R itself is often doing
>> a lot of copies; a second look at that (and try to minimize the number
>> of copies) is on the ever growing to-do list...
> 
> Fair enough.
> 
>> # rinterface
>> import rpy2.rinterface as ri
>> ri.initr()
>>
>> x = ri.FloatSexpVector([1,2,3,4])
>> x = ri.globalEnv.get("dim<-")(x, ri.IntSexpVector([2, 2]))
> 
> Right... dim<- is copying the vector and then returning the modified
> copy. I want dim<- to mutate the vector in place, like it does when
> you call it in R :-).

In R,

dim(m) <- c(2,2)

can also be written

m <- get("dim<-", m, c(2,2))

Example:
 > m <- 1:4
 > get("dim<-")(m, c(2,2))
      [,1] [,2]
[1,]    1    3
[2,]    2    4
 >

In fact R is not always making copies because of optimization tricks at 
the C level.

> Since I sent that message, though, I have learned a bit more about the
> quirky stuff R's runtime does, and am no longer convinced this is
> possible in general... sigh.

You might have lost hope too quickly.

Take the x from previous email and try playing with the following:

print(x.do_slot("dim"))
x.do_slot_assign("dim", ro.IntVector([1, 4]))
print(x.do_slot("dim"))



L.




> -- Nathaniel


------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to