Hadley Wickham’s guide to R as a programming language is written for two audiences: intermediate R users who want to understand how the language really works, and programmers coming from other languages who want to know why R behaves the way it does. Rather than teaching data analysis, it explains the ideas and techniques behind well-designed R code, including those used to build the tidyverse.

The second edition is a major revision. It adds new foundational chapters on names and values, control flow, and conditions; expands object-oriented programming into separate chapters on S3, R6, and S4 and how to choose among them; and greatly deepens the coverage of metaprogramming, including the tidy evaluation framework. Examples use newer packages such as rlang for low-level operations and purrr for functional programming, and code and figures are printed in color. Chapters include quizzes and exercises, with worked answers available in the companion book Advanced R Solutions.

Readers will learn:

  • How objects differ from the names bound to them, and when R makes copies
  • How vectors, subsetting, functions, environments, and the condition system work
  • Functional programming techniques that can replace many for loops
  • The S3, S4, and R6 object-oriented systems and their trade-offs
  • Metaprogramming with expressions, quasiquotation, and tidy evaluation
  • Debugging strategies that work wherever your code runs
  • How to find performance bottlenecks and rewrite slow functions in C++ with Rcpp

The complete book is free to read online under a Creative Commons license.