88

Does anyone know what a slot is in R?

I did not find the explanation of its meaning. I get a recursive definition: "Slot function returns or set information about the individual slots of an objects"

Help would be appreciated, Thanks - Alley

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
user573347
  • 975
  • 1
  • 7
  • 11

4 Answers4

103

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".

Internally, that is represented a list. An example :

setClass("Car",representation=representation(
   price = "numeric",
   numberDoors="numeric",
   typeEngine="character",
   mileage="numeric"
))
aCar <- new("Car",price=20000,numberDoors=4,typeEngine="V6",mileage=143)

> aCar
An object of class "Car"
Slot "price":
[1] 20000

Slot "numberDoors":
[1] 4

Slot "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"    

or through the construction of a specific method (see extra documentation).

For more on S4 programming see this question. If the concept still sounds vague to you, a general introduction in Object Oriented Programming could help.

PS: Mind the difference with dataframes and lists, where you use $ to access named variables/elements.

Community
  • 1
  • 1
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • 4
    +1 nice answer Joris. You might want to add an example of `slot(aCar, "price")` just as another usage, esp as the op was looking at the `slot()` function – Gavin Simpson Jan 17 '11 at 14:32
  • 10
    And to get all the slots of a class, there is `getSlots()`, or `slotNames()` for their names. – Laurent Jan 18 '11 at 22:57
  • So, I wonder how is this different from what we call "attributions" or "properties" in other programming languages? – C.K. Sep 23 '21 at 12:07
  • Is there an equivalent for ```$``` and ```[[]]```? So ```element(aCar, "typeEngine")``` instead of ```aCar$typeEngine``` ? – Samuel Saari Aug 18 '22 at 12:01
25

Just as names(variable) lists up all the $-accessible names of a complex variable, so too

slotNames(object) lists all the slots of an object.

Very handy to discover what goodies your fit-object contains for your viewing pleasure.

joran
  • 169,992
  • 32
  • 429
  • 468
tim
  • 3,559
  • 1
  • 33
  • 46
14

In addition to the resources @Joris points you to, plus his own answer, try reading ?Classes, which includes the following on slots:

 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.
      ....
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
4

No idea why R has to redefine everything. Most normal programming languages call them "properties" or "attributes".

Max
  • 1,135
  • 13
  • 15