The typo in the Lincoln Memorial - originally read ‘Euture’ (Source)

Day 28: Fcuk this facet_crap

A few weeks ago Jen Richmond wrote this post for the R-Ladies Sydney blog, and I have to confess it was a huge moment for me. I learned just how awesome the twitter #rstats community can be, and it’s opened up a whole new world where people on twitter are smart and kind and lovely to one another! (It’s like a parallel universe or something…)

It also called my attention to this fantastic tweet:

Fast-forward to a few weeks later. I’ve been writing these posts on regular basis, trying to be a little more active online, and following some of the fantastic quant folks on twitter. This tweet pops up on my feed:

Oh yes that one stings. The “facet_crap” problem again. My partner and I grumble about this a bit on the way to work and how we wished that programming languages were maybe a little better at detecting typos, though of course taking the moment to shudder at the horrible prospect of language with autocorrect.

So anyway then I had to go home early because I was again sick for no obvious reason. Sigh. There’s nothing quite as annoying as being just unwell enough to be useless for anything constructive, but not so unwell that you can just completely give up on the day. So of course I spend the day mindlessly browsing the internet, and then this tweet by Maëlle Salmon pops up feed:

That sounds awesome! I had no idea!

Does it solve facet_crap?

Amusing name notwithstanding, the idea behind fcuk is simple and unobtrusive. It captures error messages and when one of those object 'blahblahblah' not found messages appears, it suggests possible alternatives. It works interactively or when sourcing a script, but I have had some issues with getting to work within this post. It might be a blogdown issue, or maybe a more general incompatibility with R Markdown, or maybe I’m just not thinking clearly today. So I guess I’ll have to cut-and-paste for this one. Anyway, as usual, load the package:

library(fcuk)

Alternatively, calling fcuk::init_error_tracker() will start the error tracking, and fcuk::remove_error_tracker() will turn it off. With error tracking turned on, if I misspell the name of the t.test function it catches the error and suggests alternatives:

ttest(outcome ~ predictor)
Error in ttest(outcome ~ predictor) : could not find function "ttest"
Did you mean : step or t.test ?

Woohoo! It works!

The behaviour of the error tracking mechanism depends on the packages you have loaded. For instance, if I make the facet_crap error without ggplot2 loaded, I get this:

facet_crap(~group)
Error in facet_crap(~group) : could not find function "facet_crap"
Did you mean : .__C__repeat or rep.factor ?

Doing the same thing with ggplot2 loaded gives something much more sensible:

library(ggplot2)
facet_crap(~group)
Error in facet_crap(~group) : could not find function "facet_crap"
Did you mean : facet_wrap or FacetWrap ?

How does it work?

Poking around in the documentation reveals some wonderfully named functions. There’s get_all_objets_from_r which fetches the names of all objects currently loaded so that the package can work out what possible objects the user might be referring to. There’s the error_correction_propostion function that compares the offending typo to the list of possible objects and suggests the nearest alternatives based on similarity.

I do wonder why it uses the Jaccard measure of similarity? Here’s what I get for the facet_crap problem using the default Jaccard measure:

error_correction_propostion(
  asked_objet = "facet_crap", 
  method = "jaccard",
  n = 10
)
##  [1] "facet_wrap"       "FacetWrap"        ".__C__repeat"    
##  [4] "rep.factor"       "traceOff"         "RSpectra"        
##  [7] "facet_grid"       "theme_replace"    ".__C__data.frame"
## [10] "factor.scope"

A lot of those look super implausible as typos to me. In comparison, if I use the Jaro-Winkler distance, I get a bunch of things that do seem about right!

error_correction_propostion(
  asked_objet = "facet_crap", 
  method = "jw",
  n = 10
)
##  [1] "facet_wrap"   "FacetWrap"    "Facet"        "facet_grid"  
##  [5] "factor"       "FacetGrid"    "factorial"    "facet_null"  
##  [9] "lfactorial"   "factor.scope"

Neat.

Postscript!

On a bit of a personal note, one of the things I like about writing these posts - besides the fact that they’re fun - is the way find out about all kinds of new things just because other people read them. The whole reason I even thought to look at the fcuk package was because of the exchange between Maëlle Salmon and Elizabeth Davis. Maëlle Salmon linked to my tweets about these posts, and twitter sent me one of those “notification” thingies…

… and now I’m writing about fcuk. Yay.

Also… Aw! I’m so glad people find some of my incoherent rambling useful! ❤️

Avatar
Danielle Navarro
Associate Professor of Cognitive Science

Related