Professional Documents
Culture Documents
Using R Objects and Functions
Using R Objects and Functions
R.M. Ripley
Department of Statistics
University of Oxford
2012/13
Attributes
Using attributes
To set the value of the attribute named "attr1", which need not
exist already:
attr(obj,"attr1") <- value
But there are some special named attributes, for which the values
must be valid.
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
S3 and 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
UseMethod
UseMethod
myFn <- function(x, ...) UseMethod("myFn")
myFn.default <- function(x, ...)
print("in myFn.default")
myFn("test")
[1] in myFn.default
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"
Introduction
Command prompt
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).
Evaluation environments
Evaluation environments
Evaluation environments
run j
function()cat("anon in k: s=", s, "x=", x, "\n")
<environment: 0x01c6512c>
Name Spaces
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,...)
Returning values
a <- NULL; a$start <- 1
myfn <- function(x){
x$start <- 2
return(x)
}
a <- myfn(a)
a$start
[1] 2
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