by Ben Ogorek
I'm grateful to Rook for helping me, a simple statistician, learn a few fundamentals of web technology. For R web application development, there are increasingly polished methods available (most notably Shiny ), but you can build one using Rook, and you might just learn something if you do.
Jeffrey Horner's Rook  is both a web server interface and an R package. The idea behind the former is to separate application development from server implementation. Thus, when a web server supports a web server interface, an application written to its specifications is guaranteed to run on that server.
This concept is not unique to R; there is WSGI for Python, Rack for Ruby (Horner's own inspiration ), and PSGI for Perl. In a world of modern web development frameworks, they do not appear to be losing steam. PSGI and its associated Perl module Plack are even said to be the “superglue interface between perl web application frameworks and web servers, just like Perl is the duct tape of the internet” . Unlike the PSGI / Plack distinction, the name Rook is used for both the specification and the R package.
While there are nice examples of Rook on the web, I was unable to find a tutorial that guided me from the ground up. This is meant to be that tutorial. In the sections that follow, a Rook web application is built from scratch and explained at each stage. From a web browser, it takes user inputs, performs (simple) R calculations, and displays graphics.
Rook Application Basics
A Rook application is literally an R function that
- takes an R environment as input,
- returns a list of HTTP-relevant items as output.
Input. As for R environments, consider .GlobalEnv.
z <- "hello" x.sq <- function(x) x * 2 ls.str(.GlobalEnv) ## x.sq : function (x) ## z : chr "hello"
Output. The following is an example Rook application's output.
$status  200 $headers $headers$`Content-Length`  500 $headers$`Content-Type`  "text/html" $body  "***Times through the Rook.app function: 6 ***") }
As before, add the application to the server object.
# Add your Rook app to the Rhttp object R.server$add(app = Rook.app, name = "My rook app") print(R.server) ## Server started on 127.0.0.1:23103 ##  pic_dir http://127.0.0.1:23103/custom/pic_dir ##  My rook app http://127.0.0.1:23103/custom/My rook app ## ## Call browse() with an index number or name to run an application.
Using the application
To interact with the application, use the server object's browse method with the name given in the last step.
# view your web app in a browser R.server$browse("My rook app")
As you interact with the application, pay attention to the iter variable, which counts the total number of times through the Rook.app function. And notice what happens to the HTML input fields every time a button is pressed. HTTP is truly a stateless protocol!
To stop the server, use the stop method of the Rhttpd object.
# view your web app in a browser R.server$stop()
Sometimes I have to restart R, especially after many changes and browsing attempts.
I've had a great time learning Rook, and I hope that readers of this article have enjoyed the journey as well. As one final point, while we have developed locally, note that Rook applications can also be deployed .
The markdown  and knitr  packages, in conjunction with RStudio's IDE , were used to create this document. Sincere thanks to Josh Mills for his sound advice and feedback. Keep up with ours and other great articles on R-Bloggers, and follow me on Twitter (@baogorek) for my latest research updates.
RStudio Shiny. URL http://www.rstudio.com/shiny/
Jeffrey Horner (2012). Rook: Rook - a web server interface for R. R package version 1.0-8. URL http://CRAN.R-project.org/package=Rook
Introducing Rook, an article from the Jeffrey Horner blog by Jeffrey Horner. Published on April 18, 2011. URL http://jeffreyhorner.tumblr.com/post/4723187316/introducing-rook
PSGI/Plack webpage. URL http://plackperl.org/
Deploy Rook Apps with rApache: Part I, an article from the Jeffrey Horner blog by Jeffrey Horner. Published on July 23, 2012. URL http://jeffreyhorner.tumblr.com/post/27861973339/deploy-rook-apps-with-rapache-part-i
JJ Allaire, Jeffrey Horner, Vicent Marti and Natacha Porte (2012). markdown: Markdown rendering for R. R package version 0.5.3. http://CRAN.R-project.org/package=markdown
Yihui Xie (2012). knitr: A general-purpose package for dynamic report generation in R. R package version 0.6. http://CRAN.R-project.org/package=knitr
RStudio IDE for Windows. URL http://www.rstudio.com/ide/
R Development Core Team (2012). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0, URL http://www.R-project.org/