Skip to content

Instantly share code, notes, and snippets.

@nassimhaddad
Last active August 29, 2015 14:00
Show Gist options
  • Save nassimhaddad/11214573 to your computer and use it in GitHub Desktop.
Save nassimhaddad/11214573 to your computer and use it in GitHub Desktop.
some very minimal examples of object oriented programming with R
myfun <- function(){
out <- list()
out$x <- LETTERS
out$y <- "hello world"
class(out) <- "vtable"
out
}
print.vtable <- function(x){
cat("object of class vtable")
print(str(x$x))
}
"[.vtable" <- function(x, i){
x$x[i]
}
"$.vtable" <- function(x, i){
print("used $ operator")
x
}
z <- myfun()
print(z)
z[6]
z$y
setClass("vtable", representation(x = "character", y="character"))
x <- new("vtable", x=as.character(1:10), y = "heel")
setMethod("show", "vtable", function(object){
print("hello")
print(object@x)
})
x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment