useMethod 在这里是什么意思? [英] What does useMethod mean here?

查看:34
本文介绍了useMethod 在这里是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 R 的一个很酷的事情是,如果我输入函数名称,我就会看到实现.但是这个递归地让我困惑:

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 是一个 XTS 对象,所以这并不意味着它会调用 XTS align.time 方法......但这就是我正在寻找的!(输入 xts::align.time 给出完全相同的响应.)

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.)

推荐答案

简短的回答是您正在寻找函数 xts:::align.time.xts.

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

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

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

这告诉你有一个方法 align.time.xts 不是从命名空间导出的.此时你大概可以猜到它可以在包 xts 中找到,但是你可以用 getAnywhere 来确认:

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>

<小时>

当然可以直接读取源码,​​但是由于函数没有导出,所以需要使用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>

这篇关于useMethod 在这里是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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