Preface

Welcome to Advanced R Solutions!

This book provides worked-out solutions to the exercises given in Advanced R1 and reflects our efforts to understand and acquire its content.

Advanced R covers R2 and programming. It presents the specific characteristics of the R language to programmers and helps R users to improve their understanding of general programming concepts.

When we came across Advanced R, it gave us a more thorough understanding of the R code we worked with daily and helped us to see the underlying principles more clearly. The content helped us to get a more complete picture of R’s programming landscape.

We soon re-discovered that reading about programming is not enough and that it helps greatly to open the editor and write some code along the way. The clear structure of Advanced R and the exercises given provided a great starting point for this.

We think of this book as a solutions manual, which intends to supplement and support your own study of the R language through Advanced R. We hope that it will help you to stay on track and allow you to check your understanding and progress along the way. The solutions may also give you another perspective on some of the presented material.

How this book came to be

The solutions in this book are written from our personal perspective and current level of understanding. We both came from mathematics and statistics backgrounds preparing us more carefully for data analysis than for programming. So, we were R users first and improved as programmers in our day jobs and through working on projects like this one.

By taking advantage of the bookdown package3 to structure our process, we created this book almost as a by-product. While the initial progress was fun and motivating, actually providing solutions to all of the 284 exercises took slightly longer than originally expected (and Hadley’s rewrite of Advanced R halfway in between didn’t really make the journey shorter).

As the project matured, we strived to provide solutions that were as clean, straightforward, and expressive as possible. As well-written code is often more expressive than lengthy explanations, many of the solutions are rather code heavy. The written explanations aim to provide context and motivation, discuss important implementation details, or relate to the practical work of being an R programmer.

Hadley Wickham wrote Advanced R and created the exercises which form the substructure of this book. We took the task to solve them as correctly and idiomatically as possible. When we finished a chapter, we asked Hadley to review it. His feedback included many comments (which we then had to resolve), corrections, and suggestions, as well as a few complete solutions. We repeated this process until each exercise was reviewed and approved. As a result, we feel pretty good about the quality of the solutions in the book. However, any remaining mistakes or inconsistencies are certainly on us.

How to use this book

Since this book builds so heavily on Advanced R, we think it should be read together with the textbook, either as a hardcopy or the online version (https://adv-r.hadley.nz). Working on an exercise first by yourself should in general give you the biggest benefit.

It may be a good idea to start with the parts of Advanced R that are most relevant to your work and interest. You can certainly read the book cover to cover, but we think that you don’t have to, though it’s probably a good idea to start with the foundations part.

Of the more difficult exercises in the book, only a few were completed in one go. Often we had to reread the question or look up the related content in Advanced R and started by writing a few lines of code first or consulting the documentation. Reading the source code (preferably with syntax highlighting) and searching the web were typically quite helpful.

To support your study, you may also be interested in the R4DS Advanced R book club, where groups of readers regularly discuss a different chapter of Advanced R.

In case you want to do more or have a special interest in the mechanics of base R, you may be interested in checking out the first edition of Advanced R (http://adv-r.had.co.nz/).4 Some additional solutions related to that edition can be found at https://advanced-r-solutions-ed1.netlify.app/.

There is one recommendation from Advanced R that we’d like to echo: reading source code can be tremendously helpful in developing your programming skill! For example, you can just head to GitHub and start looking into the source code of packages you love and developers you admire. While reading, it’s not necessary to understand every line of code right away. Keeping this a regular practice (for a while) will expose you to many new ideas, patterns, and design choices, and also expand your R vocabulary.

We don’t necessarily apply many of the concepts taught in Advanced R in daily programming and that’s okay! But we hope that the code we write has become more robust, expressive, and readable, and it’s actually quite easy to see the progress when we take a look at the earlier drafts of our own code.

Acknowledgements

Many open source projects are the result of the work of a lot of people; so is this. We would like to explicitly mention and thank everybody who contributed solutions, raised questions, or helped to fix spelling and grammar to improve this work:

@3zhang, Anne (@ahoffrichter), Anh N Tran (@anhtr), Arash (@arashHaratian), Leon Kim (@BetweenTwoTests), Jun Cai (@caijun), @Charles926, Safouane Chergui (@chsafouane), Corrado Lanera (@CorradoLanera), @davidblitz, Zhuoer Dong (@dongzhuoer), @Elucidase, Fabian Scheipl (@fabian-s), @HannesOberreiter, @its-gazza, Jorge Aranda (@jorgearanda), @lotgon, @MajoroMask, Maya Gans (@MayaGans), Øystein Sørensen (@osorensen), Peter Hurford (@peterhurford), @philyoun, PJ (@pieterjanvc), Robert Krzyzanowski (@robertzk), Emily Robinson (@robinsones), Tanner Stauss (@tmstauss), @trannhatanh89, and Yihui Xie (@yihui).

Tobias Stalder (@toeb18) designed the beautiful cover, which visualizes the structure of Advanced R and its exercises.

Thanks to CRC Press for the interest in the project and our editor, Rob Calver, and his assistant, Vaishali Singh, for their patience and support in making this book a reality.

Thanks to our managers and companies for granting us some flexibility with our work schedules and generally supporting the completion of this project.

Conventions

A brief overview of conventions we followed and decisions we made.

  • Some chapters and sections in Advanced R do not contain exercises. In our book you will see that we skipped these chapters and sections. This decision introduces some gaps in the numbering, but we feel that keeping the numbers in sync with those of Advanced R will provide the most practical value.
  • We strived to follow mostly the tidyverse style guide. The {styler} package5 helped us to check many of the rules automatically.
  • Each chapter of this book was rendered in a separate R session via the bookdown package. We configured this process to initially:
    • set `%>%` <- magrittr::`%>%` to unlock the pipe operator without specifically loading the magrittr package6 every time,
    • set a random seed (1014) to improve reproducibility (similar as in Advanced R), and
    • define a few ggplot2 and knitr options.
    You can check out the exact code on GitHub.
  • We chose to keep the code in this book as self-contained as possible.
    • The packages used are usually loaded in the beginning of each chapter.
    • We repeat all code from Advanced R that is necessary to work on an exercise but not explicitly part of the exercise. When some longer code passages (from Advanced R) are omitted, this is explicitly stated in the solution.
  • The printed version of the book was rendered with R version 4.0.3 (2020-10-10) and the most recent available package versions as of December 2020. (The print version of Advanced R was rendered with R version 3.5.2.)
  • Emoji images in the printed book come from the open-licensed Twitter Emoji.
  • Benchmarks are computed when the book is rendered. While this improves reproducibility, the exact results will depend on the system creating the document.

Closing remarks

We are so happy to finish this exciting project that in fact neither of us really had the time for. We probably wouldn’t have made it to the finish line if we hadn’t worked on it together.

Collaboration is powerful and it’s fun to build and share. The various backgrounds represented in the R community generally make this exchange much more interesting and meaningful. Much of this success is possible because R is free software. At least in theory, everyone can contribute and no one can take away your freedom to do so.

The automated systems we build using these tools are not neutral and the rapid adoption of data-driven processes in business and technology does clearly affect our everyday lives and societies. It’s important that everyone has a fair say in the discussions about these systems and participates in their design. Against this background, we chose to donate half of our royalties from this book to https://rladies.org/, an organization empowering minority genders in the R community.

Thank you for your interest in this project and we hope the solutions will be of value to you.

See you around!

Malte Grosser @malte_grosser

Henning Bumann @henningsway