├── .gitignore ├── dockerfile ├── main.R ├── make_model.R ├── readme.md └── rest_controller.R /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata -------------------------------------------------------------------------------- /dockerfile: -------------------------------------------------------------------------------- 1 | # start from the rocker/r-ver:3.5.0 image 2 | FROM rocker/r-ver:3.5.0 3 | 4 | # install the linux libraries needed for plumber 5 | RUN apt-get update -qq && apt-get install -y \ 6 | libssl-dev \ 7 | libcurl4-gnutls-dev 8 | 9 | # install plumber 10 | RUN R -e "install.packages('plumber')" 11 | 12 | # copy everything from the current directory into the container 13 | COPY / / 14 | 15 | # open port 80 to traffic 16 | EXPOSE 80 17 | 18 | # when the container starts, start the main.R script 19 | ENTRYPOINT ["Rscript", "main.R"] -------------------------------------------------------------------------------- /main.R: -------------------------------------------------------------------------------- 1 | library(plumber) 2 | 3 | r <- plumb("rest_controller.R") 4 | r$run(port=80, host="0.0.0.0") -------------------------------------------------------------------------------- /make_model.R: -------------------------------------------------------------------------------- 1 | # load the dataset 2 | dataset <- iris 3 | 4 | # create the model 5 | model <- lm(Petal.Length ~ Petal.Width, data = dataset) 6 | 7 | # # example: run the model once 8 | # prediction_data <- data.frame(Petal.Width=1) 9 | # predict(model,prediction_data) -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # R API Tutorial (with docker!) 2 | 3 | This code is what was used in our blog posts on [using R to make an API](https://medium.com/@heathernolis/r-can-api-c184951a24a3) and [using R with docker](https://medium.com/@skyetetra/using-docker-to-deploy-an-r-plumber-api-863ccf91516d). 4 | 5 | The three R files are used to create a simple API with the plumber R library. The dockerfile can be used to take the API and create a docker container with it. 6 | 7 | To run the API directly, run the `main.R` script (or deploy the container). Then go to http://127.0.0.1/predict_petal_length?petal_width=1 to run the model on a petal width of 1. 8 | -------------------------------------------------------------------------------- /rest_controller.R: -------------------------------------------------------------------------------- 1 | 2 | # build the model 3 | source("make_model.R") 4 | 5 | #* @get /predict_petal_length 6 | get_predict_length <- function(petal_width){ 7 | 8 | # convert the input to a number 9 | petal_width <- as.numeric(petal_width) 10 | 11 | #create the prediction data frame 12 | prediction_data <- data.frame(Petal.Width=petal_width) 13 | 14 | # create the prediction 15 | predict(model,prediction_data) 16 | } 17 | --------------------------------------------------------------------------------