I have a stupidly simple function:
test = function(...) { table(...) }
If I just copy and paste this code into an R session and then run
test(Rle(1:10))
I get the expected result. However, I'm trying to include this function as part of a package, after installing my package
mypackage::test(Rle(1:10))
I get
Error in as.vector(x, mode) : invalid 'mode' argument
Obviously this is not my actual function, but shows what my problem is. I'm sure this is something to do with package namespace dependencies and if I put the right thing in DESCRIPTION and/or NAMESPACE it would work fine. But I can't work out what I need to put where to make it work.
This is the right answer, but in principle the code should still work, just not as efficiently. The
factor()
function usestypeof()
in a strange piece of logic, breaking the abstraction. I don't know whyfactor()
doesn't just coerceexclude
to character, sincematch()
will do that anyway?Sorry for using the wrong mailing list, I'll submit to the one you suggest in future.
I tried adding import(S4Vectors), but still got the same error message after installing and loading my package. That is,
mypackage::test(Rle(1:10))
Error in as.vector(x, mode) : invalid 'mode' argument
I then tried being more explicit by specifying importFrom(S4Vectors,Rle,table) in NAMESPACE and then package install failed with:
Error : object ‘table’ is not exported by 'namespace:S4Vectors'
All of this is incredibly confusing and makes me want to give up on making my code into a package.
To answer my own comment, I worked out that I needed to import the table function from BiocGenerics, not S4Vectors. so importFrom(BiocGenerics,table). I worked this out largely through trial and error. The fact that this wasn't obvious to Martin is writing his answer speaks volumes about how confusing this is...
It's a good idea to just
import(BiocGenerics)
. In principle, you should alsoimport(S4Vectors)
or at leastimportMethodsFrom(S4Vectors, table)
(along withimportFrom(S4Vectors, Rle)
).