Skip to content

Instantly share code, notes, and snippets.

@psolymos
Last active April 3, 2023 19:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psolymos/284b43b8dd0583b33ca7fc7dcf71082b to your computer and use it in GitHub Desktop.
Save psolymos/284b43b8dd0583b33ca7fc7dcf71082b to your computer and use it in GitHub Desktop.
Hello world examples with different R web frameworks
# httpuv
library(httpuv)
handle <- function(req) {
input <- req[["rook.input"]]
postdata <- input$read_lines()
jsonlite::toJSON(paste0("Hello ",
jsonlite::fromJSON(paste(postdata)), "!"))
}
httpuv::runServer(
host = "0.0.0.0",
port = 8080,
app = list(
call = function(req) {
list(
status = 200L,
headers = list("Content-Type" = "application/json"),
body = handle(req))
}
)
)
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# plumber
library(plumber) # load plumber
handle <- function(req) {
paste0("Hello ", jsonlite::fromJSON(paste(req$postBody)), "!")
}
pr() |>
pr_post(
"/",
handler = handle,
serializer = serializer_unboxed_json()) |>
pr_run(host = "0.0.0.0", port = 8080)
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# fiery - longer
library(fiery)
library(reqres)
handle <- function(request) {
jsonlite::toJSON(paste0("Hello ", request$body, "!"))
}
app <- Fire$new(host = '0.0.0.0', port = 8080L)
app$on("request", function(server, id, request, ...) {
OK <- request$parse(json = parse_json())
response <- request$respond()
if (OK) {
result <- try(handle(request))
if (inherits(result, "try-error")) {
response$body <- jsonlite::toJSON(result)
response$status <- 400L
} else {
response$body <- result
response$status <- 200L
}
} else {
response$body <- jsonlite::toJSON("Error: wrong input")
response$status <- 400L
}
response$type <- "application/json; charset=utf-8"
})
app$ignite()
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# fiery - shorter with routr
library(fiery)
library(reqres)
library(routr)
handle <- function(request, response, keys, ...) {
request$parse(json = parse_json())
response$status <- 200L
response$type <- "application/json; charset=utf-8"
response$body <- jsonlite::toJSON(paste0("Hello ", request$body, "!"))
return(FALSE)
}
app <- Fire$new(host = "0.0.0.0", port = 8080L)
route <- Route$new()
route$add_handler("post", "/", handle)
router <- RouteStack$new()
router$add_route(route, "hello")
app$attach(router)
app$ignite()
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# beakr
library(beakr)
handle <- function(req, res, err) {
paste0("Hello ", jsonlite::fromJSON(paste(req$body)), "!")
}
newBeakr() |>
httpPOST(
path = "/",
decorate(
FUN = handle,
content_type = "application/json"
)
) |>
handleErrors() |>
listen(host = "0.0.0.0", port = 8080)
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# ambiorix
# not on CRAN (archived)
# remotes::install_github("devOpifex/ambiorix")
library(ambiorix)
options(ambiorix.host = "0.0.0.0", ambiorix.port = 8080)
app <- Ambiorix$new()
handle <- function(body) {
paste0("Hello ", body, "!")
}
app$post("/", function(req, res){
res$json(handle(parse_json(req)))
})
app$start(open = FALSE)
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# RestRserve
library(RestRserve)
handle = function(.req, .res) {
.res$set_body(paste0("Hello ", .req$body, "!"))
}
app = Application$new(
content_type = "application/json")
app$add_post(
path = "/",
FUN = handle)
backend = BackendRserve$new()
backend$start(app, http_port = 8080)
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# opencpu
library(opencpu)
ocpu_start_server(
port = 8080,
root = "/",
workers = 1,
preload = NULL,
on_startup = NULL,
no_cache = FALSE)
# curl http://localhost:8080/library/base/R/paste/json \
# -H 'Content-Type: application/json' \
# -d '{"x":["Hello","Friend","!"],"collapse":" "}'
# Rserve
library(Rserve)
.http.request <- function(url, query, body, headers) {
paste0("Hello ", jsonlite::fromJSON(rawToChar(body)), "!")
}
Rserve::run.Rserve(http.port = 8080)
# curl http://localhost:8080/ -d '["Friend"]' \
# -H 'Content-Type: application/json'
# Rook
library(Rook)
app <- function(env){
req <- Request$new(env)
input <- fromJSON(rawToChar(req$body()$read()))
res <- Response$new()
res$write(toJSON(paste0("Hello ", input, "!")))
res$finish()
}
s <- Rhttpd$new()
s$add(RhttpdApp$new(app = app, name = "hello"))
s$start(listen = "127.0.0.1", port = 8080)
# curl -d '["Friend"]' -H 'Content-Type: application/json' \
# http://127.0.0.1:8080/custom/hello
#shiny
library(shiny)
ui <- fluidPage(
mainPanel(
fluidRow(
h2("Curl"),
textOutput("url")
),
fluidRow(
h2("Message"),
textOutput("msg")
)
)
)
server <- function(input, output, session) {
x <- reactiveVal()
url <- session$registerDataObj(
name = "hello",
data = list(),
filter = function(data, req) {
body <- jsonlite::fromJSON(rawToChar(req$rook.input$read()))
x(paste0("Hello ", body, "!"))
shiny::httpResponse(
status = 200L,
content_type = "application/json",
content = jsonlite::toJSON(
paste0("Hello ", body, "!")))
}
)
output$url <- renderText({
paste0("curl -d '[\"Friend\"]' -H 'Content-Type: application/json' http://127.0.0.1:8080/", url)
})
output$msg <- renderText({x()})
}
runApp(shinyApp(ui, server), port=8080)
# go to http://127.0.0.1:8080 and copy the curl
# check download stats
library(ggplot2)
library(dlstats)
pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "Rook", "shiny")
x <- cran_stats(pkg)
ggplot(x, aes(x = end, y = downloads, group=package, color=package)) +
geom_line() +
facet_wrap(~package) +
theme_minimal()
@psolymos
Copy link
Author

First publication dates:

  • Rserve 2006-11-16
  • Rook 2011-04-24
  • shiny 2012-12-01
  • httpuv 2013-03-11
  • opencpu 2013-09-12
  • plumber 2016-04-14
  • fiery 2016-07-04
  • beakr 2020-02-10
  • RestRserve 2020-03-02
  • ambiorix 2021-01-27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment