在浏览器()中完成和继续有什么区别?

时间:2022-09-06 13:13:04

In the help file for browser, there are two options that seem very similar:

在浏览器的帮助文件中,有两个看似非常相似的选项:

f

F

finish execution of the current loop or function

完成当前循环或函数的执行

c

C

exit the browser and continue execution at the next statement.

退出浏览器并在下一个语句处继续执行。

What is the difference between them and in what situations is the difference apparent?

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

Some clues about what may be the difference - I wrote a script called browse.R with the following contents:

关于可能有什么区别的一些线索 - 我写了一个名为browse.R的脚本,其中包含以下内容:

for (i in 1:2){
  browser()
  print(i)
}

This is the results of usingc vs f:

这是usingc vs f的结果:

> 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

Note that the level of Browse[n] changes. This still doesn't highlight any practical difference between them.

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

I also tried to see if perhaps things would disappear from the browser environment:

我还试图看看是否可能会从浏览器环境中消失:

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"

So there's no difference there either.

所以那里也没有区别。

3 个解决方案

#1


9  

There is a small difference.

有一点不同。

  • c immediately exits the browser (and debug mode) and after that executes the rest of the code in the normal way.
  • c立即退出浏览器(和调试模式),之后以正常方式执行其余代码。
  • f on the contrary stays in the browser (and debug mode) while executing the rest of the function/loop. After the function/loop is finished, he also returns to the normal execution mode.
  • 相反,f在执行函数/循环的其余部分时保持在浏览器(和调试模式)中。函数/循环结束后,他也返回正常执行模式。

Source: R-source (line 1105-1117) and R-help

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

This has a few implications:

这有一些影响:

  • c closes the browser. This means that a new browser call is called from a function. Therefore you will see the line: Called from: function(). f on the other hand will not close the browser and therefore you will not see this line. The source code for this behavior is here: https://github.com/wch/r-source/....
  • c关闭浏览器。这意味着从函数调用新的浏览器调用。因此,您将看到以下行:来自:function()。另一方面,f不会关闭浏览器,因此你不会看到这一行。此行为的源代码如下:https://github.com/wch/r-source / ....
  • Because f stays in the browser, f also keeps track of the contextlevel:
  • 因为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)

浏览器提示符为Browse [n]>形式:此处var {n}表示“浏览器级别”。浏览时可以调用浏览器(通常是在使用调试时),每次递归调用都会增加数量。 (实际数字是上下文堆栈中'contexts'的数量:在外部浏览级别通常为2,在调试器中检查转储时为1)

These differences can be tested with the code:

可以使用以下代码测试这些差异:

> 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

As far as I see it, there is no practical difference between the two, unless there lies a practical purpose in the context stack. The debugging mode has no added value. The debug flag only opens the browser when you enter the function but since you are already inside the function, it will not trigger another effect.

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

#2


3  

Difference Between Browser and Continue

At least for me, I feel the answer can be mapped out as a table, however, let's first frame up the usage of browser(), for those who may not yet have encountered it.

至少对我来说,我觉得答案可以作为一个表格进行映射,但是,让我们首先考虑使用browser(),以便那些可能还没有遇到过它的人。

The browser function is the basis for the majority of R debugging techniques. Essentially, a call to browser halts execution and starts a special interactive session where you can inspect the current state of the computations and step through the code one command at a time.

浏览器功能是大多数R调试技术的基础。实际上,对浏览器的调用会停止执行并启动一个特殊的交互式会话,您可以在其中检查计算的当前状态并一次单步执行一个代码命令。

Once in the browser, you can execute any R command. For example, one might view the local environment by using ls(); or choose to set new variables, or change the values assigned to variables simply by using the standard methods for assigning values to variables. The browser also understands a small set of commands specific to it. Which leads us to a discussion on Finish and continue...

进入浏览器后,您可以执行任何R命令。例如,可以使用ls()查看本地环境;或者选择设置新变量,或者仅使用标准方法为变量赋值来更改分配给变量的值。浏览器还了解一小组特定的命令。这引导我们讨论完成并继续......

The subtlety in relation to Finish and continue is that:

与Finish和continue相关的微妙之处在于:

  • Finish, or f: finishes execution of the current loop or function.
  • 完成或f:完成当前循环或函数的执行。
  • Continue, c: leaves interactive debugging and continues regular execution of the function. This is useful if you’ve fixed the bad state and want to check that the function proceeds correctly.
  • 继续,c:离开交互式调试并继续定期执行该功能。如果您已修复了错误状态并想要检查函数是否正确进行,这将非常有用。

