arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 1701.04781stat.CO
收藏

checkmate:防御性R编程的快速参数检查

checkmate: Fast Argument Checks for Defensive R Programming

Michel Lang

摘要:

像R这样的动态类型编程语言允许程序员编写通用,灵活和简洁的代码,并使用交互式Read-eval-print-loop(REPL)与语言交互。然而,这种灵活性有其代价:由于R解释器没有关于预期变量类型的信息,许多基函数会自动转换输入,而不是引发异常。不幸的是,这经常会导致调用堆栈更深处的运行时错误,从而混淆了原始问题并使调试具有挑战性。更糟糕的是,不需要的转换可能仍然未被检测到,并使统计分析的结果偏斜或无效。作为一种手段,可以使用断言来检测运行时期间的意外输入,并发出可理解和可跟踪的错误信号。“checkmate”包提供了大量的函数来检查最常用的R对象和变量类型的类型和相关属性。该包主要是用C编写的,以避免任何不必要的性能开销。因此,程序员可以方便地编写简洁、经过良好测试的断言,对于许多应用程序来说,这些断言的性能优于自定义R代码。此外,checkmate通过扩展大量额外的期望函数,简化了使用框架“testthat”编写单元测试,并且注册的C例程可供包开发人员在编译代码中对任意SEXP(R对象的内部数据结构,在C中实现为struct)执行断言。

英文摘要:

Dynamically typed programming languages like R allow programmers to write generic, flexible and concise code and to interact with the language using an interactive Read-eval-print-loop (REPL). However, this flexibility has its price: As the R interpreter has no information about the expected variable type, many base functions automatically convert the input instead of raising an exception. Unfortunately, this frequently leads to runtime errors deeper down the call stack which obfuscates the original problem and renders debugging challenging. Even worse, unwanted conversions can remain undetected and skew or invalidate the results of a statistical analysis. As a resort, assertions can be employed to detect unexpected input during runtime and to signal understandable and traceable errors. The package "checkmate" provides a plethora of functions to check the type and related properties of the most frequently used R objects and variable types. The package is mostly written in C to avoid any unnecessary performance overhead. Thus, the programmer can conveniently write concise, well-tested assertions which outperforms custom R code for many applications. Furthermore, checkmate simplifies writing unit tests using the framework "testthat" by extending it with plenty of additional expectation functions, and registered C routines are available for package developers to perform assertions on arbitrary SEXPs (internal data structure for R objects implemented as struct in C) in compiled code.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