is there a way to dynamically coerce an object in R?
Background I am trying to create a function factory for summary which can return the specific method based on the class of the object passed. Particularly, this is for one of the questions in http://adv-r.had.co.nz/Functional-programming.html#lists-of-functions
Implement a summary function that works like base::summary(), but uses a list of functions. Modify the function so it returns a closure, making it possible to use it as a function factory.
I have tried a few variations all of which are incorrect/ incomplete, for ex.
object.list = list(df = data.frame(),fr=factor(), mx = matrix())
summary.new = function(y){
function(x,...){
UseMethod(summary,x)
}
}
summary.new.list = lapply(object.list, summary.new)
I am wondering if there is a way to dynamically coerce an object - something like as.() and use this to return the appropriate method from the generic object.
summary.new.list function
> summary.new.list
$df
function (x, ...)
{
UseMethod("summary", x)
}
<environment: 0x108b5edc>
$fr
function (x, ...)
{
UseMethod("summary", x)
}
<environment: 0x108b5de0>
$mx
function (x, ...)
{
UseMethod("summary", x)
}
<environment: 0x108b5ce4>
I want to call the function based on the object, for ex. for dataframes I want to call summary.new.list$df(data.frame(1:12,3,4))
. Though it works now as $df function is still generic - I wish to call the base summary.data.frame function from inside there instead of UseMethod
I don't exactly understand this example's intended purpose, but here's something to chew on:
You are basically reinventing class dispatch of functions. The question asked for "coercion" but the problem didn't seem to require any coercion, at least as I read it.