D******n 发帖数: 2836 | 1 You cant pass parameters by reference to functions in R?
That sucks... | f***a 发帖数: 329 | 2 reference mechanism是啥?可能展开讲解下 | D******n 发帖数: 2836 | 3 basically it means if u pass an array to a function ,for example
func1(x)
x is an array or vector
the values of elements of x can be changed in the function, and the changes
remain after the function call is finished
example,
func1(x)
{ x[1]<- 0;
}
x<-c(1,2,3,4)
func1(x)
x
0,2,3,4
【在 f***a 的大作中提到】 : reference mechanism是啥?可能展开讲解下
| s*****n 发帖数: 2174 | 4 use <<-
global assignment.
> func1 <- function(x) {x[1]<<- 0}
> x<-c(1,2,3,4)
> func1(x)
> print(x)
[1] 0 2 3 4
> | D******n 发帖数: 2836 | 5 It is a workaround ,but it is not really pass by reference and it is so
clumsy
【在 s*****n 的大作中提到】 : use <<- : global assignment. : > func1 <- function(x) {x[1]<<- 0} : > x<-c(1,2,3,4) : > func1(x) : > print(x) : [1] 0 2 3 4 : >
| s*****n 发帖数: 2174 | 6 why do you need passing by reference?
for performance reason or something else?
Actually, when you pass a parameter into a function, the parameter is not
copied, unless it is modified, so what's the need for passing by reference? | D******n 发帖数: 2836 | 7 Because it needs to be modified.
when u pass a vector to a function, u want the function to modify its elemen
ts. It is a very common senario.Easy to implement in all other common langua
ges but not in R.
【在 s*****n 的大作中提到】 : why do you need passing by reference? : for performance reason or something else? : Actually, when you pass a parameter into a function, the parameter is not : copied, unless it is modified, so what's the need for passing by reference?
| q**j 发帖数: 10612 | 8 Passing by reference is only necessary when you need to speed things up and
save memory. Otherwise, you can always type a couple of extra lines to
achieve the same effect. as a script language, my understanding is that R
team has made the decision to trade speed for ease of coding etc. So it is
simply the consequence of this decision. if you are absolutely crazy about
speed, you might not want to use R at the first place. so there is nothing
you can really complain about. just my humble opioion. | D******n 发帖数: 2836 | 9 Actually i am raising this question because there is recursion in my functionon.
f<-function(x)
{ ...
x[1]<<-x[2];
f(x)
...
}
then if u type f(x), it might work
but f(a) it doesn't work
so i have to make a wrapper function outside it.anyway, this is not a problem now, cause i have this workaround. And maybe it is right, for most of the time, we dont necessarily need(or can avoid) pass by reference.But i just feel uncomfortable to type extra lines and to use an inconvenient angle to look a
【在 q**j 的大作中提到】 : Passing by reference is only necessary when you need to speed things up and : save memory. Otherwise, you can always type a couple of extra lines to : achieve the same effect. as a script language, my understanding is that R : team has made the decision to trade speed for ease of coding etc. So it is : simply the consequence of this decision. if you are absolutely crazy about : speed, you might not want to use R at the first place. so there is nothing : you can really complain about. just my humble opioion.
| s*****n 发帖数: 2174 | 10 If you just need to modify the arguments, you can use <<-.
This is not a reason to complain. This is not a workaround. In some
extent, passing by reference is a workaround of global assignment.
The only valid reason I can think about is the performance reason,
say passing parameter by reference probably saves time and space.
But it is not a problem for R either, because R does not copy the
parameter when it is passed into a function. However, you do need
to be careful when you do this
y <- myfun
【在 D******n 的大作中提到】 : Because it needs to be modified. : when u pass a vector to a function, u want the function to modify its elemen : ts. It is a very common senario.Easy to implement in all other common langua : ges but not in R.
| | | D******n 发帖数: 2836 | 11 Thanks for your input, but please take a look at post 12297.
It is really a workaround in that scenario.
【在 s*****n 的大作中提到】 : If you just need to modify the arguments, you can use <<-. : This is not a reason to complain. This is not a workaround. In some : extent, passing by reference is a workaround of global assignment. : The only valid reason I can think about is the performance reason, : say passing parameter by reference probably saves time and space. : But it is not a problem for R either, because R does not copy the : parameter when it is passed into a function. However, you do need : to be careful when you do this : y <- myfun
| s*****n 发帖数: 2174 | 12 I did read your post, but I do not understand why recursion causes a problem
here. What do you mean by f(x) works but f(a) doesn't?
Can you give a concrete example:
(1) What do you plan to do
(2) what is the expected (desired) output?
(3) What does R output due to the lack of passing by reference? | D******n 发帖数: 2836 | 13 Actually it is not limited to recurrsion, try the following example.
f<-function(x)
{ x[1]<<-1;
}
a<-c(0,1,2);
f(a);
cat(a,"\n");
x<-c(0,1,2);
f(x);
cat(x,"\n");
problem
【在 s*****n 的大作中提到】 : I did read your post, but I do not understand why recursion causes a problem : here. What do you mean by f(x) works but f(a) doesn't? : Can you give a concrete example: : (1) What do you plan to do : (2) what is the expected (desired) output? : (3) What does R output due to the lack of passing by reference?
| O*****y 发帖数: 222 | 14 Really great!
Songkun, could you tell me where do you learn these? I'm using R to process
some large data sets, knowing these knowledge can make my code more
efficient. Thanks!
【在 s*****n 的大作中提到】 : If you just need to modify the arguments, you can use <<-. : This is not a reason to complain. This is not a workaround. In some : extent, passing by reference is a workaround of global assignment. : The only valid reason I can think about is the performance reason, : say passing parameter by reference probably saves time and space. : But it is not a problem for R either, because R does not copy the : parameter when it is passed into a function. However, you do need : to be careful when you do this : y <- myfun
| s*****n 发帖数: 2174 | 15 This is relevant to some details about R's philosophy, lexical scope.
You can achieve it by using "substitute", for example:
g <- function(x){
tmp <- substitute(expression(x[1] <<- x[1]+1))
eval(eval(tmp))
}
x <- c(0,1,2); g(x)
print(x) # gives you 1,1,2
a <- c(0,1,2); g(a)
print(a) # gives you 1,1,2
But, it is pretty messy though.
【在 D******n 的大作中提到】 : Actually it is not limited to recurrsion, try the following example. : f<-function(x) : { x[1]<<-1; : } : a<-c(0,1,2); : f(a); : cat(a,"\n"); : x<-c(0,1,2); : f(x); : cat(x,"\n");
| D******n 发帖数: 2836 | 16 在 songkun (告别棒球场) 的大作中提到: 】
ya, thats what i meant.
You can do it but it looks ugly.
But i guess most of the time we dont come across this problem or we can copy
the varaible
g<-function(x)
{ a<-x;
a[1]<-1;
return(a);
}
a <- c(0,1,2),
a<-g(a);
cat(a);
but this is very slow. and yes, we can use your method.
Anyway, if we can use pass by reference in R, it would be much more straight
forward . Even perl has pass by reference, although as far as i know matlab
doesnt have pass by reference eithe | s*****n 发帖数: 2174 | 17 Yes, that's right.
Passing by reference are usually achieved by using pointers. R does not
allow pointers. Pointer operation is useful in generic programming but not
so much in statistical computation in comparison to being easy to use.
One interesting platform is Python, where everything is virtually passed by
reference, although no pointers are allowed. |
|