public
Created

Example of object oriented programming in R

  • Download Gist
EmailClass.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#' Constructor
EmailClass <- function(name, email) {
nc = list(
name = name,
email = email,
get = function(x) nc[[x]],
set = function(x, value) nc[[x]] <<- value,
props = list(),
history = list(),
getHistory = function() return(nc$history),
getNumMessagesSent = function() return(length(nc$history))
)
#Add a few more methods
nc$sendMail = function(to) {
cat(paste("Sending mail to", to, 'from', nc$email))
h <- nc$history
h[[(length(h)+1)]] <- list(to=to, timestamp=Sys.time())
assign('history', h, envir=nc)
}
nc$addProp = function(name, value) {
p <- nc$props
p[[name]] <- value
assign('props', p, envir=nc)
}
nc <- list2env(nc)
class(nc) <- "EmailClass"
return(nc)
}
 
#' Define S3 generic method for the print function.
print.EmailClass <- function(x) {
if(class(x) != "EmailClass") stop();
cat(paste(x$get("name"), "'s email address is ", x$get("email"), sep=''))
}
 
if(FALSE) { #Test code that won't be run when sourcing this file
test <- EmailClass(name="Jason", "jason@bryer.org")
test$addProp('hello', 'world')
test$props
test
class(test)
str(test)
test$get("name")
test$get("email")
test$set("name", "Heather")
test$get("name")
test
test$sendMail("jbryer@excelsior.edu")
test$getHistory()
test$sendMail("test@domain.edu")
test$getNumMessagesSent()
test2 <- EmailClass("Nobody", "dontemailme@nowhere.com")
test2
test2$props
test2$getHistory()
test2$sendMail('nobody@exclesior.edu')
}

I'm still trying to get my head around the best way to do OO in R, and this looks fairly simple. My question is - what if you want to want to generate a sequence of objects of a given class? Say, instead of emails, the object is a an animal in a simulation. You want to generate hundreds of animals to start with, and have the ability to add new animals to the population, or remove them. From what I've been able to figure out, you can't create sequential lists of "environments".

>f=list()
>f[1]=EmailClass(name="Jason", "jason@bryer.org")
Error in f[1] = EmailClass(name = "Jason", "jason@bryer.org") : 
  environments cannot be coerced to other types

I think the problem is that you need to use '[[' to assign into the list.

f = list()
f[[1]] = EmailClass(name="Jason", "jason@bryer.org")

-Jason

On Mon, May 28, 2012 at 11:57 PM, ozjimbob
reply@reply.github.com
wrote:

I'm still trying to get my head around the best way to do OO in R, and this looks fairly simple.  My question is - what if you want to want to generate a sequence of objects of a given class?  Say, instead of emails, the object is a an animal in a simulation.  You want to generate hundreds of animals to start with, and have the ability to add new animals to the population, or remove them.  From what I've been able to figure out, you can't create sequential lists of "environments".

>f=list()
>f[1]=EmailClass(name="Jason", "jason@bryer.org")
Error in f[1] = EmailClass(name = "Jason", "jason@bryer.org") :
 environments cannot be coerced to other types

Reply to this email directly or view it on GitHub:
https://gist.github.com/1647595

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.