我如何查看现有课程 [英] How do I see existing classes

查看:61
本文介绍了我如何查看现有课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 setClass 函数来定义几个新类.但是这些类不会出现在我的 Rstudio 环境中.如何查看所有存在的类?

I have used the setClass function to define several new classes. But these classes don't appear in my Rstudio environment. How do I see all the classes that exist?

这是一个例子:

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

这个类现在存在于某个地方,我们可以这样做

The class now exists somewhere, as we can do

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

Slots:

Name:   absolute      item
Class: character      list

并制作该类的对象:

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

Slot "item":
list()

然而,我仍然没有在任何地方看到课程.BondedDust 的回答表明,如果将它们分配给对象,则只能看到这些类.

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

那么有没有办法甚至看到 R 自带的默认类?

So is there no way to even see the default classes R comes with?

推荐答案

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

"当定义一个类时,会存储一个包含该类信息的对象.该对象,称为定义类的元数据,不存储在类的名称下(以允许程序员编写生成函数该名称),但使用特殊构造的名称.要检查类定义,请调用 getClass.元数据对象中的信息包括:

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

setClass 帮助页面,它存储在创建它的环境中(默认情况下)或使用where"参数指定的:

From the setClass help page, it's stored in the environment where it is created (by default) or in the specified with the "where" argument:

"创建一个类定义,指定表示(槽)和/或包含在这个(超类)中的类,以及其他可选的细节.作为一个副作用,类定义存储在指定的环境中.生成器函数作为 setClass() 的值返回,如果类不是虚拟的,则适用于从类创建对象."

"Create a class definition, specifying the representation (the slots) and/or the classes contained in this one (the superclasses), plus other optional details. As a side effect, the class definition is stored in the specified environment. A generator function is returned as the value of setClass(), suitable for creating objects from the class if the class is not virtual."

在控制台运行 setClass 调用后,您将在全局环境中获得一个具有该名称的对象:

After running a setClass call at the console you get an object in the global environment by that 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"

您的问题最初是关于 S4 类的,即使用 setClass 创建的类.您根本不清楚您想找到 S3 以及可能称为默认或隐式类的内容.它们的管理方式不同.如果您想查看 print 函数存在的所有类,只需键入:

Your question originally asked about S4 classes, i.e. the ones created with setClass.. It wasn't at all clear that you wanted to find S3 and what might be called default or implicit classes. They are managed in a different manner. If you want to see all the classes that exist for the print function, 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

另请阅读 ?methods 的帮助页面.这些都是基于 class 属性分派的.对于类,例如 'numeric'、integercharacter 或 'list' 是隐式的但没有存储在对象 class 中 - 属性你只需需要知道它们是内置在原始 S 语言中的.S3 调度机制实际上是在很早以前就固定在核心 S 机制上.当 S3 被新 S 语言"描述时,它是语言的一部分.我目前看到您仍然可以在亚马逊获得二手副本:

Also read the help page for ?methods. Those are each dispatched on the basis of the class attribute. For classes, such as 'numeric', integer, character or 'list' that are implicit but not stored in object class-attributes youyou simply need to know that they were built into the original S language. The S3 dispatch mechanism was actually bolted on to that core S mechanism back in the dawn of time. S3 was part of the language when it was described by "New S Language". I currently see that you can still get used copies at 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 accessible along the search path:

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

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

所以在我的机器上,有超过 10% 的可用函数是 print 方法.

So on my machine a bit more than 10% of the available functions are print methods.

这篇关于我如何查看现有课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