且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅在满足条件时启动调试器

更新时间:2022-01-27 21:32:30

您可以在 browser()中使用参数 expr :

fun <- function(x, y, n) {
  result <- 0
  for (i in 1:n) {
    browser(expr = {i == 5})
    result <- result + i * ( x + y)
  }
  return(result)
}

如果表达式的计算结果为 TRUE ,则它将仅打开调用 browser()的环境.

It will then only open the environment where browser() was called from if the expression evaluates to TRUE.

如果要使用 debug():

debug(fun, condition = i == 5)

然后调用该函数:

fun <- function(x, y, n) {
  result <- 0
  for (i in 1:n) {
    result <- result + i * ( x + y)
  }
  return(result)
}

fun(x, y, n)