Skip to content

@PirateGrunt /gist:6059626
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.