I have encountered a curious problem when I try to set a method that was also defined in another package. An example package which demonstrates this problem can be found here.
The key is the typeof
method that I try to set. I use the setMethod
function for typeof
and it works when I build the package and try it on a trivial S4 class.
x <- new("A", x = 2)
typeof(x)
[1] "typeof was called"
[1] "myClassA"
However, if I load another package that previously also set typeof
(e.g. the bigmemory) prior to loading the s4test
package it continues to work fine if called directly.
library(bigmemory)
library(s4test)
x <- new("A", x = 2)
typeof(x)
[1] "typeof was called"
[1] "myClassA"
But, if the typeof
method is called internally by another method (e.g. nrow
see here) then it fails and only returns S4
nrow(x)
[1] "A"
attr(,"package")
[1] "s4test"
Function: typeof (package base)
x="ANY"
x="big.matrix"
x="myClass"
A connection with
description "stdout"
class "terminal"
mode "w"
text "text"
opened "opened"
can read "no"
can write "yes"
[1] "S4"