简体   繁体   中英

What is the difference between as.character() and as( ,“character”) in R

In the surface they both seem to be doing the same thing. But it seems to be the case that the latter as(,"character") is more powerful.

As an example consider the following:

library(rvest)

temp <- html("http://www.example.com/")
temp <- temp %>% html_node("div p")

str(temp)
#Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 

as.character(temp) 
#Error in as.vector(x, "character") 
#   cannot coerce type 'externalptr' to vector of type 'character'

Whereas as(temp, "character") gives

#[1] "<p>This domain is established to be used for illustrative examples in documents. You may use this\n    domain in examples without prior coordination or asking for permission.</p>"

as.character() is an S3 generic, whereas as() is a function defined in the methods package for S4 generics and methods.

The author of an S3 class has no reason to write an S4 coercion method, so for intance

> as.data.frame(matrix(integer()))
[1] V1
<0 rows> (or 0-length row.names)

but

> as(matrix(integer()), "data.frame")
Error in as(matrix(), "data.frame") : 
  no method or default for coercing "matrix" to "data.frame"

For S4 classes, one (ie, the package developer) can (and really should) write both S3 and S4 methods for coercion of particular classes; a common paradigm is

as.character.MyClass <- function(x, ...) {}
setAs("MyClass", "character",
      function(from) as.character.MyClass(from))

In your example, the author (of XML) has provided a setAs function without the S3 equivalent, so you get special treatment using as() , but default (ie, error) when using as.character() .

There is no general rule about which is 'more powerful'; it would not be at all surprising to find examples even in base R and the methods package where as.X and as(, "X") behave differently and even in a logically inconsistent way.

In the next release of R (3.2.0) you will be able to say

> methods(class=class(temp))
[1] [[          coerce      html_form   html_node   html_nodes  html_table 
[7] initialize  show        slotsFromS3
see '?methods' for accessing help and source code

where 'coerce' is an indication that there is an S4 method for as(temp, ...") . The actual methods are

> x = methods(class=class(temp))
There were 18 warnings (use warnings() to see them)
> attr(x, "info")
                                                  visible from     generic isS4
coerce,oldClass,S3-method                            TRUE           coerce TRUE
coerce,XMLAbstractDocument,XMLAbstractNode-method    TRUE  XML      coerce TRUE
coerce,XMLDocument,XMLInternalDocument-method        TRUE  XML      coerce TRUE
coerce,XMLInternalDocument,character-method          TRUE  XML      coerce TRUE
coerce,XMLInternalDocument,XMLHashTree-method        TRUE  XML      coerce TRUE
coerce,XMLInternalDocument,XMLInternalNode-method    TRUE  XML      coerce TRUE
coerce,XMLInternalNode,XMLInternalDocument-method    TRUE  XML      coerce TRUE
initialize,oldClass-method                           TRUE       initialize TRUE
show,oldClass-method                                 TRUE             show TRUE
slotsFromS3,oldClass-method                          TRUE      slotsFromS3 TRUE

On the other hand there is

> methods(class="matrix")
 [1] anyDuplicated as.data.frame as.raster     boxplot       coerce       
 [6] determinant   duplicated    edit          head          initialize   
[11] isSymmetric   Math          Math2         Ops           relist       
[16] subset        summary       tail          unique       
see '?methods' for accessing help and source code

where we see methods as.data.frame() and as.raster() available for coercing a matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM