0

In developing an R package, how do I import the as() method for an S4 class?

More details: I need to convert an adjacency matrix to a graphNEL object (from the graph package). Here is the code for doing so:

library("graph")
m <- rbind(
c(0, 0, 0, 0),
c(1, 0, 0, 0),
c(0, 1, 0, 0),
c(0, 0, 1, 0)
)
gr <- as(m, "graphNEL")

Unfortunately, this code fails in an R package:

Error in as(m, "graphNEL") : 
no method or default for coercing “matrix” to “graphNEL”

The issue appears to be importing the required as() method, but I cannot seem to figure out how to do this. Note that this still fails if the graph package is included in Imports.

JohnA
  • 321
  • 2
  • 11
  • is `as()` part of `graph`, or is it the one that's part of `methods` - `methods::as` ? – SymbolixAU May 12 '16 at 03:07
  • methods::as doesn't work either. The definition of as is rather complicated (see ?as), and admittedly I am not familiar enough with S4 classes to figure this one out myself... – JohnA May 12 '16 at 03:09

1 Answers1

1

It should work if you specify the package that contains as() with ::.

Try this:

gr <- graph::as(m, "graphNEL")

Just to keep all of the information in one place.
The graph package has been removed from CRAN and is only available through Bioconductor now. As far as I know, Bioconductor packages do NOT get imported just by putting them in imports.

This post suggests the same: CRAN Package Depends on Bioconductor Package Installing error

It is possible that during testing of previous packages which imported Bioconductor packages, that these packages were already installed on the system that you were testing on. However, if that is the case, then I would assume that to also be the case here. Thus my solution may not solve your problem.

Community
  • 1
  • 1
enpitsu
  • 636
  • 3
  • 6
  • This was my first guess, but unfortunately it doesn't work--you'll get the same error. This actually makes sense since the graph package doesn't explicitly export any method called "as". In general, packages that delegate an as method won't explicitly export it I believe. – JohnA May 12 '16 at 03:03
  • Just to be sure, you tried this while the `graph` package was in your Imports? (Sounds like the answer is yet, but just checking.) – enpitsu May 12 '16 at 03:04
  • Yep, graph is in Imports. (And no worries, it's always good to be clear on these things.) – JohnA May 12 '16 at 03:06
  • 1
    Looking online it looks like the `graph` package has been removed from CRAN and is only available through Bioconductor now. Unfortunately, Bioconductor packages do NOT get imported just by putting them in imports. As far as I know, you need to have the user install the package manually before running code from your package that depends on it. – enpitsu May 12 '16 at 03:07
  • Hmmm... I have written other packages with Bioconductor packages (including graph) listed in Imports and they behave exactly as other CRAN packages. From what I can tell, this is an S4 issue, and not a graph or Bioconductor issue. But I could be wrong... – JohnA May 12 '16 at 03:11
  • This post is relevant, but also appears to have a workaround (Although this might not be the solution to your problem.): http://stackoverflow.com/questions/14343817/cran-package-depends-on-bioconductor-package-installing-error – enpitsu May 12 '16 at 03:20