9.2 调试/不调试

Keywords:

Next: ,Previous: browser,Up: Debugging

9.2 调试/不调试

可以通过 debug(fun) 对任何函数调用调试器。随后,只要该函数求值,调试器都会被调用。调试器允许你控制函数主体里面的语句求值。在任一语句求值前,该语句会被打印出来并且给出一个特别的提示。可以给定任何命令,包括前面表格中有特殊意义的命令。

通过调用undebug(以函数作为参数)来关闭调试。

     > debug(mean.default)

     > mean(1:10)

     debugging in: mean.default(1:10)

     debug: {

         if (na.rm)

             x <- x[!is.na(x)]

         trim <- trim[1]

         n <- length(c(x, recursive = TRUE))

         if (trim > 0) {

             if (trim >= 0.5)

                 return(median(x, na.rm = FALSE))

             lo <- floor(n * trim) + 1

             hi <- n + 1 - lo

             x <- sort(x, partial = unique(c(lo, hi)))[lo:hi]

             n <- hi - lo + 1

         }

         sum(x)/n

     }

     Browse[1]>

     debug: if (na.rm) x <- x[!is.na(x)]

     Browse[1]>

     debug: trim <- trim[1]

     Browse[1]>

     debug: n <- length(c(x, recursive = TRUE))

     Browse[1]> c

     exiting from: mean.default(1:10)

     [1] 5.5

Hits:Loading...

special topic