[R] return() in nested functions
Mark Hempelmann
neo27 at rakers.de
Sat Jul 14 13:16:54 CEST 2007
Dear WizaRds,
After consulting different sources I am still unable to understand the
correct use of return() in nested functions. To illustrate the problem:
f <- function(x,y,type){
est1<-function(x,y){
z=x+y
out(x,y,z)}
est2<-function(x,y){
z=x*y
out(x,y,z)}
out<-function(x,y,z)
return(x,y,z)
if (type=="est1") est1(x,y)
if (type=="est2") est2(x,y)
}
test<-f(1,2,type="est1") # gives Null for test
However, without the second 'if' condition, it works properly:
Warning message:
multi-argument returns are deprecated in: return(x, y, z)
> test
$x
[1] 1
$y
[1] 2
$z
[1] 3
Basically, the function I am working on is of the above structure, be it
more complex. I would like f to return the results of function 'out' to
the user in the assigned variable, e.g. 'test'. i did consult try() and
tryCatch(), but it doesn't seem to be what I am looking for.
Thank you for your help and understanding
mark
More information about the R-help
mailing list