gpt4 book ai didi

r - 在browser()中完成和继续之间有什么区别?

转载 作者:行者123 更新时间:2023-12-03 12:17:40 28 4
gpt4 key购买 nike

browser的帮助文件中,有两个看起来非常相似的选项:
f

finish execution of the current loop or function


c

exit the browser and continue execution at the next statement.



它们之间有什么区别,在什么情况下区别明显?

关于可能有什么区别的一些线索-我编写了一个名为 browse.R的脚本,其内容如下:
for (i in 1:2){
browser()
print(i)
}

这是使用 cf的结果:
> source("browse.R")
Called from: eval(expr, envir, enclos)
Browse[1]> c
[1] 1
Called from: eval(expr, envir, enclos)
Browse[1]> c
[1] 2
> source("browse.R")
Called from: eval(expr, envir, enclos)
Browse[1]> f
[1] 1
Browse[2]> f
[1] 2

请注意, Browse[n]的级别发生了变化。这仍然没有突出显示它们之间的任何实际区别。

我还尝试查看浏览器环境是否会消失:
for (i in 1:2){
a <- "not modified"
browser()
print(a)
}

Called from: top level
Browse[1]> a <- "modified"
Browse[1]> f
[1] "modified"
Browse[1]> a
[1] "not modified"
Browse[1]> a <- "modified"
Browse[1]> c
[1] "modified"

所以那里也没有区别。

最佳答案

两者之间有很小的差异。

  • c立即退出浏览器(和 Debug模式),然后以正常方式执行其余代码。
  • 相反,
  • f在执行其余功能/循环时停留在浏览器(和 Debug模式)中。功能/循环完成后,他还返回到正常执行模式。

  • 资料来源: R-source(第1105-1117行)和 R-help

    这有一些含义:
  • c关闭浏览器。这意味着从函数中调用了新的浏览器。因此,您将看到以下行:Called from: function()。另一方面,f不会关闭浏览器,因此您不会看到此行。此行为的源代码在这里:https://github.com/wch/r-source/...
  • 因为f保留在浏览器中,所以f也会跟踪上下文级别:

  • The browser prompt is of the form Browse[n]>: here var{n} indicates the ‘browser level’. The browser can be called when browsing (and often is when debug is in use), and each recursive call increases the number. (The actual number is the number of ‘contexts’ on the context stack: this is usually 2 for the outer level of browsing and 1 when examining dumps in debugger)



    可以使用以下代码测试这些差异:
    > test <- function(){
    browser()
    browser()
    }

    > test()
    Called from: test()
    Browse[1]> c
    Called from: test()
    Browse[1]> c

    > test()
    Called from: test()
    Browse[1]> f
    Browse[2]> f

    据我所知,除非上下文堆栈中有实际用途,否则两者之间没有实际区别。 Debug模式没有附加值。调试标志仅在您输入函数时打开浏览器,但由于您已经在函数内部,因此不会触发其他效果。

    关于r - 在browser()中完成和继续之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914393/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com