Using S4Vectors within a package
1
0
Entering edit mode
my4 • 0
@my4-10129
Last seen 3.9 years ago

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.

Package s4vectors • 930 views
ADD COMMENT
0
Entering edit mode
@martin-morgan-1513
Last seen 11 weeks ago
United States

Really a question for the bioc-devel mailing list, but you need to figure out what functions, methods, and generics you're using

> Rle
standardGeneric for "Rle" defined from package "S4Vectors"

function (values = logical(0), lengths = integer(0)) 
standardGeneric("Rle")
<environment: 0x4e3c080>
Methods may be defined for arguments: values
Use  showMethods("Rle")  for currently available ones.
> methods(table)
[1] table,ANY-method       table,DataTable-method table,Rle-method      
[4] table,Vector-method   
see '?methods' for accessing help and source code
> environment(getMethod("table", "Rle"))
<environment: namespace:S4Vectors>

 

and add those to your NAMESPACE, e.g.,

importFrom(S4Vectors, Rle, table)

Some would suggest the simpler

import(S4Vectors)

which is easier to maintain but can lead to conflicts between identical symbols defined in different packages.

ADD COMMENT
0
Entering edit mode

This is the right answer, but in principle the code should still work, just not as efficiently. The factor() function uses typeof() in a strange piece of logic, breaking the abstraction. I don't know why factor() doesn't just coerce exclude to character, since match() will do that anyway?

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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...

ADD REPLY
0
Entering edit mode

It's a good idea to just import(BiocGenerics). In principle, you should also import(S4Vectors) or at least importMethodsFrom(S4Vectors, table) (along with importFrom(S4Vectors, Rle)).

 

ADD REPLY

Login before adding your answer.

Traffic: 452 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6