Type / origin of function R 'as'

The R S3 OO system is centered around generic functions that invoke methods depending on the class of the object on which the generic function is invoked. The bottom line is that a common function calls the appropriate method, unlike other OO programming languages ​​in which the method is defined inside the class.

For example, the mean function is a common function.

 isGeneric("mean") methods(mean) 

Will open

 TRUE [1] mean,ANY-method mean.Date mean.default mean.difftime [5] mean.IDate* mean,Matrix-method mean.POSIXct mean.POSIXlt [9] mean,sparseMatrix-method mean,sparseVector-method see '?methods' for accessing help and source code 

I studied the R bit and found the as function. I am confused by the fact that R says the function is not generic, but it still has methods.

 isGeneric("as") methods(as) TRUE [1] as.AAbin as.AAbin.character [3] as.alignment as.allPerms [5] as.array as.array.default [7] as.binary as.bitsplits [9] as.bitsplits.prop.part as.call ... 

At the end, a warning appears that as not common.

  Warning message: In .S3methods(generic.function, class, parent.frame()) : function 'as' appears not to be S3 generic; found functions that look like S3 methods 

Can someone explain to me what the as function is and how to connect to as.list , as.data.frame , etc.? R says that as.list is generic (where I am tempted to get a little angry at inconsistencies in R because I expect as.list be a method for the list object of as common function), Please help.

+8
object generics r r-s3 dispatch
source share
1 answer

as not common to S3, but note that you have TRUE. (I got FALSE.) This means that you downloaded a package that defines as as S4-generic. S3-generics work through the class manager, which uses the *.default function and the UseMethod function. The FALSE that I get means that for the universal as method, no method will be found that will search. One argument in favor of the lack of a common as is that calling such a function with only one data object will not indicate a “coercive purpose”. This means that the assignment must be embedded in the function name.

After declaring as be common (note the header line, which is a hint that this applies to S4 functions:

 setGeneric("as") # note that I didn't really even need to define any methods get('as') #--- output---- standardGeneric for "as" defined from package "methods" function (object, Class, strict = TRUE, ext = possibleExtends(thisClass, Class)) standardGeneric("as") <environment: 0x7fb1ba501740> Methods may be defined for arguments: object, Class, strict, ext Use showMethods("as") for currently available ones. 

If I reload R (and do not load libraries that call setGeneric for "as"), I get:

 get('as') #--- output --- function (object, Class, strict = TRUE, ext = possibleExtends(thisClass, Class)) { if (.identC(Class, "double")) Class <- "numeric" thisClass <- .class1(object) if (.identC(thisClass, Class) || .identC(Class, "ANY")) return(object) where <- .classEnv(thisClass, mustFind = FALSE) coerceFun <- getGeneric("coerce", where = where) coerceMethods <- .getMethodsTable(coerceFun, environment(coerceFun), inherited = TRUE) asMethod <- .quickCoerceSelect(thisClass, Class, coerceFun, coerceMethods, where) .... trimmed the rest of the code 

But you ask “why,” which is always a dangerous question when discussing language design, of course. I looked at the last chapter of statistical models in S, which is a cited link for most of the help pages related to sending S3, and finds no discussion of either coercion or the as function. There is an implicit definition of “S3 generic” requiring the use of UseMethod , but a mention of why as was excluded from this strategy. I think of two possibilities: to prevent any ambiguity of inheritance in the application of coercion, or it is a solution to effectiveness.

I probably should add that there is an S4 setAs function, and you can find all the S4-coercion functions with showMethods("coerce") .

+11
source share

All Articles