Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

decorate

Decorate a function for use in a web service


Description

The decorate() function can be used to prepare a function for easy use in a beakr pipeline.

Decorating a function associates the specified function and its parameters with req, res, and err objects and assigns a content-type to the response object. This prepares a standard R function to be used in Beakr instances and accept requests.

Usage

decorate(FUN, content_type = "text/html", strict = FALSE)

Arguments

FUN

Function to decorate.

content_type

HTTP "content-type" of the function output. (e.g. "text/plain", "text/html" or other mime type)

strict

Boolean, requiring strict parameter matching.

Value

A decorated middleware function.

Examples

library(beakr)

# Create an new Beakr instance
beakr <- newBeakr()

# Create simple hello and goodbye function
hello <- function(name) { paste0("Hello, ", name, "!") }
goodbye <- function(text = "Adios") { paste0(text, ", dear friend.") }

# Create a web service from these functions
beakr %>%

  httpGET(path = "/hello", decorate(hello)) %>%

  httpGET(path = "/goodbye", decorate(goodbye)) %>%

  handleErrors() %>%

  listen(host = '127.0.0.1', port = 25118, daemon = TRUE)

# ------------------------------------------------------------
# POINT YOUR BROWSER AT:
# * http://127.0.0.1:25118/hello?name=Honeydew
# * http://127.0.0.1:25118/goodbye?text=Sionara
#
# THEN, STOP THE SERVER WITH stopServer(beakr)
# ------------------------------------------------------------

# Stop the beakr instance server
stopServer(beakr)

beakr

A Minimalist Web Framework for R

v0.4.3
GPL-3
Authors
Hans Martin [aut], Jonathan Callahan [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.