Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S4 object inheriting from data.frame: remove warning in dplyr::filter

I have an S4 class that inherits from data.frame:

> setClass("Foo", contains="data.frame")
> x <- new("Foo", data.frame(a=1:2))

Now dplyr::filter works but issues a warning:

> x %>% dplyr::filter(a > 1)
  a
1 2
Warning message:
In class(x) <- c("tbl_df", "tbl", "data.frame") :
  Setting class(x) to multiple strings ("tbl_df", "tbl", ...); result
  will no longer be an S4 object

Is there any good way to get rid of this warning? I can create a new generic of filter, and wrap the dplyr version in this something like

dplyr::filter(as.data.frame(unclass(x)), ...)

but isn't the point with inheritance to avoid such extra work?

Is this warning related to dplyr not being aware of S4 objects?

Edit: same behavior for dplyr 0.4.3 and 0.5.0

like image 932
Ott Toomet Avatar asked Nov 20 '25 08:11

Ott Toomet


1 Answers

dplyr utilises the tibble package, where a conversion from a data.frame to a tibble takes place which assigns three classes to the argument. unclass() is called before this, but unclass'ing does not strip the object of its status as being an S4 object, and so the warning above occurs.

In short, this is a problem within dplyr. I think you would have to make wrappers for the generics.

like image 178
stephematician Avatar answered Nov 21 '25 23:11

stephematician



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!