196 Pages 10 B/W Illustrations
    by Chapman & Hall

    196 Pages 10 B/W Illustrations
    by Chapman & Hall

    Continue Shopping

    Learn how to write R code with fewer bugs.

    The problem with programming is that you are always one typo away from writing something silly. Likewise with data analysis, a small mistake in your model can lead to a big mistake in your results. Combining the two disciplines means that it is all too easy for a missed minus sign to generate a false prediction that you don’t spot until it’s too late. Testing is the only way to be sure that your code, and your results, are correct.

    Testing R Code teaches you how to perform development-time testing using the testthat package, allowing you to ensure that your code works as intended. The book also teaches run-time testing using the assertive package; enabling your users to correctly run your code.

    After beginning with an introduction to testing in R, the book explores more advanced cases such as integrating tests into R packages; testing code that accesses databases; testing C++ code with Rcpp; and testing graphics. Each topic is explained with real-world examples, and has accompanying exercises for readers to practise their skills — only a small amount of experience with R is needed to get started!



    Run-time testing with assertive

    Development-time testing with testthat

    Writing easily maintainable and testable code

    Integrating testing into your packages

    Writing your own assertions and expectations

    Answers to exercises


    Richard Cotton

    "This timely book of about 180 pages by Richard Cotton provides detailed, hands-on instructions on how to improve the correctness, stability, and user-friendliness of R scripts and packages through testing.
    Written in an entertaining and informal style … [a]ll concepts and functions used in the book are introduced and demonstrated with real code, which should be a very valuable resource to readers and helps keep one engaged throughout. …
    All in all, this book addresses two very important topics—how to implement reliable, flexible input checks, and human-readable error messages and how to formalize the expected behavior of an R package—in a well-structured, hands-on, and eminently readable manner. It is a timely and important addition to the literature on programming with R, with lots of motivating examples. I look forward to teaching proper testing procedures in my advanced R programming classes based on it."
    —Fabian Scheipl, Ludwig-Maximilians-University Munich, in Biometrical Journal, August 2017

    "…provides all the guidance you’ll need to write robust, correct code in the R language…useful advice for organizing and writing code that's more maintainable in the long run…Testing is a topic that doesn't get as much attention as it deserves in data science disciplines. One reason may be that it's a fairly dry topic, but Cotton does a good job in making the material engaging with practical examples and regular exercises (with answers in the appendix). Frequent (and often amusing) footnotes help make this an entertaining read (given the material) and hopefully will motivate you to make testing a standard (and early) part of your R programming process."
    —David Smith, Microsoft Research, on Revolutions, March 2017

    "When it comes to getting things right in data science, most of the focus goes to the data and the statistical methodology used. But when a misplaced parenthesis can throw off your results entirely, ensuring correctness in your programming is just as important. This book provides all the guidance you’ll need to write robust, correct code in the lingua franca of data science, R. These practical techniques will help you implement development-time and run-time checks in your code, and the worked examples will get you up to speed quickly. And with the confidence that your code is actually doing what it’s supposed to, you can look forward to more maintainable and — most importantly — reliable results."
    David Smith, Microsoft Research

    "This book will teach you to test both your analysis code and your functions. Richie is a great teacher: the book is approachable and fun, and you'll be able to immediately apply what you learn. Along the way, you'll also get some great tips about writing high-quality code in R."
    Hadley Wickham (RStudio)

    "Writing clear, reliable tests is an essential part of programming in R. In this book, Richie Cotton covers the various approaches you can take - approaches I use in writing my R packages and production-ready code - in a useful, comprehensive and eminently readable manner."
    Oliver Keyes (ironholds.org, @quominus)

    "In short, I loved it. There is a dearth of good material (or any material, really) on this topic so I'm excited to say that 'Testing R Code' met my high expectations. I know the subject rather well but, not only did I see topics that were explained in new and easier ways than I have previously seen, but I even learned quite a bit of new information myself. It is obvious that the author knows this topic inside and out."
    Tony Fischetti, Rensselaer Polytechnic Institute