The Making of a Star Wars Package

January 6, 2016

(This article was first published on Mango Solutions, and kindly contributed to R-bloggers)

By Stephanie Locke

After some conversations at the coffee maker yesterday, I accepted the challenge of, wait for it… BUILDING A STAR WARS THEMED R PACKAGE!

Some quick Googling found a JavaScript library that will generate some Star Wars based lorem ipsum text, ForcemJS.

In R, we can write and use JavaScript via the V8 package. I set up my project using devtools with just a few commands.

create(pkg) # Create package basics
use_package_doc(pkg) # Add core package file
use_testthat(pkg) # Add test skeleton
use_readme_rmd(pkg) # Add a README
use_package("V8", pkg = pkg) # Import V8

I relied heavily on the V8 vignettes to get started on my function swli() and marveled that at its core my function consisted of three lines:

  1. Create JS console
  2. Use JS library
  3. Call JS function

After this was written, I used the handy roxygen generator hotkey (Ctrl+Alt+Shift+R in Windows) in Rstudio to document it. I added an @importFrom statement to the central package file. Once I had the function documented, the next step was to write tests.

I could have written the tests beforehand but I’d wanted to make sure I got the JS library bound and working first. Tests checked defaults, correct parameter values, incorrect values, behaviour quirks, and known bugs (with the JS library).

The package at this point could have gone live but it was a little bit boring with just one function, so I made convenience functions that wrapped swli() to make it easier for people who know the names of the first three films. These all got documented into just a couple of files by using @describeIn and I added a basic test for each function.

I then committed everything to GitHub so now the package can be installed via devtools. May the Force ipsum be with you!

if(!(require(devtools))) install.package("devtools")

To leave a comment for the author, please follow the link and comment on their blog: Mango Solutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)