使用 R,如何跟踪基于 library::method 的函数?

Using R, how to trace a function based on library::method?

我正在审查我认为属于库 devtools 的函数 install_github

确实如此。它属于图书馆 remotes.

当 RStudio 中触发错误时,您可以通过跟踪堆栈来进行故障排除。我可以主动应用该逻辑吗?

是否有函数 traceFunction() 或可以查看 return 顺序 library::method 调用列表的函数?

如果函数不存在,可以吗?

traceFunction("install_github");

更新

为了澄清,我对引用的包做了 git clonedevtools 具有以下内容:

#' @importFrom remotes install_github
#' @rdname remote-reexports
#' @export
install_github <- with_pkgbuild_build_tools(with_ellipsis(remotes::install_github))

其中 remotes 具有以下内容:

#' # To install from a private repo, use auth_token with a token
#' # from https://github.com/settings/tokens. You only need the
#' # repo scope. Best practice is to save your PAT in env var called
#' # GITHUB_PAT.
#' install_github("hadley/private", auth_token = "abc")
#'
#' # To pass option arguments to `R CDM INSTALL` use `INSTALL_opts`. e.g. to
#' install a package with source references and tests
#' install_github("rstudio/shiny", INSTALL_opts = c("--with-keep.source", "--install-tests"))
#' }
install_github <- function(repo,
                           ref = "HEAD",
                           subdir = NULL,
                           auth_token = github_pat(quiet),
                           host = "api.github.com",
                           dependencies = NA,
                           upgrade = c("default", "ask", "always", "never"),
                           force = FALSE,
                           quiet = FALSE,
                           build = TRUE, build_opts = c("--no-resave-data", "--no-manual", "--no-build-vignettes"),
                           build_manual = FALSE, build_vignettes = FALSE,
                           repos = getOption("repos"),
                           type = getOption("pkgType"),
                           ...) {

  remotes <- lapply(repo, github_remote, ref = ref,
    subdir = subdir, auth_token = auth_token, host = host)

  install_remotes(remotes, auth_token = auth_token, host = host,
    dependencies = dependencies,
    upgrade = upgrade,
    force = force,
    quiet = quiet,
    build = build,
    build_opts = build_opts,
    build_manual = build_manual,
    build_vignettes = build_vignettes,
    repos = repos,
    type = type,
    ...)
}

它调用 install_remotes,我通过搜索存储库 remotes.

中的所有“.R”文件不容易找到它

简答,没有。 R 是动态类型的,有许多不同的类型系统(S3、S4、R6 等)。简而言之,我们看到了动态类型对调用的影响。 printplot。您可能已经注意到 print 对于矩阵、简单向量、列表、data.frames 等的表现不同。plot 更加多样化,可以处理前面提到的类型,以及几乎任何自定义从其他包(相关矩阵、热图等)抛给它的对象 - 并产生截然不同的结果。

这是由于 R 的方法调度(以及不同类型系统的类似想法),它基本上查看作为第一个参数传递给函数的对象的 class。然后它会尝试为对象的 class 属性中的每个 class 调用 plot.<class>,直到某些东西起作用。如果没有任何效果,它会退回到 plot.default.

这就是为什么许多软件包可以使用 plot 功能的原因。他们实现了一个绘图函数,比如 plot.foobaz,适用于他们的 foobaz-classed 对象。

还有一些方法,根据输入,连接函数名称,然后尝试调用它们

最重要的是,我们可以将不同的包放入环境中(例如 library),这可能会改变执行路径,当包的方法 mask以前加载的包。

因此,要主动找出方法的调用树,将仅限于基于实际传递的对象。有一个包可以做到这一点,https://rdrr.io/cran/lobstr/man/cst.html.