Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
library(devtools) pkg<-"swli" 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:
- Create JS console
- Use JS library
- 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") devtools::install_github("MangoTheCat/swli")
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.