R

The statistical programming language, not the letter



tl;dr R is a powerful, effective, diverse, well-supported, free, messy, inefficient, de-facto standard. As far as scientific computation goes, this is outstandingly good.

Pros and cons

You can use R for anything, if you really really want.

Good

  • Free (beer/speech)
  • Combines unparalleled breadth and community, at least as pertains to statisticians, data miners, machine learners and other such assorted folk as I call my colleagues. To get some sense of this thriving scene, check out R-bloggers. That community alone should be enough to sell R as an ecosystem, whatever you think of technical chaos of the language itself (cf “Your community is your best asset”) And believe me, I have reservations about everything else.
  • Amazing, statistically-useful plotting (cf the awful battle to get error bars in python’s mayavi)
  • Online web-app visualisation: shiny
  • Integration into literate coding and reproducible research through knitr — see scientific writing workflow.

Bad

  • Seems to have been written by statisticians who prioritise delivering statistical functionality right now over making an elegant, fast or consistent language to access that functionality. (“Elegant”, “fast”, ”consistent”; you can choose… uh… Oh look, none of those are performance metrics for me. Gotta go! Byeeeeeee!) I’d rather access those same libraries through a language which has had as many computer scientists winnowing its ugly chaff as Python or Ruby has had. Or indeed Go or Julia. And, for that matter, I’d like as many amazing third-party libraries for non-statistical things as these other languages promise, even javascript. Anyway, it is convenient for many common use cases, which is nice.
  • Poetically, R has random scope amongst other parser and syntax weirdness.
  • Call-by-value semantics (in a "big-data" processing language?)
  • …ameliorated not even by array views,
  • …exacerbated by bloaty design.
  • Object model tacked on after the fact. In fact, several object models?. Which is fine? I guess?
  • One of the worst names to google for ever (cf Processing, Pure)

Installing R

See the R package page.

Installing packages

See the R package page.

Command-line scripting

RScript is the command-line invocation for R. littler is an older one.

Rio:

Loads CSV from stdin into R as a data.frame, executes given commands, and gets the output as CSV or PNG on stdout

Needful packages

Upon setting up a new machine I always run

install.packages(c("blogdown", "renv", "tinytex", "knitr", "devtools", "ggplot2"))
tinytex::install_tinytex()
devtools::install_github("r-lib/hugodown")

That gets the baseline tools I actually use.

Now, details.

Shiny

shiny turns statistical models into interactive web apps. I made a notebook for that.

The tidyverse

The tidyverse is a miniature ecosystem within R which has coding conventions and tooling to make certain data analysis easier and prettier, although not necessarily more performant.

install.packages(c("tidyverse"))

Blogging / reports / reproducible research

blogdown, the blogging tool, and the knit rendering engine, as mentioned elsewhere comprise R’s entrant into the academic blogoverse. It does reproducible research and miscellaneous scientific writing. This is the R killer feature that incorporates all the other killer features.

Machine learning

R now plugs into many machine-learning-style algorithms.

For one example, you can run keras, and hence presumably tensorflow via Rstudio’s keras. Other enterprises here include mlr/mlr3

R does not define a standardized interface for its machine-learning algorithms. Therefore, for any non-trivial experiments, you need to write lengthy, tedious and error-prone wrappers to call the different algorithms and unify their respective output.

Additionally you need to implement infrastructure to

  • resample your models
  • optimize hyperparameters
  • select features
  • cope with pre- and post-processing of data and compare models in a statistically meaningful way.

As this becomes computationally expensive, you might want to parallelize your experiments as well. This often forces users to make crummy trade-offs in their experiments due to time constraints or lacking expert programming skills.

mlr provides this infrastructure so that you can focus on your experiments! The framework provides supervised methods like classification, regression and survival analysis along with their corresponding evaluation and optimization methods, as well as unsupervised methods like clustering. It is written in a way that you can extend it yourself or deviate from the implemented convenience methods and construct your own complex experiments or algorithms.

I think this pitch is more or less the same for caret.

There are also externally developed of ML algorithms accessible from R that presumably have consistent interfaces by construction: h2o

Dataframe alternatives

data.table refines data frames in various ways, including lett dunderheaded default consstructors, high performance dataset querying, and approximately the same functionality as dplyr, but seems to be faster at e.g. sorting. It has a slightly different syntax to built-in dataframes, usually better syntax. Here is a tutorial and the introduction.

disk.frame is a friendly gigabyte-scale single machine disk-backed data store, for stuff too big for memory.

In the tidyverse

Testing

Academics are terrible at testing so I do not know how relevant this is, but ttdo + tinytest looks low-difficulty.

Plotting

IMO, the real killer feature of R.

See Plotting in R.

High performance R

Rcpp seems to be how everyone invokes their favoured compiled C++ code.

There are higher level tools that do this under the hood -

rstan compiles an inner loop this for Bayesian posterior simulation and a little bit of basic variational inference.

If you want a little more freedom but still want to have automatic differentiation and linear algebra done by magic, try TMB whose name and description are both awful but which manages pretty neat reduced rank matrix and optimization tricks for you.

Interacting with Julia

Julia is a nice language that can attain high performance.

I don’t know how to choose between these alternative methods. They both seem to have stalled, but XRJulia seems to be somewhat fresher.

XRJulia:

This package provides an interface from R to Julia, based on the XR structure, as implemented in the XR package, in this repository.

