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. 设备没有可用空间 /var/spool/clientmqueue sendmail

    [root@hadoop3 /]# crontab -e/tmp/crontab.TB7A7w: 设备上没有空间[root@hadoop3 /]# df -Bg文件系统 1G-块 已用 可用 已用% ...

  2. EJB3.0

    由于EJB2.0的复杂性,在Spring和Hibernate[1]  等轻量级框架出现后,大量的用户转向应用轻量级框架.在大家的呼声中, EJB 期待已久的EJB3.0规范终于发布了.在本文中将对新的 ...

  3. scikit-learn 机器学习工具

    1.http://scikit-learn.org/stable/        官网:关于scikit-learn介绍等 2. http://stackoverflow.com/questions/ ...

  4. leetcode 400 Add to List 400. Nth Digit

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note:n is ...

  5. vsftp 777权限

    1. setsebool -P ftpd_disable_trans 1 2. service vsftpd restart

  6. sabaki and leelazero

    https://tieba.baidu.com/p/5462772621?see_lz=1 http://zero.sjeng.org/ https://www.jianshu.com/p/a4ba1 ...

  7. 从0开始学习Hadoop(1) 环境准备 Win7环境+VirtureBox+Ubuntu

    虚拟机:VirtureBox 3.18 下载地址: https://www.virtualbox.org/ 操作系统:Ubuntu  版本:ubuntu-15.04-desktop-amd64.iso ...

  8. Ueditor中代码的高亮和背景在前端页面的实现

    首先废话就不多说,这个富文本编辑器的下载和js等基本文件的导入略. 我的最终目标是这样的,我们在页面中的富文本框中输入代码,希望它能够被后台接受.存入数据库,当通过服务器将这些代码再一次显示在前台的页 ...

  9. maven仓库错误

    1.jai_core 时间:2018年3月21日10:04:57 URL:https://mvnrepository.com/artifact/javax.media/jai_core/1.1.3 / ...

  10. bzoj 2456: mode【瞎搞】

    这题加个#include都会MLE-- 神思路,每个数抵消宇哥和它不同的数,最后剩下的就是众数 #include<cstdio> int n,la,x,tot; int main() { ...