[R] [R-pkgs] Rook: software and specification for R web applications and servers

Peter K peter.kharchenko at gmail.com
Thu Jun 26 18:32:33 CEST 2014


Rook apps can be run using the httpd built into R (great feature!), and can 
be deployed using rApache server. 
For those who prefer to deploy the apps under FastRWeb, below is the code 
to do so:

-- in your web.R/myrookapp.R file, we ran the Rook app instance (stored in 
variable app): 

run <- function( ... ) {
  run_rook_app(app);
}

-- somewhere from code/rserve.R source the following definitions:
library(Rook)
run_rook_app <- function(app,env=build_rook_env(request)) {
  res <- app$call(env);
  if (inherits(res,'try-error')){
    warning('App returned try-error object')
    str(res)
    return(paste("ERROR!",res,capture.output(traceback())))
  }
  ctype <- res$headers$`Content-Type`;
  res$headers$`Content-Type` <- NULL;
  headers <- unlist(lapply(names(res$headers),function(nam) paste(nam,": 
",res$headers[[nam]],sep="")))

  if (!is.null(names(res$body)) && names(res$body)[1] == 'file'){
    # send fille
   
 return(WebResult(cmd="file",payload=res$body[1],content.type=ctype,headers=headers))
  } else {
    if ((is.character(res$body) && nchar(res$body)>0)) {
      # plain text response
      
 return(WebResult(cmd="html",payload=res$body,content.type=ctype,headers=headers))
    } else if (is.raw(res$body) && length(res$body)>0) {
     
 return(WebResult(cmd="raw",payload=res$body,content.type=ctype,headers=headers))
    }
  }
}


run_rook_app <- function(app,env=build_rook_env(request)) {
  res <- app$call(env);
  if (inherits(res,'try-error')){
    warning('App returned try-error object')
    str(res)
    return(paste("ERROR!",res,capture.output(traceback())))
  }
  ctype <- res$headers$`Content-Type`;
  res$headers$`Content-Type` <- NULL;
  headers <- unlist(lapply(names(res$headers),function(nam) paste(nam,": 
",res$headers[[nam]],sep="")))

  if (!is.null(names(res$body)) && names(res$body)[1] == 'file'){
    # send fille
    #print("multipart file response")
   
 return(WebResult(cmd="file",payload=res$body[1],content.type=ctype,headers=headers))
    #sendBin(readBin(res$body[1],'raw',n=file.info(res$body[1])$size))
  } else {
    if ((is.character(res$body) && nchar(res$body)>0)) {
      # plain text response
      #print("plaint text response")
     
 return(WebResult(cmd="html",payload=res$body,content.type=ctype,headers=headers))
    } else if (is.raw(res$body) && length(res$body)>0) {
      #print("raw response")
     
 return(WebResult(cmd="raw",payload=res$body,content.type=ctype,headers=headers))
    }
  }
}

.FastRWebInputStream <- setRefClass(
  '.FastRWebInputStream',
  fields=c('con','body'),
  methods = list(
    initialize = function(request) {
      body <<- request$body;
      if(!is.null(body) && is.raw(body)) {
        con <<- rawConnection(request$body)
      } else {
        con <<- NULL;
      }
    },
    read_lines = function(n = -1L){
      if (n<=0 || is.null(body) || is.null(con)) return(character())
      readLines(con,n=n,warn=FALSE)
    },
    read = function(l = -1L){
      if (is.null(body) || is.null(con)) return(character())
      body;
    },
    rewind = function(){
      if(!is.null(body) && is.raw(body))      
        con <<- rawConnection(body)
    })
  )






-peter.

On Wednesday, January 11, 2012 5:20:04 PM UTC-5, Jeffrey Horner wrote:
>
> Dear  useRs,
>
> Rook version 1.0-3 has been submitted to CRAN. In the mean time you
> can get it here:
>
> https://github.com/jeffreyhorner/rRack/blob/master/Rook_1.0-3.tar.gz
>
> The latest release contains support for deployment with rApache. Please see
> 3.6.5 and 3.6.6 under section 'Configuring rApache' in the manual:
>
> http://www.rapache.net/manual.html#Configuring_rapache
>
> What is Rook? A package that does three things:
>
> - It provides a way to run R web applications on your desktop with the
> new internal R web server named Rhttpd. Please see the Rhttpd help page.
>
> - It provides a set of reference classes you can use to write you R
> web applications. The following help pages provide more information:
> Brewery, Builder, File, Middleware, Redirect, Request, Response, Static,
> URLMap, and Utils. Also see the example web applications located in
> 'system("exampleApps",package="Rook")'.
>
> - It provides a specification for writing your R web applications to
> work on any web server that supports the Rook specification.
>
> You may not see the need for web applications written in R, but consider
> using Rook to build a statistical engine that complements a front-end
> web system, or consider creating elegant ggplot2 graphics on-demand from
> a fresh data stream. Also, consider creating dynamic instructional content
> for the classroom.
>
> If you have other examples or ideas, please join in the discussion on
> R-help or here:
>
> http://groups.google.com/group/rrook
>
> --
> Jeffrey Horner
>
> _______________________________________________
> R-packages mailing list
> R-pac... at r-project.org <javascript:>
> https://stat.ethz.ch/mailman/listinfo/r-packages
>
> ______________________________________________
> R-h... at r-project.org <javascript:> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


More information about the R-help mailing list