这里的useMethod是什么意思?

发布于 2024-12-18 06:13:13 字数 301 浏览 1 评论 0原文

R 的一大优点是,如果我输入函数名称,我就可以看到其实现。但这让我递归地感到困惑:

> library(xts)
> align.time
function (x, ...) 
{
    UseMethod("align.time")
}
<environment: namespace:xts>

x是一个XTS对象,所以这是否意味着它将调用XTS的align.time方法...但这就是我正在看的! (输入 xts::align.time 会给出完全相同的响应。)

One of the kool things about R is if I type the function name I get to see the implementation. But this one is confusing me, recursively:

> library(xts)
> align.time
function (x, ...) 
{
    UseMethod("align.time")
}
<environment: namespace:xts>

x is an XTS object, so doesn't that mean it will call the XTS align.time method... but that is what I'm looking at! (Typing xts::align.time gives exactly the same response.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-12-25 06:13:13

简而言之,您正在寻找函数 xts:::align.time.xts

更长的答案是,您可以通过调用 methods 来查找 align.time 存在哪些方法:

> methods(align.time)
[1] align.time.POSIXct* align.time.POSIXlt* align.time.xts*    

   Non-visible functions are asterisked

这告诉您有一个方法 align.time.xts< /code> 不是从命名空间导出的。此时您可能会猜测它可以在 xts 包中找到,但是您可以使用 getAnywhere 来确认:

> getAnywhere("align.time.xts")
A single object matching 'align.time.xts' was found
It was found in the following places
  registered S3 method for align.time from namespace xts
  namespace:xts
with value

function (x, n = 60, ...) 
{
    if (n <= 0) 
        stop("'n' must be positive")
    .xts(x, .index(x) + (n - .index(x)%%n), tzone = indexTZ(x), 
        tclass = indexClass(x))
}
<environment: namespace:xts>

您当然可以直接读取源代码,但是由于该函数未导出,因此您需要使用 package:::function (即三个冒号):

> xts:::align.time.xts
function (x, n = 60, ...) 
{
    if (n <= 0) 
        stop("'n' must be positive")
    .xts(x, .index(x) + (n - .index(x)%%n), tzone = indexTZ(x), 
        tclass = indexClass(x))
}
<environment: namespace:xts>

The short answer is that you are looking for the function xts:::align.time.xts.

The longer answer is that you can find which methods exist for align.time by calling methods:

> methods(align.time)
[1] align.time.POSIXct* align.time.POSIXlt* align.time.xts*    

   Non-visible functions are asterisked

This tells you that there is a method align.time.xts that is not exported from the namespace. At this point you can probably guess that it can be found in package xts, but you can confirm that with getAnywhere:

> getAnywhere("align.time.xts")
A single object matching 'align.time.xts' was found
It was found in the following places
  registered S3 method for align.time from namespace xts
  namespace:xts
with value

function (x, n = 60, ...) 
{
    if (n <= 0) 
        stop("'n' must be positive")
    .xts(x, .index(x) + (n - .index(x)%%n), tzone = indexTZ(x), 
        tclass = indexClass(x))
}
<environment: namespace:xts>

You can, of course, read the source directly, but since the function is not exported, you need to use package:::function (i.e. three colons):

> xts:::align.time.xts
function (x, n = 60, ...) 
{
    if (n <= 0) 
        stop("'n' must be positive")
    .xts(x, .index(x) + (n - .index(x)%%n), tzone = indexTZ(x), 
        tclass = indexClass(x))
}
<environment: namespace:xts>
甜嗑 2024-12-25 06:13:13

align.time() 是从 xts 命名空间导出的,因此 xts::align.timealign.time 是同一件事。您需要注意,包中提供了一个 align.time() 方法,用于类 "xts" 的对象,并且该方法不是从命名空间导出的(它是刚刚注册为 S3 方法):

> xts:::align.time.xts
function (x, n = 60, ...) 
{
    if (n <= 0) 
        stop("'n' must be positive")
    .xts(x, .index(x) + (n - .index(x)%%n), tzone = indexTZ(x), 
        tclass = indexClass(x))
}
<environment: namespace:xts>

当您将​​ "xts" 对象传递给 align.time() 时,就会调用此方法。

当您调用 align.time() 时,UseMethod() 将搜索并调用适当的 "align.time" 方法,如果对于作为第一个参数提供的对象类可用。 UseMethod 正在做你认为它正在做的事情,你只是通过以两种不同的方式查看同一个函数(通用函数)而混淆了自己。

align.time() is exported from the xts namespace, so xts::align.time and align.time are the same thing. You need to note that there is an align.time() method for objects of class "xts" provided in the package and that is not exported from the namespace (it is just registered as an S3 method):

> xts:::align.time.xts
function (x, n = 60, ...) 
{
    if (n <= 0) 
        stop("'n' must be positive")
    .xts(x, .index(x) + (n - .index(x)%%n), tzone = indexTZ(x), 
        tclass = indexClass(x))
}
<environment: namespace:xts>

It is this method that is being called when you pass an "xts" object to align.time().

When you call align.time() UseMethod() sets up the search for and call of the appropriate "align.time" method, if available, for the class of object supplied as the first argument. UseMethod is doing exactly what you think it is doing, you have just confused yourself by looking at the same function (the generic) in two different ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文