essentially, we talking about a subtlety in mode.

基本上,我们谈论模式的微妙。

在浏览器()中完成和继续有什么区别?

Browser / Recover Overview

At least for me, you have to view this in the context of debugging a program written in R. Specifically, how you might apply Finish and continue. I am sure many understand this, but I include for completeness as I personally really didn't for a long time.

至少对我来说,你必须在调试用R编写的程序的上下文中查看它。具体来说,你可以如何应用Finish并继续。我相信很多人都明白这一点,但我包括完整性,因为我个人真的没有很长时间。

  • browser allows you to look at the objects in the function in which the browser call is placed.
  • 浏览器允许您查看放置浏览器调用的函数中的对象。
  • recover allows you to look at those objects as well as the objects in the caller of that function and all other active functions.
  • recover允许您查看这些对象以及该函数调用者和所有其他活动函数中的对象。

Liberal use of browser, recover, cat and print while you are writing functions allows your expectations and R's expectations to converge.

在编写函数时,*使用浏览器,恢复,cat和print可以使您的期望和R的期望收敛。

A very handy way of doing this is with trace. For example, if browsing at the end of the myFun function is convenient, then you can do:

一个非常方便的方法是跟踪。例如,如果在myFun函数结束时浏览很方便,那么你可以这样做:

trace(myFun, exit=quote(browser()))

trace(myFun,exit = quote(browser()))

You can customize the tracing with a command like:

您可以使用以下命令自定义跟踪:

trace(myFun, edit=TRUE)

trace(myFun,edit = TRUE)

If you run into an error, then debugging is the appropriate action. There are at least two approaches to debugging. The first approach is to look at the state of play at the point where the error occurs. Prepare for this by setting the error option. The two most likely choices are:

如果遇到错误,则调试是适当的操作。至少有两种调试方法。第一种方法是查看发生错误时的播放状态。通过设置错误选项为此做好准备。最可能的两个选择是:

options(error=recover)

选项(错误恢复=)

or

要么

options(error=dump.frames)

选项(误差= dump.frames)

The difference is that with recover you are automatically thrown into debug mode, but with dump.frames you start debugging by executing:

不同之处在于,使用recover会自动进入调试模式,但是对于dump.frames,您可以通过执行以下命令开始调试:

debugger()

调试器()

In either case you are presented with a selection of the frames (environments) of active functions to inspect.

在任何一种情况下,您都会看到一系列要检查的活动功能的框架(环境)。

You can force R to treat warnings as errors with the command:

您可以强制R使用以下命令将警告视为错误:

options(warn=2)

选项(警告= 2)

If you want to set the error option in your .First function, then you need a trick since not everything is in place at the time that .First is executed:

如果你想在你的第一个函数中设置错误选项,那么你需要一个技巧,因为在执行第一个时并不是所有的东西都存在:

options(error=expression(recover()))

选项(误差=表达式(恢复()))

or

要么

options(error=expression(dump.frames()))

选项(误差=表达式(dump.frames()))

The second idea for debugging is to step through a function as it executes. If you want to step through function myfun, then do:

调试的第二个想法是在执行时逐步执行函数。如果你想逐步执行myfun函数,那么:

debug(myfun)

调试(myfun)

and then execute a statement involving myfun. When you are done debugging, do:

然后执行涉及myfun的声明。完成调试后,执行以下操作:

undebug(myfun)

undebug(myfun)

A more sophisticated version of this sort of debugging may be found in the debug package.

可以在调试包中找到更复杂的此类调试版本。

References:

参考文献:

  • R Inferno
    • http://www.burns-stat.com/pages/Tutor/R_inferno.pdf (Great Reference)
    • http://www.burns-stat.com/pages/Tutor/R_inferno.pdf(精彩参考)
    • Circle 8 - Believing It Does as Intended (Page 45).
    • 第8圈 - 相信它符合预期(第45页)。
    • Author: Patrick Burns
    • 作者:Patrick Burns
  • R Inferno http://www.burns-stat.com/pages/Tutor/R_inferno.pdf(精彩参考)第8圈 - 相信它符合预期(第45页)。作者:Patrick Burns
  • R Programming for Bioinformatics
    • Author: Robert Gentleman
    • 作者:罗伯特绅士
  • R生物信息学编程作者:Robert Gentleman

#3


0  

You can think of finish as a break in other languages. What happens is that you no longer care about the other items in the iteration because of a certain condition such as finding a specific item or an item that would cause an error.

