Description Usage Arguments Details Value Note Author(s) References See Also Examples
This functions allow to access a referenced object. deref(ref)
returns the object, and deref(ref) <- value
assigns to the referenced object.
1 2 3 4 5 |
ref |
a reference as returned by |
value |
a value to be assigned to the reference |
deref
and deref<-
provide convenient access to objects in other environments/frames.
In fact they are wrappers to get
and assign
.
However, convenient does not neccessarily means efficient.
If performance is an issue, the direct use of new.env
, substitute
and eval
may give better results.
See the examples below.
deref
returns the referenced object.
"deref<-"
returns a reference to the modified object, see ref
.
Subsetted assignment appears to be inefficent in S+. Note the use of substitute
in the examples.
Jens Oehlschl<e4>gel
Writing R Extensions
ref
, as.ref
, get
, assign
, substitute
, eval
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | # Simple usage example
x <- cbind(1:5, 1:5) # take some object
rx <- as.ref(x) # wrap it into a reference
deref(rx) # read it through the reference
deref(rx) <- rbind(1:5, 1:5) # replace the object in the reference by another one
deref(rx)[1, ] # read part of the object
deref(rx)[1, ] <- 5:1 # replace part of the object
deref(rx) # see the change
cat("For performance test examples see ?deref\n")
## Not run:
## Performance test examples showing actually passing by reference
# define test matrix size of nmatrix by nmatrix
nmatrix <- 1000
# you might want to use less loops in S+
# you might want more in R versions before 1.8
nloop <- 10
# Performance test using ref
t1 <- function(){ # outer function
m <- matrix(nrow=nmatrix, ncol=nmatrix)
a <- as.ref(m)
t2(a)
m[1,1]
}
# subsetting deref is slower (by factor 75 slower since R 1.8 compared to previous versions
# , and much, much slower in S+) ...
t2 <- function(ref){
cat("timing", timing.wrapper(
for(i in 1:nloop)
deref(ref)[1,1] <- i
), "\n")
}
if (is.R())gc()
t1()
# ... than using substitute
t2 <- function(ref){
obj <- as.name(ref$name)
loc <- ref$loc
cat("timing", timing.wrapper(
for(i in 1:nloop)
eval(substitute(x[1,1] <- i, list(x=obj, i=i)), loc)
), "\n")
}
if (is.R())gc()
t1()
# Performance test using Object (R only)
# see Henrik Bengtsson package(oo)
Object <- function(){
this <- list(env.=new.env());
class(this) <- "Object";
this;
}
"$.Object" <- function(this, name){
get(name, envir=unclass(this)$env.);
}
"$<-.Object" <- function(this, name, value){
assign(name, value, envir=unclass(this)$env.);
this;
}
# outer function
t1 <- function(){
o <- Object()
o$m <- matrix(nrow=nmatrix, ncol=nmatrix)
t2(o)
o$m[1,1]
}
# subsetting o$m is slower ...
t2 <- function(o){
cat("timing", timing.wrapper(
for(i in 1:nloop)
o$m[1,1] <- i
), "\n")
}
if (is.R())gc()
t1()
# ... than using substitute
t2 <- function(o){
env <- unclass(o)$env.
cat("timing", timing.wrapper(
for(i in 1:nloop)
eval(substitute(m[1,1] <- i, list(i=i)), env)
), "\n")
}
if (is.R())gc()
t1()
## End(Not run)
|
dim(refdata) and dimnames(refdata) no longer allow parameter ref=TRUE, use dim(derefdata(refdata)), dimnames(derefdata(refdata)) instead
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 5 5
[1] 1 2 3 4 5
[,1] [,2] [,3] [,4] [,5]
[1,] 5 4 3 2 1
[2,] 1 2 3 4 5
For performance test examples see ?deref
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 257577 13.8 460000 24.6 350000 18.7
Vcells 456287 3.5 1023718 7.9 786431 6.0
timing 0.068
[1] 10
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 314691 16.9 592000 31.7 350000 18.7
Vcells 584356 4.5 2216018 17.0 2584528 19.8
timing 0.005
[1] 10
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 315554 16.9 592000 31.7 371417 19.9
Vcells 586264 4.5 2216018 17.0 2584528 19.8
timing 0.044
[1] 10
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 316024 16.9 592000 31.7 371417 19.9
Vcells 587355 4.5 2191376 16.8 2587558 19.8
timing 0.002
[1] 10
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.