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
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:
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:
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:
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