R pranks
One of the more fun features of R is that you can redefine pretty much anything. While this isn't particularly useful in itself, it can give you plenty of opportunities to mess with your friends' R sessions. Well, at least they were your friends before this post.
Mild pranks
You can't handle the truth
Starting off, we have the obvious. If you're tripped up by this one you deserve what's coming to you.
T <- FALSE F <- TRUE
Some people will inflict this upon themselves if they name a variable T (and set it equal to 0) or F (and set it equal to a non-zero number).
HAL 9000
A similar bush league trick.
q <- quit <- function(...) { print(paste0("I'm sorry ", Sys.info()[7], ", I'm afraid I can't do that")) }
Bonus points for setting the function up to play the audio clip. That however needs a little preparation.
No R for you!
This one has to be placed in the .Rprofile to work.
.First <- function() { quit(save = "no", runLast = FALSE) }
A bit more devious
Now we get into some more insidious tricks.
How do we know that two and two make four?
This one could presumably go undetected for a while; so perhaps you might want to make the issue a bit more obvious if you actually run this one.
`+` <- function(xx, yy) { sum <- .Primitive('+')(xx, yy) return(.Primitive('+')(sum, sum * .Machine$double.eps)) }
getRandomNumber
This one might not catch people at all. It is great for reproducibility though.
old_set.seed <- set.seed set.seed <- function(seed, ...) { old_set.seed(42, ...) }
Patience is a virtue
Is your friend always talking about how the *apply functions are so much faster and more elegant? This should put a stop to that.
make_apply <- function() { timeout <- 0 return(function(...) { Sys.sleep(timeout) timeout <<- timeout + 1 base::apply(...) }) } apply <- make_apply()
What we have here is failure to communicate
These don't even need to be pranks. You can change some things to make them better coders. For instance, you can unilaterally enforce your own style guide. For their own good.
'=' <- function(...) { stop("Using = for assignment violates the style guide; use <- instead") }
Of course, nobody did this for package writers, so hopefully they don't need to use too many non-conforming packages.
It could be worse; I had wanted to do the following
'<-' <- function(...) { stop("Using <- for assignment violates the style guide; use -> instead") }
Unfortunately, this little gem doesn't work since '->' is just a syntactic variation and I can't figure out how to distinguish it from within the function call. I'll be very appreciative if someone could show me how to do this.