Michael Lawrence
2011-Feb-09 13:54 UTC
[Rd] subassignment does not always duplicate object from active binding
Hi guys,
I think there might be an issue with the NAMED value on the object returned
by an active binding. For example, with the latest R devel,
env <- new.env(parent=emptyenv())
makeBinding <- function(data) {
force(data)
function(new) {
if (missing(new))
data
else {
print(data)
print(new)
data <<- new
}
}
}
makeActiveBinding("a", makeBinding(1), env)
env$a <- 2
# [1] 1
# [2] 2
env$a[1] <- 3
# [1] 3
# [1] 3
## but everything is alright if we add another name
b <- env$a
env$a[1] <- 4
# [1] 3
# [1] 4
This naive hack seems to have fixed the above case:
--- src/main/envir.c (revision 54284)
+++ src/main/envir.c (working copy)
@@ -142,6 +142,9 @@
SEXP expr = LCONS(fun, R_NilValue);
PROTECT(expr);
expr = eval(expr, R_GlobalEnv);
+ if (NAMED(expr) == 1) {
+ SET_NAMED(expr, 2);
+ }
UNPROTECT(1);
return expr;
}
Thanks,
Michael
[[alternative HTML version deleted]]