Create Bart Simpson Blackboard Memes with R


Everybody knows the Simpsons, everybody loves the Simpsons and everybody can laugh about Bart Simpson writing funny lines on the blackboard! If you want to create your own Bart Simpson Blackboard Meme Generator with R read on!

Conveniently enough there is a package for creating memes already (who would have thought otherwise, because there is a package for everything!), the meme package by my colleague Professor Guangchuang Yu from the University of Hong Kong. After installing it from CRAN we load it, assuming that you work on a Windows machine load the Comic Sans font and a clean Bart Simpson Blackboard pic into R:

library(meme)
if (.Platform$OS.type == "windows") {
  windowsFonts(Comic = windowsFont("Comic Sans MS"))
}
bart <- "pics/bart_simpson_chalkboard-5157.gif" # source: http://free-extras.com/images/bart_simpson_chalkboard-5157.htm

We can start right away by using the meme function with the pic, text, font size and font as arguments (you might have to adapt the font size, for a new line use the "\n" escape sequence):

meme(bart, "\nfor (i in 1:100) {\n  print(\"I will not use loops in R\")\n}", size = 1.8, font = "Comic", vjust = 0, r = 0)

As an aside: to fully appreciate the joke you should know something about loops and how to avoid them by making use of vectorization in R (see here: Learning R: The Ultimate Introduction (incl. Machine Learning!). Another method to avoid loops is by using the apply family of functions (for those so-called higher-order functions see here: Learning R: A Gentle Introduction to Higher-Order Functions).

But if you want to go full “Bart Simpson” you, of course, need to repeat the lines several times (the rep function comes in handy here). The whole (punitive) work is done by this short piece of code (just change text for your own memes):

text <- "I will not waste chalk"

text <- paste(rep(text, 8), collapse = "\n")
text <- paste0("\n", text)
meme(bart, text, size = 1.6, font = "Comic", vjust = 0, r = 0)

Happy memeing with BaRt!

4 thoughts on “Create Bart Simpson Blackboard Memes with R”

  1. Sorry to be that guy, but for loops in R are often faster than apply:

    > library(microbenchmark)
    > 
    > x  x2  x3  microbenchmark(
    +     vectorized = x1 <- x^2, 
    +     allocated = for(i in seq_along(x)){ x2[i] <- x[i]^2 },
    +     built = for(i in seq_along(x)){ x3[i] <- x[i]^2 },
    +     sapply = x4  
    > all.equal(x1, x2)
    [1] TRUE
    > all.equal(x1, x3)
    [1] TRUE
    > all.equal(x1, x4)
    [1] TRUE
    

    In the code above (assuming it formats okay), **a for loop with a pre-allocated vector is about 5 times as fast as sapply.** Even without pre-allocating, the for loop is faster. R version 3.4 introduced just-in-time compilation, which greatly increased the speed of for loops. The apply family of functions are just concise ways of writing loops. As always, vectorization is king.

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.