Create Bart Simpson Blackboard Memes with R

[This article was first published on R-Bloggers – Learning Machines, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


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, fontsize and font as arguments (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!

To leave a comment for the author, please follow the link and comment on their blog: R-Bloggers – Learning Machines.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)