4 lines of R to get you started using the Rook web server interface

Now that Jeffrey Horner has settled on a name for his new package… the Rook web server interface is now available on CRAN.

Rook provides an interface for R programmers to build web applications which can run in R 2.13’s built-in web server or (soon) rApache.

Jeffrey’s provided some great documentation and sample code on his blog, in the README file, and in the package documentation itself, but somehow I completely missed the importance of the Rhttpd class and couldn’t figure out how to load or launch any of the examples.

Hopefully I can save someone some similar head-scratching. The key is the Rhttpd class, which controls the web server and manages applications. By default it will install the “RookTest” example, so here are 4 lines you need to see it work:

> library(Rook)
> s <- Rhttpd$new()
> s$start(quiet=TRUE)
> s$print()

Server started on 127.0.0.1:31839
[1] RookTest http://127.0.0.1:31839/custom/RookTest

Call browse() with an index number or name to run an application.

[EDIT: Thanks to Jim Porzak to pointing out that browse() is a method on the Rhttpd object rather than an old school package-scoped function. Times are a-changin’… for the better!]

The browse() function didn’t seem to work for me, s$browse(1) will load the URL into your browser or you can just copy-and-paste to access the running application:

Enjoy!

Posted in Tips. Tags: , . 11 Comments »

11 Responses to “4 lines of R to get you started using the Rook web server interface”

  1. Jim Porzak Says:

    Hey Jeffrey, Thanks for those four lines. I looked at JH’s stuff yesterday & put in “look at over weekend” stack. With your help, I’m now over the “Hello World” hurdle!

    BTW, s$browse(“RookTest”) works for me on Win7(64), 2.13.0 under RStudio.
    -Jim

  2. Andreas Says:

    If you are not afraid of Python or web technology in general, I’d recommend you to use the python framework “Django” instead. With Numpy and the RPy2 R binding the combination of Django + R should be easier to use than rewriting web interfaces in R. There you have a template engine, form processing and even a full-blown data-entry user interface, both for simple “local” applications as well as for public-facing sites.

    • Jeffrey Breen Says:

      I hear you, Andreas, and thanks for the +1 on Django.

      My rule of thumb on web development is that if you find yourself putting HTML code in string variables, you’re probably doing something terribly, terribly wrong.

      Nevertheless, I can see the Rook package — and the Rhttpd object in particular — being just the trick to expose functions I have written which produce plots and the like.

      • Jeffrey Horner Says:

        Exactly!

        Rook is a foundation upon which I hope to see others create interesting frameworks. Hadley Wickam has already mentioned he’ll be reworking his sinartra package.

        Jeff

      • Michele Says:

        Agree! It quickly become messy and not readable. Using `brew` you can make web pages in a PHP fashion ( with the <% tags). ?Brewery for datails. I use this approach in AJAX calls to .Rhtml files as well.

        Michele

  3. Ajay Ohri Says:

    I think you may have missed a line of code here or somehow wordpress may have messed up the tag-

    s s$start(quiet=TRUE)

    fourth line
    > s$print()
    It seems it shows an extra space when I type the code directly from your blog ( the word “<- Rhttpd$new()" seems gone)

    s s
    An object of class “Rhttpd”

    > s$start(quiet=TRUE)
    > s$print()
    Server started on 127.0.0.1:29217
    [1] RookTest http://127.0.0.1:29217/custom/RookTest

    Call browse() with an index number or name to run an application.

    and fifth line
    > s$browse(1)

    nows lets check if wordpress shows the code correctly.

  4. Ajay Ohri Says:

    and finally you can use the short code within R from http://codex.wordpress.org/Writing_Code_in_Your_Posts “By default, WordPress will convert unrecognized uses of into characters which actually look like < and >, ” and http://en.support.wordpress.com/code/posting-source-code/

    your code here
    

    or Revolution’s online code highlighter at http://www.inside-r.org/pretty-r

    • Jeffrey Breen Says:

      Hi Ajay:

      Thanks for the pointer! I don’t know how WordPress lost it, but thanks to you, the key “s <- Rhttpd$new()" line is back. (It probably got lost when I added Jim's s$browse(1) edit.)

  5. Prince K. Osei Says:

    I’m trying to follow the example above, however Im getting the following:

    > library(“Rook”)
    > s s$start(quiet=TRUE)
    > s$print()
    Server stopped
    [1] RookTest http://127.0.0.1:0/custom/RookTest

    Call browse() with an index number or name to run an application.

    Any ideas or suggestion as to why it’s saying “Server Stopped” instead of “Server Started”

    • Geltmar Says:

      I had the same problem. It turned out that rstudio already started a webserver on another port. In a terminal on MacOS it looked like this
      /Applications/RStudio.app/Contents/MacOS/rsession –config-file none –program-mode desktop –www-port 28900
      Connecting to http://127.0.0.1:28900/custom/RookTest worked for me.
      For the s$browse(1) to take effect I set s$ListenPort<-28900
      The portnumber might be different in your case.


Leave a comment