Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@seandavi
Last active June 12, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seandavi/b9e76732df4a7f2c827b372cafc79d1e to your computer and use it in GitHub Desktop.
Save seandavi/b9e76732df4a7f2c827b372cafc79d1e to your computer and use it in GitHub Desktop.
Example of using R to build a working web API
pr <- plumber::plumb("plumber.R")
pr$run(port=8088)
# plumber.R
#' Echo the parameter that was sent in
#' @param msg The message to echo back.
#' @get /echo
function(msg=""){
print('Message endpoint called with message:')
print(msg)
list(msg = paste0("The message is: '", msg, "'"))
}
#' Plot out data from the iris dataset
#' @param spec If provided, filter the data to only this species (e.g. 'setosa')
#' @get /plot
#' @png
function(spec){
myData <- iris
title <- "All Species"
print('Plot endpoint called:')
# Filter if the species was specified
if (!missing(spec)){
title <- paste0("Only the '", spec, "' Species")
myData <- subset(iris, Species == spec)
}
print(title)
plot(myData$Sepal.Length, myData$Petal.Length,
main=title, xlab="Sepal Length", ylab="Petal Length")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment