R-什么是slot插槽?

来源:互联网 发布:godaddy域名邮箱设置 编辑:程序博客网 时间:2024/05/17 02:59


Slots are linked to S4 objects. A slot can be seen as a part, element or a "property" of an object. Say you have a car object, then you can have the slots "price", "number of doors", "type of engine", "mileage".
插槽Slots和S4对象类型相关。一个插槽可以被看作是一个部分,元素,或者对象的某个特性。比如你有一个对象是车,那么你就有这些插槽:“价格”,“门的数量”,“发动机类型”,“里程数”。

举例如下:
新建:
setClass("Car",representation=representation(   price = "numeric",   numberDoors="numeric",   typeEngine="character",   mileage="numeric"))aCar <- new("Car",price=20000,numberDoors=4,typeEngine="V6",mileage=143)> aCarAn object of class "Car"Slot "price":[1] 20000Slot "numberDoors":[1] 4Slot "typeEngine":[1] "V6"Slot "mileage":[1] 143
Here, price, numberDoors, typeEngine and mileage are slots of the S4 class "Car". This is a trivial example, in reality slots themselves can be again complex objects.

访问:
Slots can be accessed in numerous ways :
> aCar@price[1] 20000> slot(aCar,"typeEngine")[1] "V6"    
Slots:      The data contained in an object from an S4 class is defined      by the _slots_ in the class definition.      Each slot in an object is a component of the object; like      components (that is, elements) of a list, these may be      extracted and set, using the function slot()’ or more often      the operator "@"’.  However, they differ from list      components in important ways.  First, slots can only be      referred to by name, not by position, and there is no partial      matching of names as with list elements.
注意:@插槽本身也可以是dataframe,插槽位置用@,之后的dataframe元素用$.
比如:par(bg=x@colors$bg.col,col.axis=x@colors$fg.col, xaxs='r',las=2,fg=x@colors$fg.col)



0 0
原创粉丝点击