[R] source a specific function

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Mon Jun 18 23:00:36 CEST 2007


On 18-Jun-07 18:53:50, Duncan Murdoch wrote:
> 
> The exact syntax you list there won't work, but in any case, changing 
> the environment of a function in a package is a bad idea -- it may need
> to reference things from the namespace of the package.

Well, as I said before, "(assuming that you know about
interdependencies)"!

I tried Gabor's suggested syntax as follows, bearing in mind that
mvrnorm in MASS is pure R code calling only "base" functions:

library(MASS)
environment(mvrnorm) <- .GlobalEnv
mu=c(0,0)
V<-matrix(c(1.0,0.5,0.5,1.0),ncol=2)
detach()
ls()
[1] "%.+%"    "%+.%"    "mu"      "mvrnorm" "V"      

mvrnorm(10,mu,V)
             [,1]       [,2]
 [1,] -1.80466069 -1.8229928
 [2,]  0.05565147 -1.6279434
 [3,] -0.28505572 -0.8927696
 [4,] -0.48919795  0.0750501
 [5,] -0.08437832  0.1349296
 [6,]  2.17399713  1.2881640
 [7,]  1.59934824  1.3784665
 [8,]  0.30555420  0.3835743
 [9,]  0.11120527 -0.7287910
[10,] -0.77281783 -1.2265502

so that one seems to have worked.

However, I'm not sure about the space used by MASS "going away"
after detach():

Starting R from scratch:

Type 'q()' to quit R.

> gc()
         used (Mb) gc trigger (Mb)
Ncells 412589 11.1     597831   16
Vcells 102417  0.8     786432    6

> library(MASS)
> gc()
         used (Mb) gc trigger (Mb)
Ncells 459471 12.3     667722 17.9
Vcells 111187  0.9     786432  6.0

> environment(mvrnorm) <- .GlobalEnv
> detach()
> gc()
         used (Mb) gc trigger (Mb)
Ncells 459297 12.3     741108 19.8
Vcells 111105  0.9     786432  6.0
> gc()
         used (Mb) gc trigger (Mb)
Ncells 459304 12.3     818163 21.9
Vcells 111118  0.9     786432  6.0

so only about 170KB of the 2.2MB used by MASS has been recovered
after detach(). Or am I looking at the wrong indicator of space used?

On the other hand, if I first extract the code 9f mvrnorm()
which is a few lines of pure R, I get:

1. Start R from scratch:
gc()
         used (Mb) gc trigger (Mb)
Ncells 412589 11.1     597831   16
Vcells 102417  0.8     786432    6

2. Paste in the code for mvrnorm, and then:
gc()
         used (Mb) gc trigger (Mb)
Ncells 412844 11.1     667722 17.9
Vcells 102591  0.8     786432  6.0

so mrvnorm on its own is only taking up about

 (412844-412589) + (102591-102417) = 429KB

Hence I wonder whether the space first occupied by MASS (2.2MB)
apart from mvrnorm gets freed up at all?

Thanks for the comments.
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Jun-07                                       Time: 22:00:33
------------------------------ XFMail ------------------------------



More information about the R-help mailing list