Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping function names when stored in an object

Tags:

r

I am preparing a package where users have to store functions and their associated arguments in a list for later use. This is a typical example of how it works:

Preparation of the list containing function names and arguments

parameters <- list(a1 = list(fun = dnorm,
                             args = list(mean = 150, sd = 100)),
                   a2 = list(fun = dpois,
                             args = list(lambda = 40)))

Here is an example of how this parameters object is used in other functions:

x <- 1:100
myfunction <- function(x, var, parameters)
{
    results <- list(var = var,
                    y = do.call(parameters[[var]]$fun, 
                                c(list(x), parameters[[var]]$args)),
                    parameters = parameters)
    class(results) <- "customclass"
    return(results)
}
myfunction(x, "a1", parameters)

Now suppose I want to print the names of the functions stored in parameters in the output of myfunction.

print.customclass <- function(X)
{
   cat("Function name:", X$parameters[[X$var]]$fun)
}

This custom print will not work:

> myfunction(x, "a1", parameters)
Error in cat(list(...), file, sep, fill, labels, append) : 
argument 2 (type 'closure') cannot be handled by 'cat' 

I have also tried other suggested solutions found elsewhere, such as:

print.customclass <- function(X)
{
   cat("Function name:", deparse(quote(X$parameters[[X$var]]$fun)))
}

> myfunction(x, "a1", parameters)
Function name: X$parameters[[X$var]]$fun

According to Harlan in this other question, the name of the function is lost when stored in another R object. He suggested to name the list of function with the function names, e.g.:

parameters <- list(a1 = list(dnorm = dnorm,
                             args  = list(mean = 150, sd = 100)),
                   a2 = list(dpois = dpois,
                             args  = list(lambda = 40)))

However, I wanted to keep the same names (fun) for every element of my list.

My question therefore is

Can anybody think of a way to keep the function name, when the function is stored in another object?

Thank you very much for your help!

like image 333
Boris Leroy Avatar asked Oct 21 '25 23:10

Boris Leroy


1 Answers

You could use alists:

parameters <- list(a1 = alist(fun = dnorm,
                             args = list(mean = 150, sd = 100)),
                   a2 = alist(fun = dpois,
                             args = list(lambda = 40)))
#probably better to write a constructor method for parameters objects


x <- 1:100
myfunction <- function(x, var, parameters)
{ f <- function(x) do.call(fun, c(list(x), args))
  formals(f) <- c(formals(f), parameters[[var]]) 
  results <- list(var = var,
                  y = f(x),
                  parameters = parameters)
  class(results) <- "customclass"
  return(results)
}

print.customclass <- function(X)
{
  cat("Function name:", X$parameters[[X$var]]$fun)
}

myfunction(x, "a1", parameters)
#Function name: dnorm
like image 196
Roland Avatar answered Oct 23 '25 13:10

Roland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!