Anyone know if the following can be achieved in R specifically S4
foo <- setClass("foo", contains = "matrix")
foo <- function(m = matrix(1:9, nrow = 3)) new("foo", m)
setMethod("dim", signature = "foo",
function(x) {
dd <- dim([email protected])
cat("foo dims: ")
return(dd)
}
)
# followed by
bar <- foo()
How or can it be achieved to distinguish between ...
dim(bar)
# which gives
foo dims: [1] 3 3
# and calling dim to assign the return value to a variable
# ie this call
bardims <- dim(bar)
# which does
foo dims:
# but I don't want it to produce any cat output to the console/screen
in the second case I would like to suppress the cat(....)
part of the original "dim,foo-method".
I would not mind defining something like setMethod('<-dim', 'foo', function(....
but I guess that is not available?
Info: I am using R-4.0.5 here
dim<-
rather than<-dim
. - JDLdim
just to see the dims and then being verbose in its output and a "silent" version of it, when I am assigning the value ofdim(object)
to a DIFFERENT variable - ie I am not 'assigning' a value to the dimensions of the object itself but using that value somewhere later on and don't want verbosity at the Console or anywhere else; MrFlick's answer achieves exactly that -> which is quite cool - GWD