Skip to content

Instantly share code, notes, and snippets.

@robertzk
Created September 2, 2015 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertzk/91d6aac5b46b86e80069 to your computer and use it in GitHub Desktop.
Save robertzk/91d6aac5b46b86e80069 to your computer and use it in GitHub Desktop.
S3 subsetting overloading
wrapped <- function(obj) {
structure(obj, class = "wrapped")
}
`[[.wrapped` <- function(obj, x) {
if (x == "predict") {
.subset2(obj, x)
} else {
.subset2(obj, "model")[[x]]
}
}
`$.wrapped` <- `[[.wrapped`
#> x <- wrapped(list(model = list(a=1,b=2), predict = function(...) print('predict')))
#> x$predict
#function(...) print('predict')
#> x$a
#[1] 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment