Something’s Wrong!

Indications that something’s not right

message: A generic notification/diagnostic message produced by the message function;execution of the function continues

warning: An indication that something is wrong but not necessarily fatal; execution of thefunction continues; generated by the warning function

error: An indication that a fatal problem has occurred; execution stops; produced by the stop function

condition: A generic concept for indicating that something unexpected can occur; programmers can create their own conditions

How do you know that something is wrong with your function?

What was your input?

How did you call the function?

What were you expecting? Output, messages, other results?

What did you get?

How does what you get differ from what you were expecting?

Were your expectations correct in the first place?

Can you reproduce the problem (exactly)?

Debugging Tools in R

The primary tools for debugging functions in R are:

traceback: prints out the function call stack after an error occurs; does nothing if there’s no error

debug: flags a function for “debug” mode which allows you to step through execution of a function one line at a time

browser: suspends the execution of a function wherever it is called and puts the function in debug mode

trace: allows you to insert debugging code into a function a specific places

recover: allows you to modify the error behavior so that you can browse the function call stack

These are interactive tools specifically designed to allow you to pick through a function. There’s also the more blunt technique of inserting print/cat statements in the function.

traceback

> lm(y ~ x)

Error in eval(expr, envir, enclos) : object ’y’ not found

> traceback()

7: eval(expr, envir, enclos)

6: eval(predvars, data, env)

5: model.frame.default(formula = y ~ x, drop.unused.levels = TRUE)

4: model.frame(formula = y ~ x, drop.unused.levels = TRUE)

3: eval(expr, envir, enclos)

2: eval(mf, parent.frame())

1: lm(y ~ x)

debug

> debug(lm)

> lm(y ~ x)

debugging in: lm(y ~ x)

debug: {

ret.x <- x

ret.y <- y

cl <- match.call()

...

if (!qr)

z$qr <- NULL

z

}

Browse[

2]>

Browse[2]> n

debug: ret.x <- x

Browse[2]> n

debug: ret.y <- y

Browse[2]> n

debug: cl <- match.call()

Browse[2]> n

debug: mf <- match.call(expand.dots = FALSE)

Browse[2]> n

debug: m <- match(c("formula", "data", "subset", "weights", "na.action",

"offset"), names(mf), 0L)

recover

> options(error = recover)

> read.csv("nosuchfile")

Error in file(file, "rt") : cannot open the connection

In addition: Warning message:

In file(file, "rt") :

cannot open file ’nosuchfile’: No such file or directory

Enter a frame number, or 0 to exit

1: read.csv("nosuchfile")

2: read.table(file = file, header = header, sep = sep, quote = quote, dec =

3: file(file, "rt")

Selection:

Summary

There are three main indications of a problem/condition: message, warning, error- only an error is fatal

When analyzing a function with a problem, make sure you can reproduce the problem, clearly state your expectations and how the output differs from your expectation

Interactive debugging tools traceback, debug, browser, trace, and recover can be used to find problematic code in functions

Debugging tools are not a substitute for thinking!

欢迎关注:

R Programming week 3-Debugging的更多相关文章

  1. Coursera系列-R Programming第二周

    博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html  --- 好久没发博客 且容我大吼一句 终于做完这周R Progra ...

  2. Coursera系列-R Programming第三周-词法作用域

    完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...

  3. 让reddit/r/programming炸锅的一个帖子,还是挺有意思的

    这是原帖 http://www.reddit.com/r/programming/comments/358tnp/five_programming_problems_every_software_en ...

  4. [R] [Johns Hopkins] R Programming -- week 3

    library(datasets) head(airquality) #按月分組 s <- split(airquality, airquality$Month) str(s) summary( ...

  5. [R] [Johns Hopkins] R Programming 作業 Week 2 - Air Pollution

    Introduction For this first programming assignment you will write three functions that are meant to ...

  6. R Programming week 3-Loop functions

    Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...

  7. R programming, In ks.test(x, y) : p-value will be approximate in the presence of ties

    Warning message: In ks.test(x, y) : p-value will be approximate in the presence of ties   The warnin ...

  8. [R] [Johns Hopkins] R Programming -- week 4

    #Generating normal distribution (Pseudo) random number x<-rnorm(10) x x2<-rnorm(10,2,1) x2 set ...

  9. R Programming week2 Functions and Scoping Rules

    A Diversion on Binding Values to Symbol When R tries to bind a value to a symbol,it searches through ...

随机推荐

  1. 最新Bootstrap手册

    http://www.jqhtml.com/bootstraps-syntaxhigh/index.html

  2. HDU 5405 Sometimes Naive 树链剖分+bit*****

    Sometimes Naive Problem Description   Rhason Cheung had a naive problem, and asked Teacher Mai for h ...

  3. Hibernate 之 HQL

    通过对Hibernate框架的学习,已经慢慢的对Hibernate有了进一步的了解,接下来我们要说的是HibernateQusery Language(HQL),如果你正在学习SSH框架,那SQL对你 ...

  4. Android图片载入缓存框架Glide

    Glide开源框架是Google推荐的图片载入和缓框架,其在Github上的开源地址是:https://github.com/bumptech/glide 当然一个Google推荐的框架肯定就是Vol ...

  5. adbi命令【转】

    本文转载自:https://zmywly8866.github.io/2015/01/24/all-adb-command.html   ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列 ...

  6. Ruby: 获取IE的一些信息(其实应用AutoIt脚本本身,获取这些信息更加简单)

    require'win32/registry' hkey_local_machine=Win32::Registry::HKEY_LOCAL_MACHINE defgetKeyValue(hive, ...

  7. bzoj4806 炮——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4806 看到这题首先会想到状压什么乱七八糟的,然而很难做: 其实,因为求的是方案数,所以并不需 ...

  8. 分析函数Ratio_to_report使用

    分析函数Ratio_to_report( ) over()使用说明 表中需要计算单项占比:比如单项在部门占比多少,单项在公司占比多少.特别是在财务单项计算,部门个人薪水计算上. Ratio_to_re ...

  9. 简单记录CentOS服务器配置JDK+Tomcat+MySQL

    项目需要部署到一台CentOS的服务器之上,之前这台服务器上面已经安装了一个Nginx和MySQL,跑的是PHP环境,有一个项目正在运行.而我们最新的项目是用Java写的,服务器是用的Tomcat,所 ...

  10. 【洛谷2304_LOJ2134】[NOI2015]小园丁与老司机(动态规划_网络流)

    题目: 洛谷 2304 LOJ 2134 (LOJ 上每个测试点有部分分) 写了快一天 -- 好菜啊 分析: 毒瘤二合一题 -- 注意本题(及本文)使用 \(x\) 向右,\(y\) 向上的「数学坐标 ...