1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| library(httpuv) library(urltools) library(pipeR) library(Cairo) library(png) library(lattice)
app <- list( call = function(req) { if(req$REQUEST_METHOD != "GET") return(list(status = 400L, headers = list('Content-Type' = 'plain/text'), body = "Bad Request")) params <- param_get(req$QUERY_STRING, c("x", "y", "species")) if (is.na(params$x) || is.na(params$y)) return(list(status = 400L, headers = list('Content-Type' = 'plain/text'), body = "Bad Request with wrong query string")) iris2 <- iris if (!is.na(params$species)) { if (nchar(params$species) > 0 && params$species %in% unique(iris$Species)) iris2 <- subset(iris, Species == params$species) else return(list(status = 400L, headers = list('Content-Type' = 'plain/text'), body = "Bad Request with wrong species")) } Cairo(640, 640, "/dev/null", bg = "white") print(xyplot(iris2[[params$x]], iris2[[params$y]], xlab = params$x, ylab = params$y)) output <- Cairo.capture() %>>% writePNG dev.off() return(list(status = 200L, headers = list('Content-Type' = 'image/png'), body = output)) } )
runServer("0.0.0.0", 9454, app, 250)
|