R语言笔记五

本文主要探讨了在R语言中如何诊断和解决编程问题。通过分析message、warning和error的区别,强调了错误再现的重要性。文章介绍了基本的调试工具,如traceback用于查看错误调用栈,debug用于步入式调试,browser暂停函数执行并进入调试模式,以及trace和recover帮助定位问题代码。总结指出,理解问题、明确预期与使用调试工具相辅相成,是有效调试的关键。
摘要由CSDN通过智能技术生成

Debugging

Diagnosing the problem

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 the
    function 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

    Warning

>log(-1)
  ## Warning: NaNs produced
  ## [1] NaN
>printmessage <- function(x) {
         if(x > 0)
              print("x is greater than zero")
        else
            print("x is less than or equal to zero")
 invisible(x)
}

printmessage <- function(x) {
       if (x > 0)
             print("x is greater than zero") 
             else print("x is less than or equal to zero")
      invisible(x)
}
 printmessage(1)
 ## [1] "x is greater than zero"

printmessage(NA)
 ## Error: missing value where TRUE/FALSE needed

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值