Carlos Kiser Caceres Carlos Kiser Caceres - 6 days ago 6
R Question

Returning result of a list of functions using Return()

First, my English is not so correct, so I apologize in advance for any grammar mistakes/errors in my question.

I have this general function:

.my_fun <- function(x,y,...){
<body>
return(
list(
fun1 <- function(x){
<bodyfun1>
},
fun2 <- function(y){
<bodyfun2>
}, ....
)
)
}


to be used as auxiliary of:

.my_funfun <- .my_fun(x,y,...)


and use ".my_funfun" as a code starter given the args of "my_fun"

I have to return values on the given order and of course return the result of the functions included on the body of the list. Normally, I would process the functions separately, store the results in separate objects and put the values of those objects in a list, and return that list (simplest way to do it), but I'm required to keep the structure I showed before.

My question is: is there any way to solve the functions in the body of the list and return those values, keeping the given structure? Or is it impossible to use return that way? I have used return() as a premature function stopper and as a fancy printer for objects in control structures, but this usage is new to me.

If you need the specific code elements I can provide them.

Thanks in advance.

Answer

Ok, doing stuff around, I realized what my mistake on the code was. The thing is that when i call

myfun$fun1

I got no answer. The right way to make the list is

.my_fun <- function(x,y,...){
    <body>
    return(
        list(
            fun1 = function(x){
                    <bodyfun1>
                     },
            fun2 = function(y){
                    <bodyfun2>
                     }, ....
              )
        )
 }

So i can use the name s"fun1, fun2 (...) as names to call in axiliary function.s

Example:I want to make a linear congruent random generator, with the following code:

.rCongLin <- function(a, c, m, x0){
    v1 <- x0
    return(
            list(seed=function(xw){
                            v1 <- xw},
                    nxt=function(){
                            v2 <- ((a*v1)+c)%%m
                            v3 <- v2/m
                            v1<<-v2
                            return(v3)
                    },
                    shw=function(){
                            return(list(a=a,c=c,m=m,x0=x0))}
            )
    )
}

The instruction:

.rCL <- .rCongLin(51864,35186,4153,1515)

starts the number generation given the function:

rCL=function(n){
        replicate(n,.rCL$nxt())}
rCL(45)

where seed() restarts x0, nxt() calculates the numbers and shw() shows the internal state (values for calculation). rCL(n) returns n number of replicas.

The thing is that .rCL$x() (where x() is any fun of those mentioned) returns NOTHING if you use '<-' operator (change it so u have a demonstration of what im talking about), but it does return with '='.

The answer is that the functions work with '=' because names are ASSIGNED on the lists with '=' (u name things in lists with '=') and not with '<-'. Therefore, in order to make the funtions... "citable" in the body of the return(list(fun1, fun2...) you need to assign the names fun1, fun2... using '='.

It was a simple answer. Got confused in using '=' and '<-' because in the original code every assignment is done with '='. Its prone to confuse novel programmers like me :(

Thanks a lot for your help! Again Im sorry for any gramatical mistakes in this answer. I hope this helps more people.

Comments