R/plumberで引数を持つAPI


UCIのWater Treatment Plant Data Setを利用して引数を持つAPIを作成してみた。

引数を持たないAPI

wtapi.R
library(plumber)
library(stringr)
library(lubridate)

x <- read.csv(file = "water-treatment.data", header = FALSE, na.strings = "?")
x$V1 <- dmy(str_sub(x$V1, start = 3))

#* @get /wt
function() {
  return(x)
}
> pr("wtapi.R") %>% pr_run(port=8000)
Running plumber API at http://127.0.0.1:8000
Argument of class Date cannot be used to set default value in OpenAPI Specifications.
Argument of class Date cannot be used to set default value in OpenAPI Specifications.
Running swagger Docs at http://127.0.0.1:8000/__docs__/

引数を持つAPI

wtapi.R
library(plumber)
library(stringr)
library(lubridate)
library(dplyr)

x <- read.csv(file = "water-treatment.data", header = FALSE, na.strings = "?")
x$V1 <- dmy(str_sub(x$V1, start = 3))

#* @get /wt
function(start=x$V1[1], end=x$V1[dim(x)[1]]) {
  x %>% filter(V1 >= start & V1 <= end) %>% return()
}
> pr("wtapi.R") %>% pr_run(port=8000)
Running plumber API at http://127.0.0.1:8000
Argument of class Date cannot be used to set default value in OpenAPI Specifications.
Argument of class Date cannot be used to set default value in OpenAPI Specifications.
Running swagger Docs at http://127.0.0.1:8000/__docs__/