RJulia:

rJulia provides an interface between R and Julia. It allows a user to run a script in Julia from R, and maps objects between the two languages.

Strings

R is distractingly verbose when it comes to text.1 It has minimal string formatting baked in, although all the needful tools are there more or less. not-particularly consistent or polished functions You need to use libraries if you don’t want to have lots of paste invocations everywhere and the usual regexes. A more mnemonic string handling library is stringr.

Formatting numbers

I can’t help but feel there might be an easier way to represent a proportion as a percentage to two decimal places with the built-in infrastructure?

paste(format(round(x * 100, 2), nsmall = 2), '%')

Depends what I mean by easier, I suppose. There is also sprintf which does classic C formatting, which is not luxurious by modern standards but works OK

HTML

I recommend htmltools for generating HTML from structured data. The manual is annoyingly perfunctory and it sends you off to some other web pages which are supposed to tell you more about ti but do not. Nonetheless it is pretty easy to work out. Here and here are the least perfunctory introductions. I like using it with withTags, which creates an environment wherein every HTML tag is a function, and behaves in an obvious way. Here is my code to make a custom HTML fragment for every row in a data frame for the ozpowermap project.

popupHtml = apply(winners_sf, 1, function(r){
    s = as.character(withTags({
        div(
            p(
                h3(str_to_title(paste(r$GivenNm, r$Surname, paste(sep="", '(', r$PartyNm, ')'))))
            ),
            dl(
                dt('Winnning Margin'), dd(r$Margin ),
                dt('First prefs'), dd(r$FP_OrdinaryVotes),
                dt('Total Votes'), dd(r$TotalVotes),
            )
        )
    }))
    # s[[1]]
})

IDEs

See R IDES.

Saving and loading

Save my workspace (i.e. current scope and variable definitions) to ./.Rdata

> save.image()

Load my workspace from ./.Rdata

> rm(list=ls())  # clear current defs
> load(".RData") # actually load

Subsetting hell

To subset a list based object:

x[1]

to subset and optionally downcast the same:

x[[1]]

to subset a matrix-based object:

x[1, , drop=FALSE]

to subset and optionally downcast the same:

x[1]

Data exchange

How to pass sparse matrices between R and Python

My hdf5 hack

Counter-intuitively, this FS-backed method was a couple of orders of magnitude faster than rpy2 last time I tried to pass more than a few MB of data.

Apparently you can use arrow for this these days, although there is little documentation. Also, you can try rccpcnpy (pronounced Arrsipeekernoppy) which is a numpy-matrix-loader for R.

Debugging

Introspection

Here are a list of introspection hacks:

str(obj)
typeof(obj)
class(obj)
sapply(obj, class)
sapply(obj, attributes)
attributes(obj)
names(obj)

Inspecting frames post hoc

Use recover. In fact, pro-tip, you can invoke it in 3rd party code gracefully:

options(error = utils::recover)

Basic interactive debugger

There is at least one, called browser.

R for Pythonistas

Many things about R are surprising to me, coming as I do most recently from Python. I’m documenting my perpetual surprise here, in order that it may save someone else the inconvenience of going to all that trouble to be personally surprised.

Opaque imports

Importing an R package, unlike importing a python module, brings in random cruft that may have little to do with the names of the thing you just imported. That is, IMO, poor planning, although history indicates that most language designers don’t agree with me on that:

> npreg
Error: object 'npreg' not found
> library("np")
Nonparametric Kernel Methods for Mixed Datatypes (version 0.40-4)
> npreg
function (bws, …) #etc

Further, Data structures in R can do, and are intended to, provide first class scopes for looking up of names. You are, in your explorations into data, as apt to bring the names of columns in a data set into scope as much as the names of functions in a library. This is kind of useful, although it leads to bizarre and unhelpful errors, so watch it.

No scalar types…

A float is a float vector of size 1:

> 5
[1] 5

…yet verbose vector literal syntax

You makes vectors by using a call to a function called c. Witness:

> c('a', 'b', 'c', 'd')
[1] "a" "b" "c" "d"

If you type a literal vector in though, it will throw an error:

> 'a', 'b', 'c', 'd'
Error: unexpected ',' in "'a',"

I’m sure there are Reasons for this; it’s just that they are reasons that I don’t care about.

Which files do I need to keep?

Here is a good .gitignore file for R which keeps only what you need.

Tips

Quantile transform

quantile.t = function(v) ecdf(v)(v)

References

Bischl, Bernd, Michel Lang, Lars Kotthoff, Julia Schiffner, Jakob Richter, Erich Studerus, Giuseppe Casalicchio, and Zachary M. Jones. 2016. “Mlr: Machine Learning in R.” Journal of Machine Learning Research 17 (170): 1–5. http://jmlr.org/papers/v17/15-066.html.
Kuhn, Max. 2008. “Building Predictive Models in R Using the Caret Package.” Journal of Statistical Software 28 (1): 1–26. https://doi.org/10.18637/jss.v028.i05.
Kuhn, Max, and Kjell Johnson. 2013. Applied Predictive Modeling. New York: Springer-Verlag. https://www.springer.com/gp/book/9781461468486.

  1. much like me; thanks for bearing with my prolixity↩︎


No comments yet. Why not leave one?

GitHub-flavored Markdown & a sane subset of HTML is supported.