为`+`定义data.frame方法并在ggplot对象上使用时,二进制运算符的非数字参数 [英] non-numeric argument to binary operator when defining a data.frame method for `+` and using on ggplot object

查看:62
本文介绍了为`+`定义data.frame方法并在ggplot对象上使用时,二进制运算符的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样定义S3方法:

I can define a S3 method like this :

`+.data.frame` <- function(e1,e2){"hello"}
iris + iris
# [1] "hello"

但是,如果 e2 gg 对象,则此方法将无效:

But this won't work if e2 is a gg object :

iris + geom_point()

iris + geom_point()中的错误:二进制运算符的非数字参数

Error in iris + geom_point() : non-numeric argument to binary operator

另外:警告消息:不兼容的方法("+ .data.frame","+ .gg")表示"+"

In addition: Warning message: Incompatible methods ("+.data.frame", "+.gg") for "+"

我认为这与S4方法有关,但我感到困惑.您能解释一下发生了什么以及如何解决吗?

I think it has something to do with S4 methods but I'm confused. Can you explain what's at play and how to sort it out ?

所需的输出:

iris + geom_point()
# [1] "hello"

推荐答案

这实际上不是由于S4方法引起的,而是方法冲突. geom_point()和您的数据框都不是S4对象(它们中的任何 isS4 返回FALSE),因此S4方法将不会起作用.

This is actually not due to S4 methods but a method conflict. Neither geom_point() nor your data frame are S4 objects (isS4 on either of them returns FALSE), so S4 methods will not come into play.

ggplot 包为 gg 分类的对象定义了 + 的方法,而您刚刚定义了 data的方法.frame s.麻烦的是,与大多数S3泛型函数不同, + 在选择方法时考虑了两个参数的类.在这种情况下,得出的结论是,它可以合理地选择 gg 方法以及 data.table 方法.

The ggplot package defines a method for + on gg-classed objects, and you have just defined a method for data.frames. The trouble is, unlike most S3 generic functions, + takes the class of both arguments into account in selecting a method. In this case, it concludes that it could have legitimately chosen the gg method as well as the data.table method.

从"Ops"(其中"+"是成员"的帮助页面):

From the help page for "Ops" (of which "+" is a member):

在分派该组的任何成员时,将同时考虑两个参数的类.对于每个自变量,将检查其类向量,以查看是否存在匹配的特定(首选)或Ops方法.如果仅为一个参数找到一个方法,或者为两个参数找到相同的方法,则使用该方法.如果找到了不同的方法,则会出现有关不兼容的方法"的警告:在这种情况下,或者如果找不到任何参数的方法,则使用内部方法.

The classes of both arguments are considered in dispatching any member of this group. For each argument its vector of classes is examined to see if there is a matching specific (preferred) or Ops method. If a method is found for just one argument or the same method is found for both, it is used. If different methods are found, there is a warning about ‘incompatible methods’: in that case or if no method is found for either argument the internal method is used.

因此,在这种情况下,您会看到默认的 + 代码,该代码不知道如何添加数据帧和 gg 对象,并返回错误说.

So you see in this case, it falls through to the default + code, which doesn't know how to add data frames and gg objects, and returns an error saying as much.

要解决此问题,您可以编写带有签名(data.frame,gg)(或也许(data.frame,ANY))的S4方法但要注意–只有在其中一个参数是S4对象(默认情况下,该data.frames不是该参数)时,它才会被实际调用.您可能必须定义自己的包含 data.frame 的类(或者包含 gg )以触发该方法.

To solve the issue, you may be able to write an S4 method with signature (data.frame,gg) (or perhaps (data.frame,ANY) but beware — it will only actually be called if one of the arguments is an S4 object (which data.frames are not by default). You may have to define your own class which contains data.frame (or, alternatively, contains gg) in order to trigger the method.

这篇关于为`+`定义data.frame方法并在ggplot对象上使用时,二进制运算符的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