Dan Murphy
2014-Jul-01 02:47 UTC
[R] Must array methods be defined for user defined classes?
Here's a simple example: setClass("foo", contains="numeric") x <- new("foo", 4) # Division by scalar works> x / 2An object of class "foo" [1] 2 # Division by array blows up y <- array(2, c(2, 2))> x / yError in getDataPart(c(2, 2, 2, 2)) : node stack overflow # Not sure I understand the error message, but in any case, based on the success of ...> x / as.numeric(y)[1] 2 2 2 2 # ... wrote a division method for arrays, which worked.> setMethod("/", signature = c("foo", "array"), function(e1, e2) e1 / as.numeric(e2))[1] "/"> x / y[1] 2 2 2 2 Is it always necessary to define "array" methods for arithmetic on user defined classes? Maybe the answer has to do with the error message I did not understand. Thanks, Dan Murphy> sessionInfo()R version 3.1.0 (2014-04-10) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_3.1.0>