Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Using R objects and functions

R.M. Ripley

Department of Statistics
University of Oxford

2012/13

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 1 / 27


R objects Attributes

Attributes

We have met R objects with classes: e.g. Date

A class is an example of an attribute: another object (in this


case a character string) attached by name to the object

Other examples are the names of a vector, dimensions of a


matrix, levels of a factor.
Often useful when returning results from functions
e.g. result is basically a matrix but with a single extra value.
If returned as a matrix with an attribute, result can be used like a
matrix in arithmetic.
If returned as a list the matrix must first be extracted from the list.

All objects except NULL may have attributes.

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 2 / 27


R objects Attributes

Using attributes

Two functions for manipulating attributes:


attributes and attr

To retrieve all the attributes of an object:


attributes(obj)

To create a complete set of attributes for an object:


attributes(obj) <- value

To retrieve the value of the attribute named "attr1":


attr(obj,"attr1")

To set the value of the attribute named "attr1", which need not
exist already:
attr(obj,"attr1") <- value

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 3 / 27


R objects Attributes

Attributes: some words of caution

In general, can use any objects as attributes

But there are some special named attributes, for which the values
must be valid.

These are names, dimnames, class, dim, row.names,


comment and tsp

If using S4 classes (see later), do not use attributes.

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 4 / 27


R objects Object-oriented programming in R

Introduction to classes and methods

Functions such as print, plot, summary adapt their action


according to the type of object sent to them as their first argument.
Such functions are known as generic functions.
The type of object is determined by the class attribute.
methods(summary) will list all the different versions of the
summary function
methods(class="lm") will list all the generic functions
available for objects with class lm
e.g. if mylm <- lm(weight ~ feed, data=chickwts)
summary(mylm) uses summary.lm
summary(chickwts) uses summary.data.frame
Occasionally useful to call a particular generic function directly:
e.g. summary.lm on an aov object.

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 5 / 27


R objects Object-oriented programming in R

Example of aov and lm


npk.aov <- aov(yield ~ block + N*P*K, npk)
summary(npk.aov)
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.3 68.66 4.447 0.01594 *
N 1 189.3 189.28 12.259 0.00437 **
P 1 8.4 8.40 0.544 0.47490
K 1 95.2 95.20 6.166 0.02880 *
N:P 1 21.3 21.28 1.378 0.26317
N:K 1 33.1 33.14 2.146 0.16865
P:K 1 0.5 0.48 0.031 0.86275
Residuals 12 185.3 15.44
--
Signif. codes:0 *** 0.001 ** 0.01 * 0.05 . 0.1 ’ ’ 1

summary.lm(npk.aov)
R.M. Ripley (University of Oxford) R objects and functions 2012/2013 6 / 27
R objects Object-oriented programming in R

Example of aov and lm, summary.lm


