Search code examples
htmlrr-rook-package

Custom HTTP Handler using Rook


I am building this app using the Rook package in R:

library(Rook) 

s <- Rhttpd$new()

s$start(quiet=T)

PIC.DIR = paste(getwd(), 'pic', sep='/')

my.app <- function(env){
  ## Start with a predefined lognormal mean and median, and allow a user to input custom values
  req <- Request$new(env)
  res <- Response$new()

  E <- 1.5
  MED <- 1
  xmax <- 5
  breaks <- 500
  ## Allow user to input custom mean/median values
  if (!is.null(req$POST())){
    E <- as.numeric(req$POST()[["mean"]])
    MED <- as.numeric(req$POST()[["median"]])
    xmax <- as.numeric(req$POST()[["xmax"]])
    breaks <- as.numeric(req$POST()[["breaks"]])
  }

  mu <- log(MED)
  sd <- sqrt(2*log(E/MED))
  MO <- exp(mu - sd^2)

  rate <- rlnorm(1000000, mu, sd)

  today <- Sys.Date()
  dt <- format(today, format="%m/%d/%y")
  sc <- paste("Source: My Source, accessed ", dt)

  png(file=paste(PIC.DIR, "/mypic.png", sep=""), width=1024, height=612)
  h1 <- hist(rate, freq=F, col="red", xlim=c(0,xmax), breaks=breaks, main="Rate",
         xlab="Rate", ylab="# of Studies", sub=sc)
  dev.off()


  res$write('<head> 
          <title> Rate Curve </title>
         </head>
        <h1>Rate Distribution Analysis</h1>')
  res$write(paste("<img src='", s$full_url("pic"), "/mypic.png'", 
        "width='1024 px' height='612 px' />", sep = ""))
  res$write('<p>
        Input Lognormal Parameters:<form method="POST"> </br>
        mean: <input type="text" name="mean" value="1.5" /> </t>
        median: <input type="text" name="median" value="1" /> </br>
        </br>
        Graphics parameters </br>
        X-axis limit: <input type="text" name="xmax" value="5" /> </t>
        Histogram breaks: <input type="text" name="breaks" value="500" /> </br>
        <input type="submit" name="Go" />\n</form>
        </p>')
  res$finish()

}

s$add(app=my.app, name='lognorm')
s$add(app = File$new(PIC.DIR), name = "pic")
s$browse('lognorm')

The browser initially loads fine, but when I try to enter different values in the inputs, I get this error:

R Custom HTTP Handler Not Found

Unable to locate custom HTTP handler for /custom/lognorm_mode

Is the package which implements this HTTP handler loaded?

Any ideas on how to solve this?


Solution

  • Instead of the last part

    s$add(app=my.app, name='lognorm')
    s$add(app = File$new(PIC.DIR), name = "pic")
    s$browse('lognorm')
    

    Try

    library(Rook)
    
    myPort <- 23845
    myInterface <- "127.0.0.1"
    status <- -1
    
    status <- .Internal(startHTTPD(myInterface, myPort))
    
    if (status == 0) {
        unlockBinding("httpdPort", environment(tools:::startDynamicHelp))
        assign("httpdPort", myPort, environment(tools:::startDynamicHelp))
        s <- Rhttpd$new()
        s$listenAddr <- myInterface
        s$listenPort <- myPort
        s$launch(name = "lognorm", app = my.app)
    }
    

    This should keep the server alive even after the web page is launched.

    (Source http://jeffreyhorner.tumblr.com/post/33814488298/deploy-rook-apps-part-ii)