Search
Question: Using S4Vectors within a package
0
gravatar for my4
13 months ago by
my40
my40 wrote:

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.

ADD COMMENTlink modified 13 months ago by Martin Morgan ♦♦ 20k • written 13 months ago by my40
0
gravatar for Martin Morgan
13 months ago by
Martin Morgan ♦♦ 20k
United States
Martin Morgan ♦♦ 20k wrote:

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 COMMENTlink written 13 months ago by Martin Morgan ♦♦ 20k

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 REPLYlink written 13 months ago by Michael Lawrence9.8k

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 REPLYlink written 13 months ago by my40

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 REPLYlink written 13 months ago by my40

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 REPLYlink written 13 months ago by Michael Lawrence9.8k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.2.0
Traffic: 156 users visited in the last hour