如何从所需的包中调试未导出的函数?

bio*_*man 7 debugging namespaces r

在我正在使用的包中的一个功能是给我不那么信息错误.我不知道发生了什么事.该函数由我调用的函数在内部调用.像这样的东西:

myres <- the.func(x)

the.func <-function(x){
    unexported.func(x)
}
Run Code Online (Sandbox Code Playgroud)

我该如何调试unexported.func?使用debug不起作用:

>debug(unexported.func)
Error in debug(undexported.func) : object 'unexported.func' not found
Run Code Online (Sandbox Code Playgroud)

更新: 目前我做嵌套调试,如下所示.但我觉得不方便:

>debug(the.func) # Initiate debugging for the outer function, so I get unexported.func loaded.
>myres <- the.func(x)
Browse[2]>debug(unexported.func) # Now I can call debug with this. 
Run Code Online (Sandbox Code Playgroud)

Rei*_*son 8

您可以通过:::(三重冒号)运算符访问未导出的函数,并使用包命名空间名称(即包名称)作为前缀.

假设pkgA包含未导出的函数unexported.func(),我们将unexported.func()使用以下命令设置调试标志:

debug(pkgA:::unexported.func)
Run Code Online (Sandbox Code Playgroud)

如果您不知道要为给定的未导出函数使用哪个包(因此命名空间),您始终可以使用它来确定getAnywhere().