[Bioc-devel] Possible to export coerce2() from S4Vectors?

Pages, Herve hp@ge@ @ending from fredhutch@org
Wed Nov 14 02:55:27 CET 2018

Hi Kylie,

I've modified coerce2() in S4Vectors 0.21.5 so that `coerce2(from, to)` should now do the right thing when 'to' is a DataFrame derivative:


With the following gotcha: this will work only if coercion (with as()) from DataFrame to the DataFrame derivative does the right thing. So I'm assuming that this coercion makes sense and can be supported. There are 2 possible situations:

1) The automatic coercion method from DataFrame to your DataFrame derivative (i.e. the coercion method automatically defined by the methods package) does the right thing. In this case coerce2() (and therefore [[<-) will also do the right thing on your DataFrame derivatives. For example:

  setClass("MyDataFrameExtension", contains="DataFrame")

  ## WARNING: Don't trust selectMethod() here!
  selectMethod("coerce", c("DataFrame", "MyDataFrameExtension"))
  # Error in selectMethod("coerce", c("DataFrame", "MyDataFrameExtension")) :
  #  no method found for signature DataFrame, MyDataFrameExtension

  as(DataFrame(), "MyDataFrameExtension")
  # MyDataFrameExtension with 0 rows and 0 columns

  ## The automatic coercion method is only created the 1st time it's used!
  ## So now selectMethod() shows it:
  selectMethod("coerce", c("DataFrame", "MyDataFrameExtension"))
  # Method Definition:
  # function (from, to = "MyDataFrameExtension", strict = TRUE)
  # {
  #     obj <- new("MyDataFrameExtension")
  #     as(obj, "DataFrame") <- from
  #     obj
  # }
  # <environment: namespace:S4Vectors>
  # Signatures:
  #         from        to
  # target  "DataFrame" "MyDataFrameExtension"
  # defined "DataFrame" "MyDataFrameExtension"

  MDF <- new("MyDataFrameExtension")
  S4Vectors:::coerce2(list(aa=1:3, bb=21:23), MDF)
  # MyDataFrameExtension with 3 rows and 2 columns
  #          aa        bb
  #   <integer> <integer>
  # 1         1        21
  # 2         2        22
  # 3         3        23

2) The automatic coercion method from DataFrame to your DataFrame derivative doesn't do the right thing (e.g. it returns an invalid object). In this case you need to define this coercion (with a setAs() statement). This will allow coerce2() (and therefore [[<-) to do the right thing on your DataFrame derivatives.

There is no plan at the moment to export coerce2() because this should not be needed. The idea is that developers should not need to define "coerce2" methods but instead make it work via the addition of the appropriate coercion methods. The only purpose of coerce2() is to support things like [[<- and endoapply(). Once coerce2() works properly, these things work out-of-the-box.

So to summarize: just make sure that a DataFrame can be coerced to your DataFrame derivative and [[<- and endoapply() will work out-of-the-box. It could be however that this coercion doesn't make sense and cannot be supported, in which case, we'll need to do something different. Let me know if that's the case.


On 11/13/18 12:45, Bemis, Kylie wrote:

Dear all,

Are there any plans to export coerce2() from the S4Vectors namespace, like other exported internal utilities such as showAsCell() and setListElement()?

I have a couple classes that inherit from DataFrame, and some inherited methods (like [[<-) break in certain situations due to calls to coerce2() that coerce arguments to a regular DataFrame (instead of my subclass). This could be fixed if I were able to implement a coerce2() method for my subclass.

Any suggestions on how to approach problems like this when inheriting from DataFrame and other Vector derivatives?

Many thanks,

Kylie Ariel Bemis
College of Computer and Information Science
Northeastern University

        [[alternative HTML version deleted]]

Bioc-devel using r-project.org<mailto:Bioc-devel using r-project.org> mailing list

Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages using fredhutch.org<mailto:hpages using fredhutch.org>
Phone:  (206) 667-5791
Fax:    (206) 667-1319

	[[alternative HTML version deleted]]

More information about the Bioc-devel mailing list