10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix E 379editor and ESS extends the editor so that it can interface with many statistical softwarepackages. This setup works under Windows, Linux, and Mac OS X. This workingenvironment, or integrated development environment (IDE), provides an interactive shellwith TAB completion of command names and parentheses matching; a historymechanism; and integrated help pages. Additionally, functions and script files can beedited and evaluated in an R session with a simple command that can be typed orcontrolled by the mouse. The editing has built-in indenting, which allows you to identifyquickly the block of a program being edited. The only real drawback is the complexity oflearning Emacs.Many people prefer other tools <strong>for</strong> editing. A list of editors that integrate with Rappears on the R GUI web page at http://www.r-project.org/ under the “IDE/ScriptEditors” link.E.4 Object-oriented programming with RObject-oriented programming (OOP) is a more advanced topic than those covered in therest of this book. This section is included here as the freely available materials on OOPare not that accessible even <strong>for</strong> those with an understanding of OOP principles. The goalis to give a brief introduction to R’s implementation(s) <strong>for</strong> those who have some previousexperience with OOP.OOP is especially useful <strong>for</strong> large programming projects. It became popular in the1990s and is now ubiquitous. Most languages taught (e.g., Java, C++) have an OOPcomponent. OOP requires an initial investment in time to structure the data and methods,but this pays off in the long run. Often, <strong>for</strong> statistical exploration, it is programmingoverkill, but it should be considered when you are programming any larger projects.The nature of object-oriented programming in R has changed with the introduction ofthe base methods package and the add-on OOP package. At the time of writing there arefour types of OOP <strong>for</strong> R: the original S3 methods; the R. oo package,http://www.maths.1th.se/help/R/; the newer S4 methods; and the OOP package. The OOPpackage extends the S4 style; the R. oo package extends the S3 style.The notion of a class and associated methods are integral to OOP, and theirimplementation will be discussed below. But first, <strong>for</strong> OOP in R, an understanding ofmethod dispatch is helpful.E.4.1 Method dispatchMethod dispatch comes in when R decides which function to call. For example, when theR print() function is invoked, say with the command print (x), what happens? If thefunction invoked is a generic function, R first looks at what x (the first argument) is andthen, based on this, uses an appropriate idea of “print.” To determine what x is, Rconsiders its class attribute. Many R objects have a class attribute; others have an implicitclass attribute, such as data vectors. Their implicit class attribute is inherited from theirmode: <strong>for</strong> example, “character” or “numeric.” The class () function determines the classof an object. Once the class is determined, say it is “classname,” R looks <strong>for</strong> theappropriate function to call, depending on the type of function. As print() is an S3 generic

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!