Introduction
Who should read this book
What you will get out of this book
Meta-techniques
Recommended reading
Getting help
Acknowledgements
Colophon
I Foundations
Data structures
Quiz
Vectors
Atomic vectors
Lists
Exercises
Attributes
Factors
Exercises
Matrices and arrays
Data frames
Creation
Testing and coercion
Combining data frames
Special columns
Subsetting
Data types
Atomic vectors
Lists
Matrices and arrays
Data frames
S3 objects
S4 objects
Exercises
Subsetting operators
Simplifying vs. preserving subsetting
$
Missing/out of bounds indices
Exercises
Subsetting and assignment
Applications
Lookup tables (character subsetting)
Matching and merging by hand (integer subsetting)
Random samples/bootstrap (integer subsetting)
Ordering (integer subsetting)
Expanding aggregated counts (integer subsetting)
Removing columns from data frame (character subsetting)
Selecting rows based on a condition (logical subsetting)
Boolean algebra vs sets (logical & integer subsetting)
Exercises
Vocabulary
The basics
Common data structures
Statistics
Working with R
I/O
Functions
Components of a function
Primitive functions
Exercises
Lexical scoping
Name masking
Functions vs. variables
A fresh start
Dynamic lookup
Exercises
Every operation is a function call
Function arguments
Calling functions
Calling a function given a list of arguments
Default and missing arguments
Lazy evaluation
...
Exercises
Special calls
Infix functions
Replacement functions
Exercises
Return values
on.exit()
Exercises
OO field guide
Introduction
Base types
S3
Recognising objects, generic functions and methods
Defining classes and creating objects
Creating new methods and generics
Method dispatch
Exercises
S4
Recognising objects, generic functions and methods
Defining classes and creating objects
Creating new methods and generics
Method dispatch
Exercises
RC
Defining classes and creating objects
Recognising objects and methods
Method dispatch
Exercises
Picking a system
Environments
Introduction
Environment basics
What is an environment?
Manipulating and inspecting environments
Special environments
Where
Exercises
Function environments
The environment where the function was created
The environment where the function lives
The environment created every time a function is run
The environment where the function was called
Exercises
Explicit scoping with local
Assignment: binding names to values
Regular binding
<<-
Delayed bindings
Active bindings
Exercises
Debugging, condition handling and defensive programming
Debugging techniques
Debugging tools
Determining the sequence of calls
Browsing on error
Browsing arbitrary code
The call stack: traceback(), where and recover().
Other types of failure
Condition handling
Ignore errors with try()
Handle conditions with tryCatch()
withCallingHandlers()
Custom signal classes
Exercises
Defensive programming
Exercises
II Functional programming
Functional programming
Other languages
Motivation
Anonymous functions
Exercises
Introduction to closures
Function factories
Mutable state
Exercises
Lists of functions
Moving lists of functions to the global environment
Exercises
Case study: numerical integration
Exercises
Functionals
Introduction
My first functional: lapply()
Looping patterns
Exercises
For loop functionals: friends of lapply()
Vector output: sapply and vapply
Multiple inputs: Map (and mapply)
Rolling computations
Parallelisation
Exercises
Data structure functionals
Matrix and array operations
Group apply
The plyr package
Exercises
Functional programming
Reduce()
Predicate functionals
Exercises
Mathematical functionals
Exercises
Converting loops to functionals, and when it's not possible
Modifying in place
Recursive relationships
While loops
A family of functions
Exercises
Function operators
In other languages
Behavioural FOs
Useful behavioural FOs
Memoisation
Capturing function invocations
Exercises
Output FOs
Minor modifications
Changing what a function does
Exercises
Input FOs
Prefilling function arguments: partial function application
Changing input types
Exercises
Combining FOs
Function composition
Logical predicates and boolean algebra
Exercises
The common pattern and a subtle bug
Exercises
III Computing on the language
Metaprogramming
Capturing expressions
Non-standard evaluation in subset
Exercises
Scoping issues
Exercises
Calling from another function
Substitute
Non-standard evaluation in substitute
Capturing unevaluated …
The downsides of non-standard evaluation
Applications
plyr::. and ggplot2::aes
Plyr: summarise, mutate and arrange
Conclusion
Expressions
Structure of expressions
Constants
Names
Calls
Exercises
Parsing and deparsing
Sourcing files from disk
Exercises
Capturing the current call
Other uses of call capturing
Exercises
Creating a function
Unenclose
Exercises
Walking the call tree with recursive functions
Finding F and T
Finding all variables created by assignment
Modifying the call tree
Exercises
Formulas
Formula as a quoting function
xtabs()
Formulas for modelling
Special contexts
Evaluate code in a special context
with_something
capture.output
Evaluating code in a new environment
Exercises
Anaphoric functions
With connection
Special environments for run-time checking
Logical abbreviations
Test that
Domain specific languages
HTML
Goal
Escaping
Basic tag functions
Tag functions
Processing all tags
Exercises
LaTeX
LaTeX mathematics
Goal
to_math
Known symbols
Unknown symbols
Known functions
Unknown functions
Exercises
IV Performance
Performance
Micro-benchmarking
Method dispatch
Extracting variables out of a data frame
Vectorised operations on a data frame
Brainstorming
Caching
Memoisation
Byte code compilation
Other people's code
Important vectorised functions
Rewrite in a lower-level language
Profiling and benchmarking
Performance profiling
Timing
Performance testing
Memory
object.size()
Exercises
Total memory use
Garbarge collection
Memory profiling with lineprof
Exercises
Modification in place
Loops
Exercises
High performance functions with Rcpp
Getting started
Getting started with C++
No inputs, scalar output
Scalar input, scalar output
Vector input, scalar output
Vector input, vector output
Matrix input, vector output
Using sourceCpp
Exercises
Rcpp classes and methods
Rcpp sugar
Arithmetic and logical operators
Logical summary functions
Vector views
Other useful functions
Missing values
Scalars
Strings
Boolean
Vectors
Exercises
The STL
Using iterators
Algorithms
Data structures
Vectors
Sets
Map
Exercises
Case studies
Gibbs sampler
R vectorisation vs. C++ vectorisation
Using Rcpp in a Package
Adding Rcpp to an existing package (Rcpp <= 0.10.6)
Adding Rcpp to an existing package (Rcpp >= 0.10.7)
More details
Learning more
More Rcpp
More C++
Acknowledgements
R's C interface
Introduction
Differences between R and C
Calling C functions from R
Basic data structures
Character vectors
Coercion and object creation
Object creation and garbage collection
Allocation shortcuts
Extracting C vectors
Modifying strings
Coercing scalars
Modifying objects
Pairlists and symbols
Missing and non-finite values
Checking types in C
Finding the C source code for a function
.External
Using C code in a package
V Packages
Package development philosophy
Getting started
Introduction to devtools
Package basics
Package essentials
Optional components
Getting started
The R/ directory
A minimal DESCRIPTION file
Other DESCRIPTION components
Source, binary and bundled packages
Package libraries
Installing packages
Exercises
The package development cycle
Dev mode
Key functions
Development cycles
Confirmatory programming
Exploratory programming
Package level documentation
README
plyr
stringr
NEWS
CITATION
Vignettes
Demos
Documenting functions
Help
Roxygen process
Common documentation tags
Documenting functions (and methods)
Documenting S3 generic functions and methods
Documenting S4 generic and methods
Documenting R5 methods
Documenting classes
Documenting a S3 class
Documenting an S4 class
Documenting an R5 class
Documenting datasets
Documenting packages
Text formatting
Lists
Tables
Mathematics
Character formatting
Dynamic help
Testing
Motivation
testthat test structure
Expectations
Tests
Contexts
Running tests
Testing files and directories
Autotest
R CMD check
Style guide
Notation and naming
File names
Identifiers
Syntax
Spacing
Curly braces
Line length
Indentation
Assignment
Organisation
Commenting guidelines
Namespaces
How do namespaces work
Exporting
Importing
Compiled code
How do they work
Git and github
Git
The basics
Github
Releasing a package
Checking
Version numbers
Publishing on CRAN
Publicising