Last active
August 29, 2015 14:00
-
-
Save nassimhaddad/11214573 to your computer and use it in GitHub Desktop.
some very minimal examples of object oriented programming with R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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