Created
July 23, 2013 03:22
-
-
Save PirateGrunt/6059626 to your computer and use it in GitHub Desktop.
Vectors of S4 classes with non-trivial slots
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
movie = c("Thunderball", "Goldfinger") | |
rating = c(4,5) | |
dfJoe = data.frame(movie = movie, rating = rating) | |
movie = c("Manhattan", "Interiors", "Radio Days", "Bananas") | |
rating = c(5, 4, 3, 5) | |
dfBob = data.frame(movie = movie, rating = rating) | |
setClass("BorrowedStuff", representation(stuff = "data.frame", from="character")) | |
JoesStuff = new("BorrowedStuff", from = "Joe", stuff = dfJoe) | |
BobsStuff = new("BorrowedStuff", from = "Bob", stuff = dfBob) | |
sillyFunction = function(x){ | |
x + 1 | |
} | |
sillyFunction(1) | |
sillyFunction(1:10) | |
whatStuff = new("BorrowedStuff", from = c("Joe", "Bob"), stuff = c(dfJoe, dfBob)) | |
whatStuff = new("BorrowedStuff", from = c("Joe", "Bob"), stuff = list(dfJoe, dfBob)) | |
setMethod("c", signature(x = "BorrowedStuff"), function(x, ...){ | |
elements = list(x, ...) | |
stuffList = list() | |
for (i in 1:length(elements)){ | |
stuffList[i] = new("BorrowedStuff", from = slot(elements[[i]], "from"), stuff = slot(elements[[i]], "stuff")) | |
} | |
class(stuffList) = "BorrowedStuff" | |
stuffList | |
}) | |
whatStuff = c(JoesStuff, BobsStuff) | |
whatStuff[[1]]@stuff | |
someStuff = whatStuff[[1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment