Fun with R and the Noops

[This article was first published on Revolutions, 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.

Earlier this week, Github introduced Noops, a collection of simple black-box machines with API endpoints, with the goal of challenging developers of all skill levels to solve problems with them. Five “Noops” machines have been released so far along with challenges suitable for beginner programmers, with 15 further machines (and some more complex challenges) to be released over the coming three weeks. The bots available now are:

  • Hexbot, which generates random RBG color strings and x/y coordinates
  • Vexbot, which generates random endpoint coordinates of (possibly-connected) line segments
  • Directbot, which will generate sequences of LOGO-like movements, either at random or according to a pattern you specify
  • Drumbot, which generates commands for various pre-defined drumbeats
  • Fizzbot, which tests your knowledge of classic programmer interview questions

Descriptions of the Noops and their APIs are provided on GitHub, along with sample code in Javascript and/or Python. However, as long as you can connect to the internet, you can use any language you like to solve the challenges. 

R is an ideal language for interfacing with these bots. The httr package for R makes it easy to interact with APIs using the GET operation, which is also what you use to control most Noops. (Fizzbot also uses the POST verb.) All I need to do is to load the package, and define the endpoint URL for Hexbot. And the data from the bots lends itself to graphical visualization, which is also easy to do with R. So I thought it would be fun to take Hexbot for a spin with R.

Calling the endpoint without any parameters generates a single RGB color specification (for example: #95DDFA), and this is easily done using the GET function from the httr package. Here, I define a function random.color to return a random color generated by Hexbot as a single R character. Luckily, you can pass RGB strings as colors to the base graphics functions in R, so I can use the barplot function to generate a block of random color:

Onecolor

Hexbot can generate more than one color at once, if you pass in the count parameter. For this, we just need to use the query option to the GET function. Here, we define a function to generate n random colors and extract the results as a vector, which we can then display with the barplot function once more:

Manycolors
You can also provide multiple parameters to the endpoint: if you also provide width and height, it will return random x and y locations within those ranges, along with the random colors. This adds some additional complexity when it comes to extracting the data from the payload, but with that done we can display the results as a scatterplot:

Noops-scatter
Of course, this is all rather pointless, in the sense that I could trivially use R itself to generate random points and colors. But still, it's a useful exercise to work through this yourself if you're not familiar with interacting with HTTP APIs. You'll learn several things along the way:

  • about the HTTP GET operation and how to provide query parameters;
  • how to navigate the nested lists that httr creates from the JSON content payload; and
  • how to unroll those nested lists to extract the data you need in R.

The Noops yet to be revealed promise to offer more complex challenges, so I expect we'll learn about other aspects of HTTP APIs when they're to be released in the coming weeks as well.

Github: Meet the Noops

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

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)