Prumnopytis

Prumnopytis

Review: Advanced R by [[Hadley Wickham]]

Advanced R

has a large number of very short chapters across 450 pages, a format it probably owes to its online availability.

The author knits together

The work and author are an active part of the R community.

Why was I here   aside introspection

I jumped ship from matlab to scipy in early 2014. It seemed more vibrant, and didn't involve buying licenses.

Python in the wild has seemed increasingly jumbled, unmotivated and terminally newbie, so in 2019 I decided to jump back into a ship: R.

The stats department at Waikato University endorses R (kiwi as) and I have had commercial encounter with Apache Spark.

Why R?

I wanted a high level interactive language that fit in with and between Emacs org-mode and low level cluster / gpu implementation.

Emacs speaks statistics and R fit that bill beautifully out of the box.

Lights

High

  • Foundations building up to generic functions and S3 classes were a great refresher and primer
  • Quick breakdown and examples of functional programming
  • Walkthrough applications of R's deep reflection powers
  • Advice about and for performant programming
  • Great forwarding to further books and online resources

Low

  • I would have liked less context around traversing R's abstract syntax tree even though it was useful
  • Paid attention to RStudio and Windows installs without mentioning ESS
  • Long written urls everywhere in an ebook
  • Mangled LaTeX rendering in chapter about LaTeX
  • mangledlatex.png

Exercises

I diverged from the prescribed exercises to just skimming them and then poking around independently in Foundations somewhere.

They were interesting to read and think about.

Did not ace any of the can-skip-this-section quizes :(.

Playing with R with ESS with org-babel   aside

Since I was not using R Studio!

## Make and enter a source block
<s <return> R C-c '

## Evaluated line-by-line
C-<return>

## Evaluate buffer
C-c M-b

## To leave
C-c '

In the containing .org, C-c C-c within the source block evaluates



#+BEGIN_SRC R :results output
  M <-
    matrix(
      1:10,
      ncol=2
    )

  t(
    lapply(
      M,
      mean
    )
  )
#+END_SRC

#+RESULTS:
:      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
: [1,] 1    2    3    4    5    6    7    8    9    10   

Load R org-babel evaluation:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . nil)
   (R . t)
   (latex . t)))

