Skip to content
Prev 19979 / 398500 Next

What is the most efficient way to assign to PARTS of objects in other frames/environments?

Can please someone familiar with the R internals explain on the following:

PR#1434 from r-bugs clarifies that

  assign("a[1]", x, SomeOtherFrame)

or 

  assign("a$a", x, SomeOtherFrame)

will NOT assign to an object 'a' in the other frame BUT create a new object
called 'a[1]' resp. 'a$a'.

This leads to the following question: what is the most efficient way to
assign to PARTS of objects in other frames/environments?

Any way I know appears to have some problems associated:

# doing it locally involves several calls and probably copies the whole
object
local.copy <- get("a", SomeOtherFrame)
local.copy$a <- x
assign("a", local.copy, SomeOtherFrame)

# using eval fails because x might be unknown in the other frame
+   # x is locally defined
+   x <- 9
+   eval(parse(text="a$a <- x"), envir=globalenv())
+ }
Error in eval(expr, envir, enclos) : Object "x" not found


For me, it would highly make sense, if assign("a[1]") WOULD parse "a[1]" and
assign to a's part, as Martin expected it to do. The same applies to
get("a[1]", SomeOtherFrame). 

BTW: this issue dates back to 1997, when I tried (for S+Version3) to
implement a library "ref" that allows to access objects in other frames via
"references".

I would appreciate any clarification on this one.

Best


Jens Oehlschl?gel