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!