[R] Creating R packages, passing by reference and oo R.

Sharpie chuck at sharpsteen.net
Mon Apr 5 16:32:44 CEST 2010



Gabor Grothendieck wrote:
> 
> Passing by value does not necessarily mean physical copying.  Check out
> this:
> 
>> x <- matrix(1:1000^2, 1000, 1000)
>> gc()
>          used (Mb) gc trigger (Mb) max used (Mb)
> Ncells 114520  3.1     350000  9.4   350000  9.4
> Vcells 577124  4.5    1901092 14.6  1577448 12.1
>> f <- function(x) { y <- max(x); print(gc()); y }
>> f(x)
>          used (Mb) gc trigger (Mb) max used (Mb)
> Ncells 114560  3.1     350000  9.4   350000  9.4
> Vcells 577133  4.5    1901092 14.6  1577448 12.1
> [1] 1000000
>> R.version.string
> [1] "R version 2.10.1 (2009-12-14)"
>> win.version()
> [1] "Windows Vista (build 6002) Service Pack 2"
> 

Yes, but you might pay a price in memory if you start altering values in x
within the function:

> x <- matrix(1:1000^2, 1000, 1000)
> gc(TRUE)
Garbage collection 5 = 1+0+4 (level 2) ... 
3.2 Mbytes of cons cells used (34%)
4.5 Mbytes of vectors used (30%)
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 119554  3.2     350000  9.4   350000  9.4
Vcells 578542  4.5    1902416 14.6  1581475 12.1

> f <- function(x) { x[100,100]<-2; print(gc(TRUE)) }
> f(x)
Garbage collection 7 = 1+0+6 (level 2) ... 
3.2 Mbytes of cons cells used (34%)
12.1 Mbytes of vectors used (60%)
          used (Mb) gc trigger (Mb) max used (Mb)
Ncells  119634  3.2     350000  9.4   350000  9.4
Vcells 1578573 12.1    2629025 20.1  2078567 15.9

> R.version.string
[1] "R version 2.10.1 (2009-12-14)"


-Charlie

-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://n4.nabble.com/Creating-R-packages-passing-by-reference-and-oo-R-tp1751525p1751682.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list