Ï
Jeff Allen / @trestleJeff
http://plumber.trestletech.com
Slides hosted at http://plumber.trestletech.com/components/earl-2015/
buildModel <- function(myData){
mcmcTheKmeansLogarithm(myData)
}
forecastSales <- function(date){
linearModel(sales, date)
}
makePlot <- function(){
par(mar = rep(2, 4)) # margins
dates <- seq(as.Date("2015-10-01"),
as.Date("2015-10-31"), by=1)
sold <- 1:31
plot(dates, sold, type="b", main="Sales")
}
#* @get /salesgraph
#* @png
makePlot <- function(){
par(mar = rep(2, 4)) # margins
dates <- seq(as.Date("2015-10-01"),
as.Date("2015-10-31"), by=1)
sold <- 1:31
plot(dates, sold, type="b", main="Sales")
}
sales <- NULL
#* @post /addsale
function(qty, item, id){
sales <<- rbind(sales, data.frame(
id = id,
time = Sys.time(),
item = item,
qty = qty
))
id
}
GET plumber.trestletech.com/sale?qty=2
#* @get /sale
function(qty){
qty == "2" # TRUE
}
POST plumber.trestletech.com/ HTTP/1.1
qty=3
#* @post /
function(qty){
qty == "3" # TRUE
}
req
and res
#* @get /
function(req, res){
ip <- req$REMOTE_ADDR
res$setHeader("Last-Modified",
"Fri, 16 Oct 2015 12:45:26 GMT")
# ...
}
library(plumber)
pr <- plumber::plumb("myfile.R")
pr$run(port=8000)
#* @post /transaction
function(item, qty, id){
sales <<- rbind(sales, list(time=Sys.time(),
item=item, qty=qty, id=id))
}
POST myplumber.com/transaction HTTP/1.1
item=black-hat&qty=3&id=429
#* @get /transaction/<id>
function(id){
id <- as.integer(id)
sales[sales$id == id, ]
}
GET myplumber.com/transaction/429 HTTP/1.1
[{"id":429,"time":"2015-10-16 20:52:06",
"item":"blue-hat","qty":"3"}]
#* @get /transaction/<id:int>
function(id){
sales[sales$id == id, ]
}
GET myplumber.com/transaction/429 HTTP/1.1
[{"id":429,"time":"2015-10-16 20:52:06",
"item":"blue-hat","qty":"3"}]
#* @get /hats/<color>/size/<size:int>
function(color, size){
hats[hats$color == color
& hats$size == size]
}
#* @get /transaction/<id>
function(id){
id <- as.integer(id)
sales[sales$id == id, ]
}
#* @get /transaction/plot
#* @png
function(id){
plot(sales$date, sales$qty,
main="Qty/Purchase Over Time",
xlab="Date", ylab="Qty")
}
Returns a PNG image of your graph
#* @filter logger
function(req){
print(paste0(date(), " - ",
req$REMOTE_ADDR, " - ",
req$REQUEST_METHOD, " ",
req$PATH_INFO))
forward()
}
#* @filter setuser
function(req){
un <- req$HTTP_COOKIE
# Make req$username available to endpoints
req$username <- un
forward()
}
#* @filter nochrome
function(req){
if (!grepl("Chrome", req$HTTP_USER_AGENT)){
forward()
} else {
res$status <- 400
res$body <- "NOT WELCOME HERE!"
}
}