Call:aov(formula = yield ~ block + N * P * K,data=npk)
Residuals:
Min 1Q Median 3Q Max
-5.3000 -1.6833 0.1583 1.9979 4.4750
Coefficients: (1 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 54.8750 0.8021 68.415 < 2e-16 ***
block1 1.7125 1.3893 1.233 0.24131
block2 1.6792 0.8021 2.093 0.05822 .
block3 -1.8229 0.5672 -3.214 0.00744 **
block4 -1.0138 0.4393 -2.308 0.03965 *
block5 0.2950 0.3587 0.822 0.42689
N1 2.8083 0.8021 3.501 0.00437 **
P1 -0.5917 0.8021 -0.738 0.47490
K1 -1.9917 0.8021 -2.483 0.02880 *
N1:P1 -0.9417 0.8021 -1.174 0.26317
N1:K1 -1.1750 0.8021 -1.465 0.16865
P1:K1 0.1417 0.8021 0.177 0.86275
--
Signif. codes:0 *** 0.001 ** 0.01 * 0.05 . 0.1 ’ ’ 1
Residual standard error: 3.929 on 12 degrees of freedom
Multiple R-squared: 0.7886, Adjusted R-squared: 0.5948
F-statistic: 4.069 on 11 and 12 DF, p-value: 0.01156
R.M. Ripley (University of Oxford) R objects and functions 2012/2013 7 / 27
R objects Object-oriented programming in R

S3 and S4 classes

Two different systems of classes and methods in R


S3 Informal classes e.g. lm, summary.lm
i Simple to change the contents of the objects of the class, but
cannot assume the objects are all identical
ii Generic function used is determined by the type of a single
argument only, almost always the first
S4 Formal classes: only used in some packages e.g. Matrix
i Allow more control over the contents of objects of the class
ii Allow the generic function used to be determined by the type of
more than one argument
iii Recommended for advanced users developing extensive
software systems

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 8 / 27


R objects Object-oriented programming in R

A little more about S4 classes


Parts of an S3 object are components of a list: find their names by
names(object), access as object$part
Parts of an S4 object are known as slots.
Find their names by getSlots(class(object)) or
slotNames(object)
Access as object@part
An S4 matrix class
library(Matrix)
(I3 <- Matrix(diag(3)))# unit "diagonalMatrix"
3 x 3 diagonal matrix of class "ddiMatrix"
[,1] [,2] [,3]
[1,] 1 . .
[2,] . 1 .
[3,] . . 1

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 9 / 27


R objects Object-oriented programming in R

A little more about S4 classes

Slots
class(I3)
[1] "ddiMatrix"
attr(,"package")
[1] "Matrix"
getSlots("ddiMatrix")
diag Dim Dimnames x
"character" "integer" "list" "numeric"
slotNames(I3)
[1] "diag" "Dim" "Dimnames" "x"
I3@Dim
[1] 3 3

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 10 / 27


R objects Object-oriented programming in R

More about S3 classes

From now on in these lectures, a class is an informal one.

R has an inheritance mechanism:


Objects can have multiple classes. Objects created by glm have
their class attribute equal to the vector c("glm", "lm")
If there is no method defined for class "glm" but there is one for
class "lm", the one for "lm" will be used instead
If there is no method defined for either class, then the default
method (e.g. summary.default) will be used.
The function inherits(x, class) returns TRUE if the class
appears in the class attribute of x or is an implicit class of x (such
as matrix), FALSE otherwise

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 11 / 27


R objects Object-oriented programming in R

UseMethod

To create a new generic function, use UseMethod

UseMethod
myFn <- function(x, ...) UseMethod("myFn")
myFn.default <- function(x, ...)
print("in myFn.default")

myFn("test")
[1] in myFn.default

UseMethod does not return to the calling function.

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 12 / 27


R objects Object-oriented programming in R

NextMethod
Within a generic function, you can call the function for the next class in
the inheritance hierarchy by using NextMethod
NextMethod
myFn.character<-function(x, ...){
print(x)
NextMethod(x)
print(c(x, 2))
}
myFn("test")

[1] "test"
[1] "in myFn.default"
[1] "test" "2"

Note that NextMethod returns to the calling function.


R.M. Ripley (University of Oxford) R objects and functions 2012/2013 13 / 27
Evaluation and Scope Frames and Environments

Introduction

Where does R look for an object in order to use it to evaluate an


expression?

Where will R create a variable?

Fairly straightforward when using the command prompt,


but inside a function gets more complicated.

When looking for an object which in context must be a function,


R will ignore other objects with the same name

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 14 / 27


Evaluation and Scope Frames and Environments

Command prompt

At the command prompt,


uses the search path to locate objects
search() will list the order
Some objects, particularly functions, may be masked by others
earlier in the path
Use conflicts() to find out if this is happening
find() and exists() will help you locate objects

creates variables in (the frame of) your workspace


the workspace is also known as the global environment
e.g. lm <- lm or the result of fix(lm) will create a copy of the
variable lm in your workspace, which will mask the one in the
stats package

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 15 / 27


Evaluation and Scope Frames and Environments

Frames and Environments


Some confusion of terminology:

Definition
A frame is a collection of objects (symbol-value pairs).

Definition
An environment is used to mean either of
a frame
a frame plus its enclosure, a pointer to the enclosing environment
The enclosing environment could be thought of as the next frame in
the search path, but on the second definition would be that plus its
enclosure. Extending this gives a third possible meaning, essentially
equivalent to the search path from the current frame (which is not
necessarily the same as that from the command prompt).

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 16 / 27


Evaluation and Scope Frames and Environments

Functions, frames and environments

When a function is called, a new frame is created to store its local


variables.

This frame has an enclosure, known as the evaluation


environment

For functions which you create at the command prompt, this


enclosure is usually the workspace (or global environment)

For functions in packages, it is a name space

For functions which are defined inside other functions, it is usually


the environment created for the calling function.

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 17 / 27


Evaluation and Scope Frames and Environments

Evaluation environments

Functions not defined within other functions


f1 <- function()
{
print(x)
}
f2 <- function()
{
x <- 2
f1()
}
f2()
Error in print(x) : object "x" not found
x in f2() is not visible to f1()
Error unless there is an x lurking in your workspace

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 18 / 27


Evaluation and Scope Frames and Environments

Evaluation environments

Functions defined within other functions


s <- 5; x <- 6
f <- function(){
g <- function()cat("g: s=", s, "x=", x, "\n")
## g will get environment from f
j <- function(){
k <- function()
function()cat("anon in k: s=", s, "x=", x, "\n")
s <- 1
k()}
## j returns a function with environment from j
s <- 3; x <- 4 ## alter environment of f
cat("\nrun j\n");print(j())
cat("\nrun return val of j:\n");j()();
cat("run g\n"); g()}
R.M. Ripley (University of Oxford) R objects and functions 2012/2013 19 / 27
Evaluation and Scope Frames and Environments

Evaluation environments

Functions defined within other functions: Results


f()

run j
function()cat("anon in k: s=", s, "x=", x, "\n")
<environment: 0x01c6512c>

run return val of j:


anon in k: s= 1 x= 4
result of running g:
g: s= 3 x= 4

j()() gets s from j() and x from f()


g() gets both s and x from f()

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 20 / 27


Evaluation and Scope Frames and Environments

Name Spaces

Packages must use a special environment called a name space.


Only items exported from the name space are visible from outside.
This reduces the possibilities of name clashes
So the package stats may be in your search path, but some
objects will not be found.
For visible objects, can use e.g. stats::lm to refer specifically to
the object named lm in name space stats
If you want to find an object hidden in a name space, use
getAnywhere()
Can access non-visible objects using e.g. MASS:::boxcox.lm

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 21 / 27


Evaluation and Scope Frames and Environments

Search paths for functions

Differs according to whether the function is in a name space or not


Not in a name space: here it depends whether it was created
inside another function or not
no search path is the local frame created for the function and
then the normal search path as if from the command prompt.
(see first example)
yes search path is the local frame created for the function, then
the frame of the function where it was defined, then the frame
where that function was defined... until you reach a function
which was not created inside another function, when the
normal global search path is added.
In our second example, the function returned by j() had an
extra frame at the front of its search path relative to g().

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 22 / 27


Evaluation and Scope Frames and Environments

Search paths for functions in name spaces

For functions defined in name spaces, the search path is

1 first the local frame created for the function


2 then the frame of the name space where it was defined
3 then a frame containing any objects from other packages which
have been imported into the name space
4 then the name space of the package base
5 and finally the normal global search path

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 23 / 27


Evaluation and Scope Function arguments and return values

Function arguments
There is a special function argument "..." It is usually used to pass
on an unspecified list of arguments to another function:
... argument
plot.default includes a "..." argument:
plot.default <-
function (x,y=NULL,type="p",xlim=NULL,ylim=NULL,
log="",main=NULL,sub=NULL,xlab=NULL,ylab=NULL,
ann=par("ann"),axes=TRUE,frame.plot=axes,
panel.first=NULL,panel.last=NULL,asp=NA,...)

This is designed for extra graphics parameters.


plot.default passes them on to many of the routines
it calls:
plot.xy(xy, type, ...)

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 24 / 27


Evaluation and Scope Function arguments and return values

Use of "..." in generic functions

The "..." argument is often used in generic functions, since the


different methods often need different argument lists
... argument
myFn <- function(x, ...) UseMethod("myFn")

myFn.myclass1 <- function(x, brief=TRUE)

myFn.myclass2 <- function(x, plotit=TRUE)

R.M. Ripley (University of Oxford) R objects and functions 2012/2013 25 / 27


Evaluation and Scope Function arguments and return values

Function return values


All arguments in R are passed by value. If you change an argument within
a function the original object is not changed. To influence the calling
function you must return the changed value. A frequently used method :

Returning values
a <- NULL; a$start <- 1
myfn <- function(x){
x$start <- 2
return(x)
}
a <- myfn(a)
a$start
[1] 2

This approach allows one or more elements of a list to be updated,


although it necessitates copying the whole object.
R.M. Ripley (University of Oxford) R objects and functions 2012/2013 26 / 27
Exercises

Exercises 8
1 Write a print method for the object returned from the control
function in the project.
2 Create a new generic function called devfn. You are given
fv vector of fitted values (predicted means on log scale)
rfs vector of relapse free survival times
α the standard deviation on log scale for log normal,
the scale parameter for a log logistic.
goal vector of outcomes 0, (no event) or 1 (event)
The fitted values are the result of either a log normal model or a
log logistic model, as in survreg. The function devfn should
calculate the appropriate deviance (-2 log likelihood, the deviance
of the saturated model is taken to be 0), with the classes set to
"lnormal" or "llogistic" respectively.
Check by using the output from survreg. You will need to set the
class on the linear predictors (mymodel$linear.predictors)
appropriately.
R.M. Ripley (University of Oxford) R objects and functions 2012/2013 27 / 27

You might also like