[R] R and C - variable by reference
Duncan Murdoch
murdoch at stats.uwo.ca
Thu May 15 12:05:12 CEST 2008
Naira wrote:
> Hi all,
>
> I would like to know if it is possible in R to give a the reference of a
> variable in a function; in order to be able to change the variable in the
> function and to keep the change when the function ended.
> In other words, is it possible to code this following C code in R:
> void functionName(int &var){}
No, not directly. There are tricks to achieve the same thing (e.g. put
the variable in an environment and pass the environment instead), but
generally speaking you are better off using R as if it was a functional
language, i.e. avoiding side effects in your functions. Use
x <- modify(x)
rather than expecting modify() to make changes to the things passed to it.
Duncan Murdoch
More information about the R-help
mailing list