R使用浏览器
示例
该browser函数可以像断点一样使用:代码执行将在调用点处暂停。然后,用户可以检查变量值,执行任意R代码,并逐行逐步执行该代码。
一旦browser()在代码中命中,交互式解释器将启动。任何R代码都可以正常运行,此外还存在以下命令,
例如,我们可能有一个脚本,
toDebug <- function() {
a = 1
b = 2
browser()
for(i in 1:100) {
a = a * b
}
}
toDebug()运行上面的脚本时,我们最初会看到类似的内容,
Called from: toDebug Browser[1]>
这样我们就可以与提示进行交互了,
Called from: toDebug
Browser[1]> a
[1] 1
Browser[1]> b
[1] 2
Browse[1]> n
debug at #7: for (i in 1:100) {
a = a * b
}
Browse[2]> n
debug at #8: a = a * b
Browse[2]> a
[1] 1
Browse[2]> n
debug at #8: a = a * b
Browse[2]> a
[1] 2
Browse[2]> Qbrowser()也可以用作功能链的一部分,如下所示:
mtcars %>% group_by(cyl) %>% {browser()}