Functional programming and unit testing for data munging with R


This book is no longer available for sale.

Functional programming and unit testing for data munging with R

About the Book

Most of the time spent working on a project is dedicated to preparing data for analysis. In this book, you will learn how to make this very important part of your work more streamlined and reproducible. We will learn about functional programming, unit testing and documenting your code in an automated way with the R programming language. The end goal is to write a package that contains all the needed functions to make your data tidy. This package will be extremely specific to your own needs and data, so you probably will not want to publish it on CRAN, however you can easily share this package with your colleagues or with your future self. This is especially useful if you are part of a project where you get yearly, quarterly or monthly data and need to repeat the same tasks over and over again. Having a thouroughly tested package makes sure that your fresh data gets tidied without any nasty surprises!

The book is still a work in progress. You can read it for free at You can show your support by buying it on leanpub for any price, but the version on leanpub will not be always up-to-date. This is because I use bookdown to write the book, and converting the html files generated by bookdown to leanpub's markdown takes some time. However, once I'll finish the book, the version on leanpub will of course be completed and well formatted.

About the Author

Bruno Rodrigues, Phd
Bruno Rodrigues, Phd

Hi I'm Bruno and have been programming with the R language in a professional setting as well as a hobby for quite some time now. In my free time I'm either taking care of my family, working out, or reading ancient philosophy.

Table of Contents

  • Chapter 1 Why this book?
    • 1.1 Important notice
    • 1.2 Motivation
    • 1.3 Who am I?
    • 1.4 Thanks
    • 1.5 License
  • Chapter 2 Introduction
    • 2.1 Getting R
    • 2.2 A short overview of functional programming
    • 2.3 A short overview of unit testing
    • 2.4 General recommendations to follow this book
  • Chapter 3 Functional Programming
    • 3.1 Introduction
    • 3.2 Mapping and Reducing: the base way
    • 3.3 Mapping and Reducing: the purrr way
    • 3.4 Basic anonymous functions
    • 3.5 Wrap-up
    • 3.6 Exercises
  • Chapter 4 The tidyverse
    • 4.1 Smoking is bad for you, but pipes are your friend
    • 4.2 Getting data into R with readr, readxl, haven and what are tibbles
    • 4.3 Transforming your data with dplyr
    • 4.4 Tidy your data with tidyr
    • 4.5 Functional programming with purrr and purrrlyr
    • 4.6 Special packages for special kinds of data: forcats, lubridate, and stringr
    • 4.7 Exercises
  • Chapter 5 Programming with the tidyverse
  • Chapter 6 Packages
    • 6.1 Why you need your own packages in your life
    • 6.2 R packages: the basics
    • 6.3 Writing documentation for your functions
    • 6.4 Extra files inside your package and dependencies
    • 6.5 Unit test your package
    • 6.6 Checking the coverage of your unit tests with covr
    • 6.7 Wrap-up
  • Chapter 7 Unit testing
    • 7.1 Introduction
    • 7.2 Unit testing with the testthat package
    • 7.3 Actually running your tests
    • 7.4 Wrap-up
    • 7.5 Exercises
  • Chapter 8 Putting it all together: writing a package to work on data
    • 8.1 Getting the data
    • 8.2 Your first data munging package: prepareData

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub