How to see existing classes

I used a function setClass

to define several new classes. But these classes are not showing up in my Rstudio environment. How do I view all existing classes?

Here's an example:

setClass("geckoNss", representation(absolute = "character", item = "list"))

      

Now the class exists somewhere, how can we do

> getClass("geckoNss")
Class "geckoNss" [in ".GlobalEnv"]

Slots:

Name:   absolute      item
Class: character      list

      

and create objects of this class:

> new("geckoNss")
An object of class "geckoNss"
Slot "absolute":
character(0)

Slot "item":
list()

      

However, I still don't see the class anywhere. BondedDust's answer suggests that you can only see these classes if you assign them to an object.

So isn't there a way to see what default R classes have?

+2


source to share


1 answer


http://stat.ethz.ch/R-manual/R-devel/library/methods/html/Classes.html

"When a class is defined, an object is stored that contains information about that class. The object, known as the metadata that defines the class, is not stored under the class name (so programmers can write producing functions of that name), but under a specially constructed name. To examine the class definition , call getClass. Information in the metadata object includes: "

In the help page, setClass

it is stored in the environment in which it is created (by default) or specified in the "where" argument:

"Create a class definition by specifying the view (slots) and / or the classes it contains (superclasses) and other additional data. As a side effect, the class definition is stored in the specified environment. The generator function is returned as a setClass () value suitable for creating objects from a class if the class is not virtual. "

After running the call to setClass in the console, you get the object in the global environment using this name:

> track <- setClass("track",
+          slots = c(x="numeric", y="numeric"))
> ls()
 [1] "A"             "AE_by_factors" "B"            
 [4] "dat"           "dd"            "df"           
 [7] "final"         "hl"            "len"          
[10] "lm0"           "ml"            "ml0"          
[13] "peas2"         "realdata"      "temp"         
[16] "tolerance"     "track"         "TravelMode"   
[19] "vbin"          "vint"          "vnum"         
> track
class generator function for class "track" from package ‘.GlobalEnv’
function (...) 
new("track", ...)

> class(track)
#----------
[1] "classGeneratorFunction"
attr(,"package")
[1] "methods"

      

The question you originally asked was about S4 grades i.e. created with setClass

. It's not entirely clear what you wanted to find S3 and what might be called standard or implicit classes. They are governed differently. If you want to see all the classes that exist for a function print

, just type:



 methods(print) # I get 397 different methods at the moment. Each one implies an S3 class.
 # a variable number of values will appear depending on which packages ar loaded

      

Also read the man page ?methods

. Each one is sent based on an attribute class

. For classes such as 'numeric', integer

, character

or 'list', which implicitly but not stored in the object class

-attributes, you just need to know that they have been built in the original language in S. S3 dispatch mechanism was actually attached to the core S -the mechanism at dawn. S3 was part of the language when it was described as "New S Language". Currently I see that you can still use copies on Amazon:

New S Language Paperback – June 30, 1988
by R. A. Becker (Author), J. M. Chambers (Author), Allan R Wilks (Author)

      

There are other functions that allow you to look at the functions available along the search path:

> ?objects
> length(objects())
[1] 85

> length(apropos(what="", mode="function"))
[1] 3431

      

So, on my machine, over 10% of the available functions are methods print

.

+3


source







All Articles