Skip to content

Instantly share code, notes, and snippets.

@PirateGrunt
Created July 23, 2013 03:22
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 PirateGrunt/6059626 to your computer and use it in GitHub Desktop.
Save PirateGrunt/6059626 to your computer and use it in GitHub Desktop.
Vectors of S4 classes with non-trivial slots
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