(I like to evaluate elisp with M-x eval-buffer in scratch. Note that org-babel latex doesn't seem to play nicely with org-mode Latex.)

Table of Contents, marked done when I remembered to.

DONE Introduction [8/8]

DONE Who should read this book

DONE What you will get out of this book

DONE Meta-techniques

DONE Recommended reading

DONE Getting help

DONE Acknowledgments

DONE Conventions

DONE Colophon

DONE Foundations [8/8]

DONE Data structures [5/5]

  • DONE Vectors [3/3]
    • DONE Atomic vectors [2/2]
      • DONE Types and tests
      • DONE Coercion
    • DONE Lists
    • DONE Exercises
  • DONE Attributes [3/3]
    • DONE Names
    • DONE Factors
    • DONE Exercises
  • DONE Matrices and arrays [1/1]
    • DONE Exercises
  • DONE Data frames [5/5]
    • DONE Creation
    • DONE Testing and coercion
    • DONE Combining data frames
    • DONE Special columns
    • DONE Exercises
  • DONE Answers

DONE Subsetting [5/5]

  • DONE Data types [7/7]
    • DONE Atomic vectors
    • DONE Lists
    • DONE Matrices and arrays
    • DONE Data frames
    • DONE S3 objects
    • DONE S4 objects
    • DONE Exercises
  • DONE Subsetting operators [4/4]
    • DONE Simplifying vs. preserving subsetting
    • DONE \$
    • DONE Missing/out of bounds indices
    • DONE Exercises
  • DONE Subsetting and assignment
  • DONE Applications [9/9]
    • DONE Lookup tables (character subsetting)
    • DONE Matching and merging by hand (integer subsetting)
    • DONE Random samples/bootstrap (integer subsetting)
    • DONE Ordering (integer subsetting)
    • DONE Expanding aggregated counts (integer subsetting)
    • DONE Removing columns from data frames (character subsetting)
    • DONE Selecting rows based on a condition (logical subsetting)
    • DONE Boolean algebra vs. sets (logical & integer subsetting)
    • DONE Exercises
  • DONE Answers

DONE Vocabulary [5/5]

  • DONE The basics
  • DONE Common data structures
  • DONE Statistics
  • DONE Working with R
  • DONE I/O

DONE Style guide [3/3]

package formatR ?

  • DONE Notation and naming [2/2]
    • DONE File names
    • DONE Object names
  • DONE Syntax [5/5]
    • DONE Spacing
    • DONE Curly braces
    • DONE Line length
    • DONE Indentation
    • DONE Assignment
  • DONE Organisation [1/1]
    • DONE Commenting guidelines

DONE Functions [7/7]

  • DONE Function components [2/2]
    • DONE Primitive functions
    • DONE Exercises
  • DONE Lexical scoping [5/5] ??
    • DONE Name masking
    • DONE Functions vs. variables
    • DONE A fresh start
    • DONE Dynamic lookup
    • DONE Exercises
  • DONE Every operation is a function call
  • DONE Function arguments [6/6]
    • DONE Calling functions
    • DONE Calling a function given a list of arguments
    • DONE Default and missing arguments
    • DONE Lazy evaluation
    • DONE ...
    • DONE Exercises
  • DONE Special calls [3/3]
    • DONE Infix functions
    • DONE Replacement functions
    • DONE Exercises
  • DONE Return values [2/2]
    • DONE On exit
    • DONE Exercises
  • DONE Quiz answers

DONE OO field guide [6/6]

  • DONE Base types
  • DONE S3 [5/5]
    • DONE Recognising objects, generic functions and methods
    • DONE Defining classes and creating objects
    • DONE Creating new methods and generics
    • DONE Method dispatch
    • DONE Exercises
  • DONE S4 [5/5]
    • DONE Recognising objects, generic functions, and methods
    • DONE Defining classes and creating objects
    • DONE Creating new methods and generics
    • DONE Method dispatch
    • DONE Exercises
  • DONE RC [4/4]
    setRefClass()
    • DONE Defining classes and creating objects
    • DONE Recognising objects and methods
    • DONE Method dispatch
    • DONE Exercises
  • DONE Picking a system
  • DONE Quiz answers

DONE Environments[6/6]

  • DONE Environment basics [1/1]
    • DONE Exercises
  • DONE Recursing over environments [1/1]
    • DONE Exercises
  • DONE Function environments [5/5]
    • DONE The enclosing environment
    • DONE Binding environments
    • DONE Execution environments
    • DONE Calling environments
    • DONE Exercises
  • DONE Binding names to values [1/1]
    • DONE Exercises
  • DONE Explicit environments [3/3]
    • DONE Avoiding copies
    • DONE Package state
    • DONE As a hashmap
  • DONE Quiz answers

DONE Debugging, condition handling, and defensive programming [5/5]

  • DONE Debugging techniques
  • DONE Debugging tools [5/5]
    • DONE Determining the sequence of calls
    • DONE Browsing on error
    • DONE Browsing arbitrary code
    • DONE The call stack: traceback(), where, and recover()
    • DONE Other types of failure
  • DONE Condition handling [5/5]
    • DONE Ignore errors with try
    • DONE Handle conditions with tryCatch()
    • DONE withCallingHandlers()
    • DONE Custom signal classes
    • DONE Exercises
  • DONE Defensive programming [1/1]
    • DONE Exercises
  • DONE Quiz answers

DONE Functional programming[3/3]

DONE Functional programming[5/5]

  • DONE Motivation
  • DONE Anonymous functions [1/1]
    • DONE Exercises
  • DONE Closures[3/3]
    • DONE Function factories
    • DONE Mutable state
    • DONE Exercises
  • DONE Lists of functions [2/2]
    • DONE Moving lists of functions to the global environment
    • DONE exercises
  • DONE Case study: numerical integration [1/1]
    • DONE Exercises

DONE Functionals[7/7]

  • DONE My first functional: lapply()[2/2]
    • DONE Looping patterns
    • DONE Exercises
  • DONE For loop functionals: friends of lapply()[5/5]
    • DONE Vector output: sapply and vapply
    • DONE Multiple inputs: Map (and mapply)
    • DONE Rolling computations
    • DONE Parallelisation
    • DONE Exercises
  • DONE Manipulating matrices and data frames [4/4]
    • DONE Matrix and array operations
    • DONE Group apply
    • DONE The plyr package
    • DONE Exercises
  • DONE Manipulating lists [3/3]
    • DONE Reduce()
    • DONE Predicate functionals
    • DONE Exercises
  • DONE Mathematical functionals [1/1]
    • DONE exercises
  • DONE Loops that should be left as is[3/3]
    • DONE Modifying in place
    • DONE Recursive relationships
    • DONE While loops
  • DONE A family of functions [1/1]
    • DONE Exercises

DONE Function operators [4/4]

  • DONE Behavioural FOs [4/4]
    • DONE Memoisation
    • DONE Capturing function invocations
    • DONE Laziness
    • DONE Exercises
  • DONE Output FOs [3/3]
    • DONE Minor modifications
    • DONE Changing what a function does
    • DONE Exercises
  • DONE Input FOs [3/3]
    • DONE Prefilling function arguments: partial function application
    • DONE Changing input types
    • DONE Exercises
  • DONE Combining FOs [3/3]
    • DONE Function composition
    • DONE Logical predicates and boolean algebra
    • DONE Exercises

DONE Computing on the language [2/2]

Non-standard evaluation [6/6]

  • DONE Capturing expressions [1/1]
    • DONE Exercises
  • DONE Non-standard evaluation in subset [1/1]
    • DONE Exercises
  • DONE Scoping issues [1/1]
    • DONE Exercises
  • DONE Calling from another function [1/1]
    • DONE Exercises
  • DONE Substitute [3/3]
    • DONE Adding an escape hatch to substitute
    • DONE Capturing unevaluated ...
    • DONE Exercises
  • DONE The downsides of non-standard evaluation [1/1]
    • DONE Exercises

DONE Expressions [7/7]

  • DONE Structure of expressions [1/1]
    • DONE Exercises
  • DONE Names [1/1]
    • DONE Exercises
  • DONE Calls [3/3]
    • DONE Modifying a call
    • DONE Creating a call from its components
    • DONE Exercises
  • DONE Capturing the current call [1/1]
    • DONE Exercises
  • DONE Pairlists [1/1]
    • DONE Exercises
  • DONE Parsing and deparsing [1/1]
    • DONE Exercises
  • DONE Walking the AST with recursive functions [4/4]
    • DONE Finding F and T
    • DONE Finding all variables created by assignment
    • DONE Modifying the call tree
    • DONE Exercises

DONE Domain specific languages [2/2]

  • DONE HTML [6/6]
    • DONE Goal
    • DONE Escaping
    • DONE Basic tag functions
    • DONE Tag functions
    • DONE Processing all tags
    • DONE Exercises
  • DONE LaTeX [8/8]
    • DONE LaTeX mathematics
    • DONE Goal
    • DONE to_math
    • DONE Known symbols
    • DONE Unknown symbols
    • DONE Known functions
    • DONE Unknown functions
    • DONE Exercises

DONE Performance [10/10]

DONE Performance [5/5]

  • DONE Why is R slow?
  • DONE Microbenchmarking [1/1]
    • DONE Exercises
  • DONE Language performance [4/4]
    • DONE Extreme dynamism
    • DONE Name lookup with mutable environments
    • DONE Lazy evaluation overhead
    • DONE Exercises
  • DONE Implementation performance [3/3]
    • DONE Extracting a single value from a data frame
    • DONE iffelse(), pmin() and pmax()
    • DONE Exercises
  • DONE Alternative R implementations

DONE Optimising code[11/11]

  • DONE Measuring performance [1/1]
    • DONE Limitations
  • DONE Improving performance
  • DONE Code organisation
  • DONE Has someone already solved the problem? [1/1]
    • DONE Exercises
  • DONE Do as little as possible [1/1]
    • DONE Exercises
  • DONE Vectorise [1/1]
    • DONE Exercises
  • DONE Avoid copies
  • DONE Byte code compilation
  • DONE Case study: t-test
  • DONE Parallelise
  • DONE Other techniques

DONE Memory [4/4]

  • DONE Object size [1/1]
    • DONE Exercises
  • DONE Memory usage and garbage collection
  • DONE Memory profiling and lineprof [1/1]
    • DONE Exercises
  • DONE Modification in place[2/2]
    • DONE Loops
    • DONE Exercises

DONE High performance functions with Rcpp [3/3]

  • DONE Getting started with C++
    • DONE No inputs, scalar output
    • DONE Scalar input, scalar output
    • DONE Vector input, scalar output
    • DONE Vector input, vector output
    • DONE Matrix input, vector output
    • DONE Using sourceCpp
    • DONE Exercises
  • DONE Attributes and other classes [3/3]
    • DONE Lists and data frames
    • DONE Functions
    • DONE Other types
  • DONE Missing values [5/5]
    • DONE Scalars [2/2]
      • DONE Integers
      • DONE Doubles
    • DONE Strings
    • DONE Boolean
    • DONE Vectors
    • DONE Exercises

DONE Rcpp sugar [4/4]

  • DONE Arithmetic and logical operators
  • DONE Logical summary functions
  • DONE Vector views
  • DONE Other useful functions

DONE The STL [7/7]

  • DONE Using iterators
  • DONE Algorithms
  • DONE Data structures
  • DONE Vectors
  • DONE Sets
  • DONE Map
  • DONE Exercises

DONE Case studies [2/2]

  • DONE Gibbs sampler
  • DONE R vectorisation vs. C++ vectorisation

DONE Using Rcpp in a package

DONE Learning more

DONE Acknowledgements

DONE R's C interface [6/6]

DONE Calling C functions from R

DONE C data structures

DONE Creating and modifying vectors [7/7]

  • DONE Creating vectors and garbage collection
  • DONE Missing and non-finite values
  • DONE Accessing vector data
  • DONE Character vectors and lists
  • DONE Modifying inputs
  • DONE Coercing scalars
  • DONE Long vectors

DONE Pairlists

DONE Input validation

DONE Finding the C source code for a function

Comments

comments powered by Disqus