Getting ready

In R, generally if you write the object name into the console, then it automatically prints its internal elements. For example, take a look at the following code snippet:

    x <- c(13, 21, 19, 18, 21, 16, 21, 24, 17, 18, 12, 18, 29, 17, 18,   
11, 13, 20, 25, 18, 15, 19, 21, 21, 7, 12, 23, 31, 16, 19, 23, 15,
25, 19, 15, 25, 25, 16, 29, 15, 26, 29, 23, 24, 20, 19, 14, 27, 22,
26)

class(x) <- "robustSummary"

Now, if you just type x in the R console, it will print all the items in the object x and its class name. In R, when you call an object into the R console, it actually looks for a method for the class of the given object. If there is no method available for that class, then it automatically calls print.default() and produces the output accordingly. In this example, calling the function print() is equivalent to calling print.default(), as shown in the following code:

    > print(x)
[1] 13 21 19 18 21 16 21 24 17 18 12 18 29 17 18 11 13 20 25 18 15
19 21 21 7 12 23 31 16
[30] 19 23 15 25 19 15 25 25 16 29 15 26 29 23 24 20 19 14 27 22 26
attr(,"class")
[1] "robustSummary"

> print.default(x)
[1] 13 21 19 18 21 16 21 24 17 18 12 18 29 17 18 11 13 20 25 18 15
19 21 21 7 12 23 31 16
[30] 19 23 15 25 19 15 25 25 16 29 15 26 29 23 24 20 19 14 27 22 26
attr(,"class")
[1] "robustSummary"

However, if you call it using the class name, then you will get the following error:

   print.robustSummary(x)
Error: could not find function "print.robustSummary"

The error states that there is no function or method for the class robustSummary. In this recipe, you will define a method for the generic function print() for the object class robustSummary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset