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

…and the different options:

villain_name

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

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

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