Created
September 2, 2015 22:31
-
-
Save robertzk/91d6aac5b46b86e80069 to your computer and use it in GitHub Desktop.
S3 subsetting overloading
This file contains hidden or 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
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