Creating R packages, passing by reference and oo R.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating R packages, passing by reference and oo R.

Roger Gill
Dear All,

I would like some advice on creating R packages, passing by reference and oo R.

I have created a package that is neither elegant nor extensible and rather cumbersome (it works). I would like to re write the code to make the package distributable (should it be of interest) and easy to maintain.

The package is for Bayesian model determination via a reversible jump algorithm and has currently been written to minimise computational expense.

At every iteration of the MCMC I make a call to a likelihood function. Here, I pass by value a matrix containing the explanatory variables of the current model and this is quite costly if X is quite large. This matrix could change at every iteration (it's certainly proposed). Ideally I would pass by reference (a pointer in C, \@ in perl etc...) and allow the user to specify this function should they so desire. This is where I become a little stuck.

I have searched the lists, used google and not found a accepted solution. How do I pass by ref? What are my options? Can you point me towards some suitable reading? Are there any good examples? How have others overcome this problem.

Thanks in advance,

best wishes

Roger




______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating R packages, passing by reference and oo R.

Duncan Murdoch
On 05/04/2010 7:35 AM, Roger Gill wrote:

> Dear All,
>
> I would like some advice on creating R packages, passing by reference and oo R.
>
> I have created a package that is neither elegant nor extensible and rather cumbersome (it works). I would like to re write the code to make the package distributable (should it be of interest) and easy to maintain.
>
> The package is for Bayesian model determination via a reversible jump algorithm and has currently been written to minimise computational expense.
>
> At every iteration of the MCMC I make a call to a likelihood function. Here, I pass by value a matrix containing the explanatory variables of the current model and this is quite costly if X is quite large. This matrix could change at every iteration (it's certainly proposed). Ideally I would pass by reference (a pointer in C, \@ in perl etc...) and allow the user to specify this function should they so desire. This is where I become a little stuck.
>
> I have searched the lists, used google and not found a accepted solution. How do I pass by ref? What are my options? Can you point me towards some suitable reading? Are there any good examples? How have others overcome this problem.


R discourages passing by reference; it encourages a functional style of
programming.  You can do references by using an external pointer (so the
contents of the object are managed entirely in your C code), or by
putting the object into an environment, and passing the environment around.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating R packages, passing by reference and oo R.

Gabor Grothendieck
In reply to this post by Roger Gill
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"


On Mon, Apr 5, 2010 at 7:35 AM, Roger Gill <[hidden email]> wrote:

> Dear All,
>
> I would like some advice on creating R packages, passing by reference and oo R.
>
> I have created a package that is neither elegant nor extensible and rather cumbersome (it works). I would like to re write the code to make the package distributable (should it be of interest) and easy to maintain.
>
> The package is for Bayesian model determination via a reversible jump algorithm and has currently been written to minimise computational expense.
>
> At every iteration of the MCMC I make a call to a likelihood function. Here, I pass by value a matrix containing the explanatory variables of the current model and this is quite costly if X is quite large. This matrix could change at every iteration (it's certainly proposed). Ideally I would pass by reference (a pointer in C, \@ in perl etc...) and allow the user to specify this function should they so desire. This is where I become a little stuck.
>
> I have searched the lists, used google and not found a accepted solution. How do I pass by ref? What are my options? Can you point me towards some suitable reading? Are there any good examples? How have others overcome this problem.
>
> Thanks in advance,
>
> best wishes
>
> Roger
>
>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating R packages, passing by reference and oo R.

Sharpie
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