I am trying to overload the subscript operator ("["
) for a custom class I've created. I am trying to figure out how to deal with the following issues.
a[x] = foo
vs foo = a[x]
foo = a[,x]
how can I identify the first parameter ?EDIT: My first point has received multiple answers. In the process I've figured out the answer to the second one. You can use the "missing" function to figure out which parameters are present.
Here is a sample code:
setMethod("[", signature(x="myClass"),
function(x, i, j, k, l) {
if (missing(i)) { i = 0 }
if (missing(j)) { j = 0 }
if (missing(k)) { k = 0 }
if (missing(l)) { l = 0 }
})
I have accepted an answer to this question as point number 3 is of least priority to me.
Discover the Generic so you know what you are aiming to implement :
getGeneric('[')
# standardGeneric for "[" defined from package "base"
# function (x, i, j, ..., drop = TRUE)
And :
getGeneric('[<-')
# standardGeneric for "[<-" defined from package "base"
# function (x, i, j, ..., value)
Then you implement it like this for example :
`[<-.foo` <-
function(x, i, j, value)
{
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With