R is a programming language and software environment for statistical analysis and graphics. It originated from S, a statistical programming language developed in the 1970s. R was first released in 1993 and has since grown in popularity due to its ability to run on Linux, Windows and Mac operating systems. It allows users to contribute additional packages to extend its functionality. Getting help in R can be obtained through manuals, online searches, and mailing lists. R has a command line interface but various graphical user interfaces and integrated development environments are also available. Everything in R is an object that has a class and methods, with common functions to define classes, create objects, and extract object elements.
2. 2
INTRODUCTION
In this lecture, we will cover the following
topics before getting into the applications of
FRM concepts using R:
1. Origin and development
2. Getting help
3. Working with R
4. Classes, methods, and functions
5. The accompanying package FRAPO
3. 3
ORIGIN AND DEVELOPMENT
R is mainly a programming environment for
conducting statistical computations and producing
high-level graphics.
The website of the R project is:
http://www.r-project.org
The source code of the software is published as free
software under the terms of the GNU General
Public License (GPL);
http://www.gnu.org/licenses/gpl.html
7. 7
ORIGIN AND DEVELOPMENT
A major advancement of the S language took place in
1988, following which the system was rewritten in C
and functions for conducting statistical analysis were
added.
This was version 3 of the S language, referred to as
S3, and at that stage in the development of S, the R
story commences.
In August 1993, Ross Ihaka and Robert Gentleman,
both affiliated with the University of Auckland, New
Zealand, released a binary copy of R on Statlib.
8. 8
ORIGIN AND DEVELOPMENT
This first R binary was based on a Scheme
interpreter with an S-like syntax.
The growing interest in the project led to the need
for a powerful distribution channel for the software.
The master repository for the software (known as
the “Comprehensive R Archive Network”) is still
located in Vienna, with mirror servers spread all
over the globe.
9. 9
ORIGIN AND DEVELOPMENT
In order to keep pace with changes requested by users
and the fixing of bugs in a timely manner, a core group
of R developers was set up in mid-1997.
This established framework and infrastructure is
probably the reason why R has since made such
tremendous further progress.
Users can contribute packages to solve specific
problems or tasks and hence advances in statistical
methods and/or computations can be swiftly
disseminated.
10. 10
GETTING HELP
As already indicated in the previous paragraph, the first
resort for obtaining help is to read the R manuals.
These manuals cover different aspects of R and the one
mentioned above provides a useful introduction to R.
The following R manuals are available, and their titles
are self-explanatory.
These manuals can either be accessed from the
project’s website or invoked from an R session by
typing:
> help.start()
11. 11
GETTING HELP
Incidentally, in addition to these R manuals,
many complementary tutorials and related
material can be accessed from http://www.r-
project.org/otherdocs.html and an annotated
listing of more than 100 books on R is available
at http://www.r-project.org/doc/bib/R-books.html.
12. 12
GETTING HELP
To conduct an online search the function Rsite - Search() is available
which includes searches in the R mailing lists (mailing lists
will be covered as another means of getting help in due course).
> ## Online search facilities
> ?RSiteSearch
> RSiteSearch("Portfolio")
> ## The CRAN package sos
> ## 1. Installation
> install.package("sos")
> ## 2. Loading
> library(sos)
> ## 3. Getting an overview of the content
> help(package = sos)
> ## 4. Opening the package’s vignette
> vignette("sos")
> ## 5. Getting help on findFn
> ?findFn
> ## 6. Searching online for "Portfolio"
> findFn("Portfolio")
14. 14
WORKING WITH R
By default, R is provided with a command line
interface (CLI).
CLI is a very powerful tool that gives the user
direct control over calculations.
Some of the platform-independent GUIs and
IDEs are presented, acknowledging the fact that
R is shipped with a GUI on the Microsoft
Windows operating system only.
15. 15
WORKING WITH R
IDEs Lists are very important for R Software to run
properly.
Eclipse: Eclipse is a Java-based IDE and was first
designed as an IDE for this language.
Emacs/ESS GNU: Emacs is an extensible and
customizable text editor, which at its core is an
interpreter for the Emacs Lisp language.
JGR, Vim, RStudio
16. 16
CLASSES, METHODS & FUNCTIONS
Everything in R is an object, that a class is the definition of an
object, and that a method is a function by which a predefined
calculation/manipulation on the object is conducted.
x <- 1:5
> x
[1] 1 2 3 4 5
> class(x)
[1] "integer"
> xchar <- x
> class(xchar) <- "character"
> class(xchar)
[1] "character"
> xchar
[1] "1" "2" "3" "4" "5"
RUN
THESE
CODES
17. 17
CLASSES, METHODS & FUNCTIONS
The most commonly encountered ones are:
setClass() for defining a new S4 class;
new() for creating an object of a certain class;
setGeneric() for defining a function as generic;
setMethods() for defining a method for a certain class;
as() and setAs() for coercing an object of one class to
another class;
setValidity() and validObject() for validating the
appropriateness of an object belonging to a certain
classshowClass(), getClass(), showMethods(),
findMethods(), and getMethods() for displaying the
definition/availability of S4 classes and methods;
slot(), getSlots(), @ for extracting elements from an
object.