您可以将完成视为其他语言的突破。发生的事情是您不再关心迭代中的其他项目,因为某些条件(例如查找特定项目或可能导致错误的项目)。

continue, on the other hand, will stop at the current line of the loop, ignore the rest of the code block, and continue to the next item in the iteration. You would use this option if you intend to go through every item in the iteration and just ignore the items that satisfy the condition to skip over.

另一方面,continue将停止在循环的当前行,忽略代码块的其余部分,并继续迭代中的下一个项目。如果您打算浏览迭代中的每个项目并忽略满足条件的项目,则可以使用此选项。

#1


9  

There is a small difference.

有一点不同。

  • c immediately exits the browser (and debug mode) and after that executes the rest of the code in the normal way.
  • c立即退出浏览器(和调试模式),之后以正常方式执行其余代码。
  • f on the contrary stays in the browser (and debug mode) while executing the rest of the function/loop. After the function/loop is finished, he also returns to the normal execution mode.
  • 相反,f在执行函数/循环的其余部分时保持在浏览器(和调试模式)中。函数/循环结束后,他也返回正常执行模式。

Source: R-source (line 1105-1117) and R-help

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

This has a few implications:

这有一些影响:

  • c closes the browser. This means that a new browser call is called from a function. Therefore you will see the line: Called from: function(). f on the other hand will not close the browser and therefore you will not see this line. The source code for this behavior is here: https://github.com/wch/r-source/....
  • c关闭浏览器。这意味着从函数调用新的浏览器调用。因此,您将看到以下行:来自:function()。另一方面,f不会关闭浏览器,因此你不会看到这一行。此行为的源代码如下:https://github.com/wch/r-source / ....
  • Because f stays in the browser, f also keeps track of the contextlevel:
  • 因为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)

浏览器提示符为Browse [n]>形式:此处var {n}表示“浏览器级别”。浏览时可以调用浏览器(通常是在使用调试时),每次递归调用都会增加数量。 (实际数字是上下文堆栈中'contexts'的数量:在外部浏览级别通常为2,在调试器中检查转储时为1)

These differences can be tested with the code:

可以使用以下代码测试这些差异:

> 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

As far as I see it, there is no practical difference between the two, unless there lies a practical purpose in the context stack. The debugging mode has no added value. The debug flag only opens the browser when you enter the function but since you are already inside the function, it will not trigger another effect.

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

#2


3  

Difference Between Browser and Continue

At least for me, I feel the answer can be mapped out as a table, however, let's first frame up the usage of browser(), for those who may not yet have encountered it.

至少对我来说,我觉得答案可以作为一个表格进行映射,但是,让我们首先考虑使用browser(),以便那些可能还没有遇到过它的人。

The browser function is the basis for the majority of R debugging techniques. Essentially, a call to browser halts execution and starts a special interactive session where you can inspect the current state of the computations and step through the code one command at a time.

浏览器功能是大多数R调试技术的基础。实际上,对浏览器的调用会停止执行并启动一个特殊的交互式会话,您可以在其中检查计算的当前状态并一次单步执行一个代码命令。

Once in the browser, you can execute any R command. For example, one might view the local environment by using ls(); or choose to set new variables, or change the values assigned to variables simply by using the standard methods for assigning values to variables. The browser also understands a small set of commands specific to it. Which leads us to a discussion on Finish and continue...

进入浏览器后,您可以执行任何R命令。例如,可以使用ls()查看本地环境;或者选择设置新变量,或者仅使用标准方法为变量赋值来更改分配给变量的值。浏览器还了解一小组特定的命令。这引导我们讨论完成并继续......

The subtlety in relation to Finish and continue is that:

与Finish和continue相关的微妙之处在于:

  • Finish, or f: finishes execution of the current loop or function.
  • 完成或f:完成当前循环或函数的执行。
  • Continue, c: leaves interactive debugging and continues regular execution of the function. This is useful if you’ve fixed the bad state and want to check that the function proceeds correctly.
  • 继续,c:离开交互式调试并继续定期执行该功能。如果您已修复了错误状态并想要检查函数是否正确进行,这将非常有用。

essentially, we talking about a subtlety in mode.

基本上,我们谈论模式的微妙。

在浏览器()中完成和继续有什么区别?

Browser / Recover Overview

At least for me, you have to view this in the context of debugging a program written in R. Specifically, how you might apply Finish and continue. I am sure many understand this, but I include for completeness as I personally really didn't for a long time.

