Thomas Mang <thomas.mang <at> fiwi.at> writes:

> I wonder if the following is possible in R:
> 
> Suppose a function takes an argument, and wants to modify this argument 
> so that the change is visible _at the call side_. It would be what is 
> for example known as pass-by-reference-to-non-const in C++.
> 
> test <- function(x)
> {
>   x <- 10
>   ...
>   return (somethingElse) # I do NOT want having to return x
> }
> 
> number = 5
> test(number)
> stopifnot(number == 10)
> 
> Is there some trick with scoping rules and maybe <<- to achieve this ?

Yes, it is possible. No, don't do it (coming from the c++ world, I understand
your temptation). Try to look for "assign" to see the attached flames.


http://r-project.markmail.org/search/?q=assign

Dieter

______________________________________________
R-help@r-project.org 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 via email to