在R中,如何找出为特定函数调用分派的方法?

dan*_*dan 5 r

我试图理解一些我没写的代码(mg.gv中的plot.gam),并且调用了plot()函数,其中包含一些我不认识的奇怪参数(例如"P").我想弄清楚这个调用中正在调度哪个绘图方法.findMethod()和类似的函数没有帮助(我认为情节是S3).我尝试了调试库,但这不会让你"进入"函数调用(基本调试函数也没有).

有没有办法监控R中的所有函数调用及其相关的方法调度?或者也许是一个函数,我可以传递一个包含实际函数调用的字符串(而不仅仅是签名),它将告诉我调度的方法是什么?

Rei*_*son 5

plot.gam()我们注意到plot()被调用x$smooth[[i]],这是类的对象:

class(x$smooth[[i]])
[1] "tprs.smooth" "mgcv.smooth"
Run Code Online (Sandbox Code Playgroud)

一个plot()类的方法"mgcv.smooth",它是这个正被用于一般情况下的情节.?plot.gam提到这是用于大多数平滑的默认方法,但是有一些特定方法可以支持某些类型的平滑gam()(来自以下内容的详细信息部分?plot.gam:

For smooth terms ‘plot.gam’ actually calls plot method functions
depending on the class of the smooth. Currently random effect and
Markov random field smooths have special methods, the rest use the
defaults described below.
Run Code Online (Sandbox Code Playgroud)

出于某种原因,methods()没有找到这些方法,但确实存在:

> mgcv:::plot.mgcv.smooth
function (x, P = NULL, data = NULL, label = "", se1.mult = 1, 
    se2.mult = 2, partial.resids = FALSE, rug = TRUE, se = TRUE, 
    scale = -1, n = 100, n2 = 40, pers = FALSE, theta = 30, phi = 30, 
    jit = FALSE, xlab = NULL, ylab = NULL, main = NULL, ylim = NULL, 
    xlim = NULL, too.far = 0.1, shade = FALSE, shade.col = "gray80", 
    shift = 0, trans = I, by.resids = FALSE, scheme = NULL, ...) 
{
....
Run Code Online (Sandbox Code Playgroud)

在这可能与一个bug methods()这意味着plot.function在列表中没有显示我的当前R可能不会纳入该修复.这种方法应该正常显示,在这种情况下的一般建议是识别对象类(如上所示),然后使用methods()和类似的函数(例如showMethods())来确定是否可用于类的特定方法返回的对象.