如何在RStudio中调试

如何在RStudio中调试

本文介绍了如何在RStudio中调试(放置断点等)安装的R包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要逐行运行一个函数来了解它的工作原理。但是这个功能是安装的软件包的一部分,我不知道R存储安装的软件包的来源(比如说MultiPhen)。我在Ubuntu 12(64it)中使用RStudio 0.98.501和R 3.0.2。显然没有存储安装的软件包的源代码,对吧?对不起,如果它是一个天真的问题,我是新的R.
如果源不存储,是否有反馈重新安装一个源代码和调试包(基本上放置一个断点)。

I need to run a function line-by-line to understand how it works. But the function is part of an installed package and I don't know where R stores the source of the installed packages (say MultiPhen). I am using RStudio 0.98.501 and R 3.0.2 in Ubuntu 12 (64it). Apparently source code of installed packages are not stored, right? Sorry if it is a naive question, I am new to R.If the sources are not stored, is there anyway to re-install a package with source and debug it (basically place a break point).

谢谢,

Kayhan

推荐答案

查看 trace 。这是在基础包函数 var 中的第四个语句添加断点的示例。这里我们要求 trace 在第六个语句中调用函数 browser

Look at trace. Here is an example adding a breakpoint at the fourth statement in the base package function var. Here we ask trace to invoke the function browser at the sixth statement:

> trace(var, browser, at=6)
Tracing function "var" in package "stats"
[1] "var"
> var(1:10)
Tracing var(1:10) step 6
Called from: eval(expr, envir, enclos)
Browse[1]> n
debug: if (is.data.frame(y)) y <- as.matrix(y) else stopifnot(is.atomic(y))
Browse[2]> n
debug: stopifnot(is.atomic(y))
Browse[2]> n
debug: .Call(C_cov, x, y, na.method, FALSE)
Browse[2]> n
[1] 9.166667

记住 untrace 您可以使用 trace 做相当复杂的东西,尽管在大多数情况下, trace(fun.name,browser)可能足够

Remember to untrace when you're done. You can do fairly complex stuff with trace, though in most cases trace(fun.name, browser) is probably enough.

或者,您只需加载包,并在命令行中键入该功能的名称,如下所示:

Alternatively, you can just load the package and type the name of the function on the command line like so:

> var
function (x, y = NULL, na.rm = FALSE, use)
{
    if (missing(use))
        use <- if (na.rm)
            "na.or.complete"
        else "everything"
    na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs",
        "everything", "na.or.complete"))
    if (is.na(na.method))
        stop("invalid 'use' argument")
    if (is.data.frame(x))
        x <- as.matrix(x)
    else stopifnot(is.atomic(x))
    if (is.data.frame(y))
        y <- as.matrix(y)
    else stopifnot(is.atomic(y))
    .Call(C_cov, x, y, na.method, FALSE)
}
<bytecode: 0x000000000928ad30>
<environment: namespace:stats>

然后,您可以将其复制到编辑器中,随身携带,添加您的浏览器语句,并逐步查看结果。

You can then copy that into your editor and play around with it, add your browser statement, and step through the results.

这篇关于如何在RStudio中调试(放置断点等)安装的R包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 06:34