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

Duncan Murdoch murdoch at stats.uwo.ca
Mon Apr 5 13:47:04 CEST 2010


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



More information about the R-help mailing list