{"id":14067891,"url":"https://github.com/beeva-jorgezaldivar/plumberModel","last_synced_at":"2025-07-30T02:32:06.922Z","repository":{"id":223339774,"uuid":"136940402","full_name":"beeva-jorgezaldivar/plumberModel","owner":"beeva-jorgezaldivar","description":"Create APIs for the deployment of R models with minimal code","archived":false,"fork":false,"pushed_at":"2018-09-11T10:07:31.000Z","size":2451,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-13T07:13:57.988Z","etag":null,"topics":["api","caret","data-science","deployment","machine-learning","plumber","r"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beeva-jorgezaldivar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-11T14:47:45.000Z","updated_at":"2021-01-11T07:06:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"aac4dd93-79a8-49cf-9600-4b3408895e63","html_url":"https://github.com/beeva-jorgezaldivar/plumberModel","commit_stats":null,"previous_names":["beeva-jorgezaldivar/plumbermodel"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeva-jorgezaldivar%2FplumberModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeva-jorgezaldivar%2FplumberModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeva-jorgezaldivar%2FplumberModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beeva-jorgezaldivar%2FplumberModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beeva-jorgezaldivar","download_url":"https://codeload.github.com/beeva-jorgezaldivar/plumberModel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228075632,"owners_count":17865508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","caret","data-science","deployment","machine-learning","plumber","r"],"created_at":"2024-08-13T07:05:49.896Z","updated_at":"2024-12-04T08:31:18.848Z","avatar_url":"https://github.com/beeva-jorgezaldivar.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"# plumberModel\n\nPackage that helps to deploy a trained model to production whit minimal code.\nIt creates an API using plumber package with several useful endpoints.\n\n```r\nlibrary(tidyverse)\nlibrary(caret)\nlibrary(plumberModel)\n\nmodel \u003c- train(iris %\u003e% select(-Species), iris$Species)\napi \u003c- PlumberModel$new(model)\napi$run(port = 8000)\n```\n\n## 1. Available endpoints\n\n### `GET` /modelInfo\n\nReturns a JSON with basic information about the model.\n\n```json\n{\n  \"name\":[\"Unnamed model\"],\n  \"method\":[\"rf\"],\n  \"type\":[\"Regression\"],\n  \"version\":[\"1.0.0\"],\n  \"hyperParameters\":[{\"mtry\":2}]\n}\n```\n\n### `GET` /trainResults\n\nReturns the metrics calculated for the trained model.\n\n```json\n[\n  {\"Metric\":\"RMSE\",\"Value\":0.2799},\n  {\"Metric\":\"Rsquared\",\"Value\":0.9748},\n  {\"Metric\":\"MAE\",\"Value\":0.2154},\n  {\"Metric\":\"RMSESD\",\"Value\":0.0238},\n  {\"Metric\":\"RsquaredSD\",\"Value\":0.004},\n  {\"Metric\":\"MAESD\",\"Value\":0.0181}\n]\n```\n\n### `GET` /inputFeatures\n\nReturns the features of the data used to train the model and some information\nabout them.\n\n```json\n{\n  \"Sepal.Length\":{\"class\":[\"numeric\"],\"mean\":[5.8433],\"std\":[0.6857]},\n  \"Sepal.Width\":{\"class\":[\"numeric\"],\"mean\":[3.0573],\"std\":[0.19]},\n  \"Petal.Width\":{\"class\":[\"numeric\"],\"mean\":[1.1993],\"std\":[0.581]},\n  \"Species\":{\"class\":[\"factor\"],\"levels\":[\"setosa\",\"versicolor\",\"virginica\"]}\n}\n```\n\n### `GET` /predict\n\nPredicts using query params as features. The name of each param must match\nwith the name of an input variable.\n\nAn example query would be:\n\n```url\npredict?Sepal.Length=5.0\u0026\u0026Sepal.Width=3.5\u0026\u0026Petal.Width=1.21\u0026\u0026Species=setosa\n```\n\nReturns model predictions as a JSON array.\n\n```json\n  [2.5505]\n```\n\n### `POST` /predict\n\nIt has the same behavior as the `GET` version, but uses POST body as input. \n\nThe body must be a JSON with the following structure:\n\n```json\n[\n  {\"Sepal.Length\":5.1,\"Sepal.Width\":3.5,\"Petal.Width\":0.2,\"Species\":\"setosa\"},\n  {\"Sepal.Length\":4.9,\"Sepal.Width\":3,\"Petal.Width\":0.2,\"Species\":\"setosa\"},\n  {\"Sepal.Length\":4.7,\"Sepal.Width\":3.2,\"Petal.Width\":0.2,\"Species\":\"setosa\"}\n]\n```\n\nThe response would be:\n\n```json\n  [1.4379,1.4549,1.4437]\n```\n\n## 2. Adding custom endpoins\n\nPlumberModel objects are [R6 classes](https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html)\nthat inherits from plumber class.\n\nIn order to add custom endpoints one would have to define a subclass.\n\nThe following example adds a new '/helloWorld' endpoint:\n\n```r\nCustomPlumberModel \u003c- R6Class(\n  classname = \"CustomPlumberModel\",\n  inherit = PlumberModel,\n  public = list(\n    initialize = function(mdl){\n      super$initialize(mdl)\n      self$handle(\"GET\", \"/helloWorld\", function(req, res){\n        \"hello world!\"\n      })\n    }\n  )\n)\n```\n\nYou can find more information about plumber and its quirks on \n[plumber documentation](https://www.rplumber.io/docs/programmatic-usage.html#defining-endpoints)\n\n## 3. Add support for custom trained models\n\nBy default PlumberModel only supports models trained with caret library. In\norder to make it work with custom models it is necessary to implement several\n[generic S3 functions](http://adv-r.had.co.nz/S3.html).\n\nThe following example would work with a custom model with class 'customModel'\n\n```r\n\n#' Gets basic info about the model.\n#' @param mdl Object with class 'customModel'.\n#' @return Named list with the custom information about the model\nmodelInfo.customModel \u003c- function(mdl){\n  ...\n}\n\n#' Returns a description of each input variable of the model.\n#' @param mdl Object with class 'customModel'.\n#' @return Named list with the following structure:\n#' list(\n#'  \u003cvar_name_1\u003e = list(class = [\"numeric\"]), \u003cother_info\u003e = ..., ...),\n#'  \u003cvar_name_2\u003e = list(class = [\"factor\"], \u003clevels\u003e = [\"lvl1\", \"lvl2\"], ...)\n#¡ )\ninputFeatures.customModel \u003c- function(mdl){\n  ...\n}\n\n#' Gets the metrics of the model.\n#' @param mdl Object with class 'customModel'.\n#' @return Data.frame with the metrics.\ntrainResults.customModel \u003c- function(mdl){\n  ...\n}\n\n#' Predicts using the model.\n#' @param mdl Object with class 'customModel'.\n#' @return Vector with the predictionss\npredict.customModel \u003c- function(mdl){\n  ...\n}\n\n```\n\n## 4. Visualización del estado del modelo\n\nOptionally, you cand build a static web server on top of the API, for monitoring\nthe model.\nYou must use the class `PlumberModelWebApp` and access the index url '/'.\n\n```r\nlibrary(tidyverse)\nlibrary(caret)\nlibrary(plumberModel)\n\nmodelo \u003c- train(iris %\u003e% select(-Species), iris$Species)\napi \u003c- PlumberModelWebApp$new(modelo)\napi$run(port = 8000)\n```\n\n![ejemplo front-end](doc/img/webapp-example.png)\n\n## 5. Ejemplos\n\nYou can find examples in the [examples folder](doc/examples/).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeeva-jorgezaldivar%2FplumberModel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeeva-jorgezaldivar%2FplumberModel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeeva-jorgezaldivar%2FplumberModel/lists"}