Skip to content

Instantly share code, notes, and snippets.

@robkooper
Created March 5, 2020 21:12
Show Gist options
  • Save robkooper/163f6e8542e94c835a9abff9770354bf to your computer and use it in GitHub Desktop.
Save robkooper/163f6e8542e94c835a9abff9770354bf to your computer and use it in GitHub Desktop.
library(plumber)
# create the actual app
master_pr <- plumber$new()
# list of all files with endpoints, suggest to keep one file per level
# each file needs a unique endpoint
f1_pr <- plumber$new("plumber1.R")
master_pr$mount("/f1", f1_pr)
f2_pr <- plumber$new("plumber2.R")
master_pr$mount("/f2", f2_pr)
# start the app
master_pr$run(port=8000)
# plumber.R
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg=""){
list(msg = paste0("The message is: '", msg, "'"))
}
#* Plot a histogram
#* @png
#* @get /plot
function(){
rand <- rnorm(100)
hist(rand)
}
#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b){
as.numeric(a) + as.numeric(b)
}
# plumber.R
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg=""){
list(msg = paste0("The message is: '", msg, "'"))
}
#* Plot a histogram
#* @png
#* @get /plot
function(){
rand <- rnorm(100)
hist(rand)
}
#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b){
as.numeric(a) + as.numeric(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment