William Dunlap
2011-Feb-07 19:56 UTC
[Rd] as.list(subclassed function) -> cannot coerce type 'closure' to vector of type 'list'
I was looking for all the glm-related 'family' functions in stats using the following predicate that returns TRUE for any function whose first argument is called "link". is.family <- function(object) is.function(object) && identical(names(as.list(object))[1], "link") It threw an error when applied to SSfol > is.family(SSfol) Error in as.vector(x, "list") : cannot coerce type 'closure' to vector of type 'list' but works when I unclass SSfol > is.family(unclass(SSfol)) [1] FALSE It looks like as.list fails on any function that is assigned a class: > as.list(function(x)x+1) $x [[2]] x + 1 > as.list(structure(function(x)x+1, class="unrecognized class name")) Error in as.vector(x, "list") : cannot coerce type 'closure' to vector of type 'list' Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
luke-tierney at uiowa.edu
2011-Feb-07 20:13 UTC
[Rd] as.list(subclassed function) -> cannot coerce type 'closure' to vector of type 'list'
For this application I would use names(formals(object)) to extract the formal arguments. luke On Mon, 7 Feb 2011, William Dunlap wrote:> I was looking for all the glm-related 'family' functions > in stats using the following predicate that returns TRUE > for any function whose first argument is called "link". > is.family <- function(object) is.function(object) && > identical(names(as.list(object))[1], "link") > > It threw an error when applied to SSfol > > is.family(SSfol) > Error in as.vector(x, "list") : > cannot coerce type 'closure' to vector of type 'list' > but works when I unclass SSfol > > is.family(unclass(SSfol)) > [1] FALSE > > It looks like as.list fails on any function that is assigned > a class: > > as.list(function(x)x+1) > $x > > > [[2]] > x + 1 > > > as.list(structure(function(x)x+1, class="unrecognized class name")) > Error in as.vector(x, "list") : > cannot coerce type 'closure' to vector of type 'list' > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Luke Tierney Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke at stat.uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Prof Brian Ripley
2011-Feb-07 21:08 UTC
[Rd] as.list(subclassed function) -> cannot coerce type 'closure' to vector of type 'list'
This is because there is an as.list.function, but your classed object does not inherit from 'function'. However, I think coercing to a list is S-like, and in R we have formals() and body(), and I think you want the former. Seems this was a late 2008 change: maybe the author can tell us why it was done? On Mon, 7 Feb 2011, William Dunlap wrote:> I was looking for all the glm-related 'family' functions > in stats using the following predicate that returns TRUE > for any function whose first argument is called "link". > is.family <- function(object) is.function(object) && > identical(names(as.list(object))[1], "link") > > It threw an error when applied to SSfol > > is.family(SSfol) > Error in as.vector(x, "list") : > cannot coerce type 'closure' to vector of type 'list' > but works when I unclass SSfol > > is.family(unclass(SSfol)) > [1] FALSE > > It looks like as.list fails on any function that is assigned > a class: > > as.list(function(x)x+1) > $x > > > [[2]] > x + 1 > > > as.list(structure(function(x)x+1, class="unrecognized class name")) > Error in as.vector(x, "list") : > cannot coerce type 'closure' to vector of type 'list' > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595