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. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...

  2. 用javascript写一个前端等待控件

    前端等待控件有啥新奇的?什么jquery啦,第三方控件啦,好多好多,信手拈来. 因为项目使用了bootstrap的原因,不想轻易使用第三方,怕不兼容.自己写一个. 技术点包括动态加载CSS,javas ...

  3. POI中HSSF和XSSF操作Excel

    POI中HSSF和XSSF操作Excel   在公司实习快一个月了,这段时间公司业务要用JAVA操作复杂的Excel报表.刚开始的Excel还好,没有涉及到复杂的图表,所以使用JXL操作Excel,但 ...

  4. HDU - 4333 Revolving Digits(拓展kmp+最小循环节)

    1.给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s.求这个过程中比原来的数字小的.相等的.大的数字各有多少. 例如:字符串123,变换过程:123 -> ...

  5. java中字节数组byte[]和字符(字符串)之间的转换

    转自:http://blog.csdn.net/linlzk/article/details/6566124 Java与其他语言编写的程序进行tcp/ip socket通讯时,通讯内容一般都转换成by ...

  6. [Usaco2017 Dec] A Pie for a Pie

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5140 [算法] 最短路 时间复杂度 : O(N^2) [代码] #include&l ...

  7. x86 linux 裁剪过程中能正常跑起来的必要配置项

    A .选中Executable file formats/Emulations ---> Kernel support for ELFbinaries -----加载运行rootfs 中的程序. ...

  8. 数据库无法访问,用户 NT AUTHORITY/SYSTEM或NT AUTHORITY\NETWORK SERVICE登录失败的解决办法

    问题:win7中的在IIS 7.0中,在 Default Web Site 目录下挂一虚拟目录. 在相应的应用程序池 DefaultAppPool 设置标识设置成NetworkService. 但是打 ...

  9. win8 使用notepad++写C代码

    1. 安装mingw,这里有个不错的教程 http://www.metsky.com/archives/588.html 2. 在notepad++里做设置, 安装nppexec: nppexec-& ...

  10. Linux 常用命令十一 ps

    一.ps命令 Linux中的ps命令是Process Status的缩写. ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要 ...