Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Last active July 22, 2020 04:52
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 virtualstaticvoid/8bef2b32ae6b9665ffe1340283ffce44 to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/8bef2b32ae6b9665ffe1340283ffce44 to your computer and use it in GitHub Desktop.
Heroku Buildpack R - plumber Example

See virtualstaticvoid/heroku-buildpack-r#107

heroku create --stack heroku-16

heroku buildpacks:add https://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16

git push heroku master

heroku logs # to check

curl -v -L https://your-heroku-app-name.heroku.com/mean
curl -v -d "a=1" -d "b=2" -L https://your-heroku-app-name.herokuapp.com/sum
library(plumber)
port <- Sys.getenv('PORT')
r <- plumb("/app/myfile.R")
r$run(host='0.0.0.0', port=strtoi(port))
libxml2-dev
#
# Example R code to install packages
# See http://cran.r-project.org/doc/manuals/R-admin.html#Installing-packages for details
#
###########################################################
# Update this line with the R packages to install:
my_packages = c("plumber")
###########################################################
install_if_missing = function(p) {
if (p %in% rownames(installed.packages()) == FALSE) {
install.packages(p, dependencies = TRUE)
}
else {
cat(paste("Skipping already installed package:", p, "\n"))
}
}
invisible(sapply(my_packages, install_if_missing))
# taken from example on https://www.rplumber.io/
#* @get /mean
normalMean <- function(samples=10){
data <- rnorm(samples)
mean(data)
}
#* @post /sum
addTwo <- function(a, b){
as.numeric(a) + as.numeric(b)
}
web: R -f /app/app.R --gui-none --no-save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment