使用Rook的自定义HTTP处理程序

时间:2012-11-05 16:12:31

标签: html r rook

我正在使用R:

中的Rook包构建此应用程序
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')

浏览器最初加载正常,但是当我尝试在输入中输入不同的值时,我收到此错误:

  

未找到自定义HTTP处理程序

     

无法找到/ custom / lognorm_mode

的自定义HTTP处理程序

是否加载了实现此HTTP处理程序的包?

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:2)

而不是最后一部分

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

尝试

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)
}

即使在网页启动后,这也应该使服务器保持活动状态。

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