R Programming week 3-Debugging
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的更多相关文章
- Coursera系列-R Programming第二周
博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html --- 好久没发博客 且容我大吼一句 终于做完这周R Progra ...
- Coursera系列-R Programming第三周-词法作用域
完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...
- 让reddit/r/programming炸锅的一个帖子,还是挺有意思的
这是原帖 http://www.reddit.com/r/programming/comments/358tnp/five_programming_problems_every_software_en ...
- [R] [Johns Hopkins] R Programming -- week 3
library(datasets) head(airquality) #按月分組 s <- split(airquality, airquality$Month) str(s) summary( ...
- [R] [Johns Hopkins] R Programming 作業 Week 2 - Air Pollution
Introduction For this first programming assignment you will write three functions that are meant to ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- 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 ...
- [R] [Johns Hopkins] R Programming -- week 4
#Generating normal distribution (Pseudo) random number x<-rnorm(10) x x2<-rnorm(10,2,1) x2 set ...
- 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 ...
随机推荐
- 【bzoj4320】ShangHai2006 Homework
若Y小于等于sqrt(300000),暴力,对所有的插入的数都更新mn[i]. 若Y大于sqrt(300000),枚举kY,用并查集维护>=i的第一个数,这样只支持删除操作是O(1),然后倒着枚 ...
- 从远程Linux Copy文件到本机 界面化操作
1.安装SSHSecureShellClient 2.打开 3.设置1,然后打开2就可以操作了
- go---weichart个人对Golang中并发理解
个人觉得goroutine是Go并行设计的核心,goroutine是协程,但比线程占用更少.golang对并发的处理采用了协程的技术.golang的goroutine就是协程的实现. 十几个gorou ...
- 使用Kotlin如何startActivity
没错,就是这么简单的一个功能,不过由于初学kotlin,所以找了很久才找到如何写,所以还是贴出来给需要的人吧,上代码: startActivity(Intent(MainActivity@this, ...
- BZOJ2843:极地旅行社(LCT入门题)
不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服 务.当地最受欢迎的当然是帝企鹅了,这些小家伙经常成群结队的游走在各个冰岛之间.Mirko的旅 ...
- [Usaco2009 MAR] Earthquake Damage 2
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1585 [算法] 一个最小割的经典模型 , 详见代码 时间复杂度 : O(dinic( ...
- python-----群发图片
使用wxpy库给3个人群发同一张图片 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/2/22 15:25 # @Author ...
- bzoj3631 [JLOI2014]松鼠的新家——树上差分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3631 树上差分:注意路径的结尾被多算了一次,最后要减去(不能提前减). 代码如下: #inc ...
- sqlserver2008Mail
use msdb GO DROP PROC MailTim GO CREATE PROC MailTim @Subject NVARCHAR(100),@Body NVARCHAR( ...
- 用jQuery插件来提升SharePoint列表表单用户体验
本文将描述如何通过简单的CSS和jQuery插件提升SharePoint默认的列表表单体验.这些小技巧并不仅仅改善了外观,还提升了可用性. 剩余字数 我们以通知列表为例.通知正文字段假设要求不应该超过 ...