Learning R: Build xkcd’s Star Wars Spoiler Generator


Star Wars is somewhat nerdy, R definitely is… what could be more worthwhile than combining both 😉

This Sunday was Star Wars Day (May the 4th be with you!) and suitable for the occasion we will do a little fun project and implement the following xkcd flowchart, which can give us more than 2 million different Star Wars plots.

Even if you are new to R, the used code should be comprehensible, so read on!

First, I provide you with the surrounding phrases…

phrase_1 <- "In this Star Wars movie, our heroes return to take on the First Order and new villain"
phrase_2 <- "with help from their new friend"
phrase_3 <- "Rey builds a new lightsaber with a"
phrase_4 <- "blade, and they head out to confront the First Order's new superweapon, the"
phrase_5 <- "a space station capable of"
phrase_6 <- "They unexpectedly join forces with their old enemy"
phrase_7 <- "and destroy the superweapon in a battle featuring"
phrase_8 <- "P.S. Rey's parents are"
phrase_9 <- "and"

…and the different options:

villain_name <- c("Kyle Ren", "Malloc", "Darth Sebelius", "Theranos", "Lord Juul")
friend_name <- c("Kim Spacemeasurer", "Teen Yoda", "Dab Tweetdeck", "Yaz Progestin", "TI-83")
color <- c("beige", "ochre", "mauve", "aquamarine", "taupe")
superweapon_name <- c("Sun Obliterator", "Moonsquisher", "World Eater", "Planet Zester", "Superconducting Supercollider")
evil_plan <- c("blowing up a planet with a bunch of beams of energy that combine into one", "blowing up a bunch of planets with one beam of energy that splits into many", "cutting a planet in half and smashing the halves together like two cymbals", "increasing the CO2 levels in a planet's atmosphere, causing rapid heating", "triggering the end credits before the movie is done")
character_1 <- c("Boba Fett", "Salacious Crumb", "The Space Slug", "the bottom half of Darth Maul", "Youtube commenters")
strange_event <- c("a bow that shoots little lightsaber-headed arrows", "X-Wings and TIE fighters dodging the giant letters of the opening crawl", "a Sith educational display that uses Force Lightning to demonstrate the dielectric breakdown of air", "Kylo Ren putting on another helmet over his smaller one", "a Sith car wash where the bristles on the brushes are little lightsabers")
character_2 <- c("Luke", "Leia", "Han", "Obi-Wan", "a random junk trader")
character_3 <- c("Poe", "BB-8", "Amilyn Holdo", "Laura Dern", "a random junk trader", "that one droid from the Jawa Sandcrawler that says Gonk")

A quick calculation reveals that we have 2,343,750 possibilities of different Star Wars stories.

Now, perhaps with the data readily available, you might want to try to implement the SWSG (Star Wars Spoiler Generator) yourself…

Here is one way to do it: we create a function with sample() to choose from one of the given options, paste() to concatenate the different parts, and cat() to output the result:

SWSG <- function() {
  cat(paste(phrase_1, sample(villain_name, 1), phrase_2, sample(friend_name, 1)), "\n")
  cat(paste(phrase_3, sample(color, 1), phrase_4, sample(superweapon_name, 1), phrase_5, sample(evil_plan, 1)), "\n")
  cat(paste(phrase_6, sample(character_1, 1), phrase_7, sample(strange_event, 1)), "\n")
  cat(paste(phrase_8, sample(character_2, 1), phrase_9, sample(character_3, 1)))
}

Every call of this function generates a new random storyline:

SWSG()
## In this Star Wars movie, our heroes return to take on the First Order and new villain Kyle Ren with help from their new friend Kim Spacemeasurer 
## Rey builds a new lightsaber with a ochre blade, and they head out to confront the First Order's new superweapon, the Moonsquisher a space station capable of blowing up a bunch of planets with one beam of energy that splits into many 
## They unexpectedly join forces with their old enemy Boba Fett and destroy the superweapon in a battle featuring a bow that shoots little lightsaber-headed arrows 
## P.S. Rey's parents are Leia and Amilyn Holdo

SWSG()
## In this Star Wars movie, our heroes return to take on the First Order and new villain Theranos with help from their new friend Teen Yoda 
## Rey builds a new lightsaber with a aquamarine blade, and they head out to confront the First Order's new superweapon, the Moonsquisher a space station capable of blowing up a bunch of planets with one beam of energy that splits into many 
## They unexpectedly join forces with their old enemy the bottom half of Darth Maul and destroy the superweapon in a battle featuring X-Wings and TIE fighters dodging the giant letters of the opening crawl 
## P.S. Rey's parents are Han and a random junk trader

SWSG()
## In this Star Wars movie, our heroes return to take on the First Order and new villain Malloc with help from their new friend Kim Spacemeasurer 
## Rey builds a new lightsaber with a aquamarine blade, and they head out to confront the First Order's new superweapon, the Sun Obliterator a space station capable of cutting a planet in half and smashing the halves together like two cymbals 
## They unexpectedly join forces with their old enemy Youtube commenters and destroy the superweapon in a battle featuring Kylo Ren putting on another helmet over his smaller one 
## P.S. Rey's parents are a random junk trader and Poe

That was easy, right? May the FoRce be with you!

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.