Muutke küpsiste eelistusi

Testing R Code [Kõva köide]

  • Formaat: Hardback, 196 pages, kõrgus x laius: 234x156 mm, kaal: 432 g, 10 Illustrations, black and white
  • Sari: Chapman & Hall/CRC The R Series
  • Ilmumisaeg: 19-Jan-2017
  • Kirjastus: Chapman & Hall/CRC
  • ISBN-10: 1498763650
  • ISBN-13: 9781498763653
  • Formaat: Hardback, 196 pages, kõrgus x laius: 234x156 mm, kaal: 432 g, 10 Illustrations, black and white
  • Sari: Chapman & Hall/CRC The R Series
  • Ilmumisaeg: 19-Jan-2017
  • Kirjastus: Chapman & Hall/CRC
  • ISBN-10: 1498763650
  • ISBN-13: 9781498763653
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 dont spot until its 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!

Arvustused

"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 topicshow to implement reliable, flexible input checks, and human-readable error messages and how to formalize the expected behavior of an R packagein 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 youll need to write robust, correct code in the R languageuseful advice for organizing and writing code that's more maintainable in the long runTesting 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 youll 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 its 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

Preface xiii
1 Introduction
1(8)
1.1 Interacting vs. Programming
1(1)
1.2 Two Kinds of Testing
2(1)
1.3 How Run-Time Testing Will Help You
2(2)
1.4 How Development-Time Testing Will Help You
4(3)
1.5 Summary
7(2)
2 Run-Time Testing with assertive
9(30)
2.1 Using Assertions
9(2)
2.1.1 Chaining Assertions Together with Pipes
10(1)
2.2 Using Predicates: is and has Functions
11(3)
2.2.1 Exercise: Using Predicates and Assertions
13(1)
2.3 The Virtual Package System
14(1)
2.4 A Tour of the assertive Package
14(18)
2.4.1 assertive.base
14(2)
2.4.2 assertive.properties
16(3)
2.4.3 assertive.types
19(1)
2.4.4 assertive.numbers
20(2)
2.4.5 Exercise: Examining an Object
22(1)
2.4.6 assertive.files
22(1)
2.4.7 Exercise: Inspecting Files and Directories
23(1)
2.4.8 assertive.strings
24(1)
2.4.9 assertive.matrices
25(1)
2.4.10 Exercise: Testing Properties of Matrices
25(1)
2.4.11 assertive.sets
25(1)
2.4.12 assertive.models
26(1)
2.4.13 assertive.reflection
27(1)
2.4.14 Exercise: Explore Your R Setup
28(1)
2.4.15 assertive.datetimes
28(1)
2.4.16 assertive.data, assertive.data.us, and assertive.data.uk
29(1)
2.4.17 Exercise: Checking Customer Data
30(1)
2.4.18 assertive.code
31(1)
2.5 Controlling Severity
32(1)
2.6 Fail Early, Fail Often
33(1)
2.7 Case Study: Calculating the Geometric Mean
34(3)
2.7.1 Exercise: Calculating the Harmonic Mean
37(1)
2.8 Alternatives
37(1)
2.9 Summary
38(1)
3 Development-Time Testing with testthat
39(24)
3.1 Using Unit Tests
39(1)
3.2 The Structure of a Unit Test
39(3)
3.2.1 Exercise: Using expect_equal
40(1)
3.2.2 How Equal Is Equal?
41(1)
3.3 Testing Errors
42(1)
3.3.1 Exercise: Using expect_error
42(1)
3.4 Different Types of Expectation
42(1)
3.5 Testing Warnings and Messages
43(3)
3.5.1 Chaining Expectations Together with Pipes
44(1)
3.5.2 Exercise: Using expect_output
45(1)
3.5.3 Testing for Lack of Output
45(1)
3.6 Varying the Strictness of Expectations
46(1)
3.7 Providing Additional Information on Failure
47(1)
3.8 Case Study: Calculating Square Roots
48(6)
3.8.1 Exercise: Find More Tests for square_root
53(1)
3.9 Other Expectations
54(3)
3.9.1 Exercise: Testing the Return Type of Replicates
56(1)
3.10 Organising Tests Using Contexts
57(1)
3.11 Running Your Tests
57(2)
3.12 Customizing How Test Results Are Reported
59(1)
3.13 Alternatives
60(1)
3.14 Summary
61(2)
4 Writing Easily Maintainable and Testable Code
63(22)
4.1 Don't Repeat Yourself
63(9)
4.1.1 Case Study: Drawing Lots of Plots
64(4)
4.1.2 Idea 1: Use Variables Rather Than Hard-Coded Values
68(1)
4.1.3 Idea 2: For Values That You Want To Change Everywhere, Update Global Settings
69(1)
4.1.4 Idea 3: Wrap the Contents into a Function
70(1)
4.1.5 Exercise: Reducing Duplication
71(1)
4.2 Keep It Simple, Stupid
72(12)
4.2.1 Simplifying Function Interfaces
72(2)
4.2.2 Idea 1: Pass Arguments for Advanced Functionality to Another Function
74(2)
4.2.3 Exercise: Outsourcing Argument Checking
76(1)
4.2.4 Idea 2: Having Wrapper Functions for Specific Use Cases
76(1)
4.2.5 Exercise: Wrappers for Formatting Currency
77(1)
4.2.6 Idea 3: Auto-Guessing Defaults
77(1)
4.2.7 Exercise: Providing Better Defaults for write.csv
78(1)
4.2.8 Idea 4: Split Functionality into Many Functions
79(1)
4.2.9 Exercise: Decomposing the quantile Function
80(1)
4.2.10 Cyclomatic Complexity
80(3)
4.2.11 How to Reduce Cyclomatic Complexity
83(1)
4.2.12 Exercise: Calculating Leap Years
83(1)
4.3 Summary
84(1)
5 Integrating Testing into Packages
85(14)
5.1 How to Make an R Package
85(2)
5.1.1 Prerequisites
85(1)
5.1.2 The Package Directory Structure
86(1)
5.1.3 Including Tests in Your Package
86(1)
5.2 Case Study: The hypotenuser Package
87(3)
5.3 Checking Packages
90(2)
5.3.1 Exercise: Make a Package with Tests
91(1)
5.4 Using Version Control, Online Package Hosting, and Continuous Integration
92(3)
5.4.1 Version Control with git
92(1)
5.4.2 Online Project Hosting
92(1)
5.4.3 Continuous Integration Services
93(2)
5.5 Testing Packages on CRAN
95(2)
5.5.1 Testing Packages with r-hub
96(1)
5.6 Calculating Test Coverage Using coveralls.io
97(1)
5.7 Summary
98(1)
6 Advanced Development-Time Testing
99(28)
6.1 Code with Side Effects
99(2)
6.1.1 Exercise: Writing a Test That Handles Side Effects
101(1)
6.2 Testing Complex Objects
101(3)
6.2.1 Exercise: Testing a Complex Object
104(1)
6.3 Testing Database Connections
104(6)
6.3.1 Option 1: Mock the Connection
106(1)
6.3.2 Option 2: Mock the Connection Wrapper
107(1)
6.3.3 Option 3: Mock the Specific Query Functions
108(2)
6.3.4 Summarizing the Pros and Cons of Each Database Method
110(1)
6.4 Testing Rcpp Code
110(8)
6.4.1 Getting Set Up to Use C++
111(3)
6.4.2 Case Study: Extending the hypotenuser Package to Include C++ Code
114(3)
6.4.3 Exercise: Testing an Rcpp Function
117(1)
6.5 Testing Write Functions
118(2)
6.5.1 Exercise: Writing INI Configuration Files
120(1)
6.6 Testing Graphics
120(5)
6.6.1 Generating the Report from the markdown
124(1)
6.6.2 Including Graphics Tests in Packages
124(1)
6.6.3 Exercise: Write a Graphics Test Report
124(1)
6.7 Summary
125(2)
7 Writing Your Own Assertions and Expectations
127(12)
7.1 The Quick, Nearly Good Enough Option
127(1)
7.2 Writing Scalar Predicates
128(2)
7.2.1 Exercise: Writing a Custom Scalar Predicate
129(1)
7.2.2 Writing Type Predicates
129(1)
7.3 Writing Scalar Assertions
130(1)
7.3.1 Exercise: Writing a Custom Scalar Assertion
131(1)
7.4 Writing Vector Predicates
131(2)
7.4.1 Exercise: Writing a Vector Predicate
132(1)
7.5 Writing Vector Assertions
133(2)
7.5.1 Exercise: Writing a Vector Assertion
134(1)
7.6 Creating Custom Expectations
135(2)
7.6.1 Exercise: Create a Custom Expectation
137(1)
7.7 Summary
137(2)
A Answers to Exercises
139(34)
A.1 Preface
139(1)
A.1.1 Exercise: Are You Ready?
139(1)
A.2
Chapter 2
139(10)
A.2.1 Exercise: Using Predicates and Assertions
139(2)
A.2.2 Exercise: Examining an Object
141(1)
A.2.3 Exercise: Inspecting Files and Directories
142(1)
A.2.4 Exercise: Testing Properties of Matrices
143(1)
A.2.5 Exercise: Explore Your R Setup
143(1)
A.2.6 Exercise: Checking Customer Data
143(4)
A.2.7 Exercise: Calculating the Harmonic Mean
147(2)
A.3
Chapter 3
149(4)
A.3.1 Exercise: Using expect_equal
149(1)
A.3.2 Exercise: Using expect_error
150(1)
A.3.3 Exercise: Using expect_output
151(1)
A.3.4 Exercise: Find More Tests for square_root
151(1)
A.3.5 Exercise: Testing the Return Type of Replicates
152(1)
A.4
Chapter 4
153(6)
A.4.1 Exercise: Reducing duplication
153(2)
A.4.2 Exercise: Outsourcing Argument Checking
155(1)
A.4.3 Exercise: Wrappers for Formatting Currency
156(1)
A.4.4 Exercise: Providing Better Defaults for write.csv
157(1)
A.4.5 Exercise: Decomposing the quantile Function
157(1)
A.4.6 Exercise: Calculating Leap Years
158(1)
A.5
Chapter 5
159(3)
A.5.1 Exercise: Make a Package with Tests
159(3)
A.6
Chapter 6
162(5)
A.6.1 Exercise: Writing a Test That Handles Side Effects
162(1)
A.6.2 Exercise: Testing a Complex Object
162(1)
A.6.3 Exercise: Testing an Rcpp Function
163(2)
A.6.4 Exercise: Writing INI Configuration Files
165(2)
A.6.5 Exercise: Write a Graphics Test Report
167(1)
A.7
Chapter 7
167(6)
A.7.1 Exercise: Writing a Custom Scalar Predicate
167(1)
A.7.2 Exercise: Writing a Custom Scalar Assertion
168(1)
A.7.3 Exercise: Writing a Vector Predicate
169(1)
A.7.4 Exercise: Writing a Vector Assertion
169(1)
A.7.5 Exercise: Create a Custom Expectation
170(3)
Bibliography 173(4)
Concept Index 177(2)
Package Index 179(2)
Dataset Index 181(2)
People Index 183(2)
Function Index 185
Richard Cotton