Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R_inferno.pdf
Скачиваний:
6
Добавлен:
21.05.2015
Размер:
947.8 Кб
Скачать

Circle 7

Tripping on Object

Orientation

We came upon a sinner in the seventh Circle. He said, \Below my head is the place of those who took to simony before me|they are stu ed into the ssures of the stone." Indeed, with ames held to the soles of their feet.

It turns out that versions of S (of which R is a dialect) are color-coded by the cover of books written about them. The books are: the brown book, the blue book, the white book and the green book.

7.1S3 methods

S3 methods correspond to the white book.

The concept in R of attributes of an object allows an exceptionally rich set of data objects. S3 methods make the class attribute the driver of an object-oriented system. It is an optional system. Only if an object has a class attribute do S3 methods really come into e ect.

There are some functions that are generic. Examples include print, plot, summary. These functions look at the class attribute of their rst argument. If that argument does have a class attribute, then the generic function looks for a method of the generic function that matches the class of the argument. If such a match exists, then the method function is used. If there is no matching method or if the argument does not have a class, then the default method is used.

Let's get speci c. The lm (linear model) function returns an object of class

"lm". Among the methods for print are print.lm and print.default. The result of a call to lm is printed with print.lm. The result of 1:10 is printed with print.default.

S3 methods are simple and powerful. Objects are printed and plotted and summarized appropriately, with no e ort from the user. The user only needs to know print, plot and summary.

38

7.1. S3

CIRCLE 7. TRIPPING ON OBJECT ORIENTATION

There is a cost to the free lunch. That print is generic means that what you see is not what you get (sometimes). In the printing of an object you may see a number that you want|an R-squared for example|but don't know how to grab that number. If your mystery number is in obj, then there are a few ways to look for it:

print.default(obj)

print(unclass(obj))

str(obj)

The rst two print the object as if it had no class, the last prints an outline of the structure of the object. You can also do:

names(obj)

to see what components the object has|this can give you an overview of the object.

7.1.1generic functions

Once upon a time a new user was appropriately inquisitive and wanted to know how the median function worked. So, logically, the new user types the function name to see it:

> median

function (x, na.rm = FALSE) UseMethod("median") <environment: namespace:stats>

The new user then asks, \How can I nd the code for median?"

The answer is, \You have found the code for median." median is a generic function as evidenced by the appearance of UseMethod. What the new user meant to ask was, \How can I nd the default method for median?"

The most surere way of getting the method is to use getS3method:

getS3method('median', 'default')

7.1.2methods

The methods function lists the methods of a generic function. Alternatively given a class it returns the generic functions that have methods for the class. This statement needs a bit of quali cation:

It is listing what is currently attached in the session.

It is looking at names|it will list objects in the format of generic.class. It is reasonably smart, but it can be fooled into listing an object that is not really a method.

39

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]