For simplicity, I will use the following sample code :)
I had defined a S4 class test
, and then as usual I adopted setMethod
to write generic function split
for class test
:
# define a S4 class
setClass(
Class="test",
representation=representation(
m = "matrix"
)
)
# write generic function 'split' for S4 class 'test'
setMethod(f = "split", signature = c("test"), function(x, f) {
split(x@m, f)
})
# call generic function for test
split(new("test", m=matrix(1:9,3)), c(1,2,3))
Run the code above and the R command line will give out message as follows:
Creating a generic function for ‘split’ from package ‘base’ in the global environment
and then the program output follows:
$`1`
[1] 1 4 7
$`2`
[1] 2 5 8
$`3`
[1] 3 6 9
It seems that output is correct. But my question is how to supress the message:
Creating a generic function for ‘split’ from package ‘base’ in the global environment
Thanks a lot :)
PS:
I found that replace the definition of method split
for S4 class test
with the form of how we realize S3 generic methods as follows will do get rid of that message:
split.test <- function(x, f) {
split(x@m, f)
}
However, I don't think it's a good idea to mix S3 and S4 :)