new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
24
Task/Classes/R/classes-2.r
Normal file
24
Task/Classes/R/classes-2.r
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
setClass("circle",
|
||||
representation(
|
||||
radius="numeric",
|
||||
centre="numeric"),
|
||||
prototype(
|
||||
radius=1,
|
||||
centre=c(0,0)))
|
||||
#Instantiate class with some arguments
|
||||
circS4 <- new("circle", radius=5.5)
|
||||
#Set other data slots (properties)
|
||||
circS4@centre <- c(3,4.2)
|
||||
|
||||
#Define a method
|
||||
setMethod("plot", #signature("circle"),
|
||||
signature(x="circle", y="missing"),
|
||||
function(x, ...)
|
||||
{
|
||||
t <- seq(0, 2*pi, length.out=200)
|
||||
#Note the use of @ instead of $
|
||||
plot(x@centre[1] + x@radius*cos(t),
|
||||
x@centre[2] + x@radius*sin(t),
|
||||
type="l", ...)
|
||||
})
|
||||
plot(circS4)
|
||||
13
Task/Classes/R/classes.r
Normal file
13
Task/Classes/R/classes.r
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#You define a class simply by setting the class attribute of an object
|
||||
circS3 <- list(radius=5.5, centre=c(3, 4.2))
|
||||
class(circS3) <- "circle"
|
||||
|
||||
#plot is a generic function, so we can define a class specific method by naming it plot.classname
|
||||
plot.circle <- function(x, ...)
|
||||
{
|
||||
t <- seq(0, 2*pi, length.out=200)
|
||||
plot(x$centre[1] + x$radius*cos(t),
|
||||
x$centre[2] + x$radius*sin(t),
|
||||
type="l", ...)
|
||||
}
|
||||
plot(circS3)
|
||||
Loading…
Add table
Add a link
Reference in a new issue