至少对我来说,你必须在调试用R编写的程序的上下文中查看它。具体来说,你可以如何应用Finish并继续。我相信很多人都明白这一点,但我包括完整性,因为我个人真的没有很长时间。

  • browser allows you to look at the objects in the function in which the browser call is placed.
  • 浏览器允许您查看放置浏览器调用的函数中的对象。
  • recover allows you to look at those objects as well as the objects in the caller of that function and all other active functions.
  • recover允许您查看这些对象以及该函数调用者和所有其他活动函数中的对象。

Liberal use of browser, recover, cat and print while you are writing functions allows your expectations and R's expectations to converge.

在编写函数时,*使用浏览器,恢复,cat和print可以使您的期望和R的期望收敛。

A very handy way of doing this is with trace. For example, if browsing at the end of the myFun function is convenient, then you can do:

一个非常方便的方法是跟踪。例如,如果在myFun函数结束时浏览很方便,那么你可以这样做:

trace(myFun, exit=quote(browser()))

trace(myFun,exit = quote(browser()))

You can customize the tracing with a command like:

您可以使用以下命令自定义跟踪:

trace(myFun, edit=TRUE)

trace(myFun,edit = TRUE)

If you run into an error, then debugging is the appropriate action. There are at least two approaches to debugging. The first approach is to look at the state of play at the point where the error occurs. Prepare for this by setting the error option. The two most likely choices are:

如果遇到错误,则调试是适当的操作。至少有两种调试方法。第一种方法是查看发生错误时的播放状态。通过设置错误选项为此做好准备。最可能的两个选择是:

options(error=recover)

选项(错误恢复=)

or

要么

options(error=dump.frames)

选项(误差= dump.frames)

The difference is that with recover you are automatically thrown into debug mode, but with dump.frames you start debugging by executing:

不同之处在于,使用recover会自动进入调试模式,但是对于dump.frames,您可以通过执行以下命令开始调试:

debugger()

调试器()

In either case you are presented with a selection of the frames (environments) of active functions to inspect.

在任何一种情况下,您都会看到一系列要检查的活动功能的框架(环境)。

You can force R to treat warnings as errors with the command:

您可以强制R使用以下命令将警告视为错误:

options(warn=2)

选项(警告= 2)

If you want to set the error option in your .First function, then you need a trick since not everything is in place at the time that .First is executed:

如果你想在你的第一个函数中设置错误选项,那么你需要一个技巧,因为在执行第一个时并不是所有的东西都存在:

options(error=expression(recover()))

选项(误差=表达式(恢复()))

or

要么

options(error=expression(dump.frames()))

选项(误差=表达式(dump.frames()))

The second idea for debugging is to step through a function as it executes. If you want to step through function myfun, then do:

调试的第二个想法是在执行时逐步执行函数。如果你想逐步执行myfun函数,那么:

debug(myfun)

调试(myfun)

and then execute a statement involving myfun. When you are done debugging, do:

然后执行涉及myfun的声明。完成调试后,执行以下操作:

undebug(myfun)

undebug(myfun)

A more sophisticated version of this sort of debugging may be found in the debug package.

可以在调试包中找到更复杂的此类调试版本。

References:

参考文献:

  • R Inferno
    • http://www.burns-stat.com/pages/Tutor/R_inferno.pdf (Great Reference)
    • http://www.burns-stat.com/pages/Tutor/R_inferno.pdf(精彩参考)
    • Circle 8 - Believing It Does as Intended (Page 45).
    • 第8圈 - 相信它符合预期(第45页)。
    • Author: Patrick Burns
    • 作者:Patrick Burns
  • R Inferno http://www.burns-stat.com/pages/Tutor/R_inferno.pdf(精彩参考)第8圈 - 相信它符合预期(第45页)。作者:Patrick Burns
  • R Programming for Bioinformatics
    • Author: Robert Gentleman
    • 作者:罗伯特绅士
  • R生物信息学编程作者:Robert Gentleman

#3


0  

You can think of finish as a break in other languages. What happens is that you no longer care about the other items in the iteration because of a certain condition such as finding a specific item or an item that would cause an error.

您可以将完成视为其他语言的突破。发生的事情是您不再关心迭代中的其他项目,因为某些条件(例如查找特定项目或可能导致错误的项目)。

continue, on the other hand, will stop at the current line of the loop, ignore the rest of the code block, and continue to the next item in the iteration. You would use this option if you intend to go through every item in the iteration and just ignore the items that satisfy the condition to skip over.

另一方面,continue将停止在循环的当前行,忽略代码块的其余部分,并继续迭代中的下一个项目。如果您打算浏览迭代中的每个项目并忽略满足条件的项目,则可以使用此选项。