从父类调用setup_data [英] Call setup_data from parent class

查看:149
本文介绍了从父类调用setup_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在阅读有关如何将自调整文本放置到条中的出色答案:

I was reading this excellent answer on how to place self adjusting text into bars: Resizeable text grobs inside bars

ggproto上阅读了一点,尤其是在

After reading a bit on ggproto and especially the vignette on Extending ggplot I was wondering why the author had to define the setup_data routine as follows:

GeomFit <- ggproto("GeomFit", GeomRect,
               setup_data = function(data, params) {
                 data$width <- data$width %||%
                   params$width %||% (resolution(data$x, FALSE) * 0.9)
                 transform(data,
                           ymin = pmin(y, 0), ymax = pmax(y, 0),
                           xmin = x - width / 2, xmax = x + width / 2, width = NULL
                 )
               })

因为这本质上是来自ggplot2::GeomBar的复制粘贴:

Because this is essentially a copy paste from ggplot2::GeomBar:

GeomBar$setup_data
# <ggproto method>
#   <Wrapper function>
#     function (...) 
# f(...)

#   <Inner function (f)>
#     function (data, params) 
# {
#     data$width <- data$width %||% params$width %||% (resolution(data$x, 
#         FALSE) * 0.9)
#     transform(data, ymin = pmin(y, 0), ymax = pmax(y, 0), xmin = x - 
#         width/2, xmax = x + width/2, width = NULL)
# }

所以我想我可以简单地替换为:

So I thought I could replace this simply by:

GeomFit <- ggproto("GeomFit", GeomRect,
                   setup_data = function(self, data, params)
                      ggproto_parent(GeomBar, self)$setup_data(data, params))

这种方法有效,但是我怀疑这是否会导致某些不必要的行为,只是因为GeomFit的父类是GeomRect不是 GeomBar.

This approach works, but I have my doubts whether this may lead to some unwanted behaviour simply becasue the parent class of GeomFit is GeomRect and not GeomBar.

问题

使用ggproto_parent从不是ggproto对象的父类的类中调用函数是否可以(在某种意义上:没有条件可能导致问题的意义)?为什么ggproto_parent首先要有parent自变量?父母不应该由ggproto的第二个参数确定吗?

Is it ok (in the sense of: there are no conditions where this may cause a problem) to use ggproto_parent to call a function from a class which is not the parent class of my ggproto object? Why does ggproto_parent have a parent argument in the first place? Shouldn't the parent be anyways determined by the second argument of ggproto?

推荐答案

我没有参与ggplot2程序包的开发,但是由于已经过了一周的时间,所以我会对其进行调查.到目前为止,还没有人发布...

I'm not involved in the ggplot2 package's development, but I'll take a stab at this since it's been a week & no one else has posted so far...

以下是我从ggplot2的 GitHub页面:

Below are the relevant function definitions I copied off ggplot2's GitHub page:

ggproto_parent <- function(parent, self) {
  structure(list(parent = parent, self = self), class = "ggproto_parent")
}

`$.ggproto_parent` <- function(x, name) {
  res <- fetch_ggproto(.subset2(x, "parent"), name)
  if (!is.function(res)) {
    return(res)
  }

  make_proto_method(.subset2(x, "self"), res)
}

从第一个函数中,我们可以推断出ggproto_parent(GeomBar, self)在这种情况下将返回两个项的列表list(parent = GeomBar, self = self)(其中self解析为GeomFit).该列表以x的形式传递给第二个函数.

From the first function, we can deduce that ggproto_parent(GeomBar, self) in this case would return a list of two items, list(parent = GeomBar, self = self) (where self resolves to GeomFit). This list is passed to the second function as x.

然后,第二个函数在x[["parent"]]中搜索与给定名称相对应的方法(在本例中为setup_data(fetch_ggproto)),如果所涉及的函数包括x[["self"]]关联>参数(make_proto_method).

The second function then searches within the x[["parent"]] for a method corresponding to the given name, in this case setup_data (fetch_ggproto), and associates it with x[["self"]] if the function in question includes a self parameter (make_proto_method).

这两个函数都不检查GeomFit的父级(可以在GeomFit$super()进行访问),所以我认为在ggproto_parent()中使用GeomBar代替GeomRect并不重要.

Neither function checks for the parent of GeomFit (which can be accessed at GeomFit$super()), so I don't think having GeomBar instead of GeomRect in ggproto_parent() really matters.

也就是说,我可能会使用这样的东西:

That said, I would probably have used something like this:

GeomFit <- ggproto("GeomFit", GeomRect,
                   setup_data = .subset2(GeomBar, "setup_data"))

或者这个:

GeomFit <- ggproto("GeomFit", GeomRect,
                   setup_data = environment(GeomBar$setup_data)$f)

哪个代码更短.

这篇关于从父类调用setup_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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