2

I would like to play with the plumber library by making an app that takes 14 days of historical data and returns an exponential smoothing forecast.

The problem is I am somewhat unfamiliar with passing a lot of data (a parameter with multiple values) to an API. My questions can be summarized as follows:

  1. How should I prepare the data in R to be passed to the API?

  2. How should the API be prepared in plumber to receive time series data?

Below is some example data and a function which accomplishes what I would like in R.

library(tidyverse)

# data to be passed to API
head(forecast::wineind,14)
#>        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov
#> 1980 15136 16733 20016 17708 18019 19227 22893 23739 21133 22591 26786
#> 1981 15028 17977                                                      
#>        Dec
#> 1980 29740
#> 1981

#* Return Forecast Data
#* @list a The first number
#* @get /simple_fcast
function(){
  ts() %>% 
  forecast::ets() %>% 
  forecast::forecast()
}
#> function(){
#>   ts() %>% 
#>   forecast::ets() %>% 
#>   forecast::forecast()
#> }

Created on 2018-11-14 by the reprex package (v0.2.1)

Alex
  • 2,603
  • 4
  • 40
  • 73

1 Answers1

2

The answer is to use "message body" per the documentation here https://www.rplumber.io/docs/routing-and-input.html#

#' @post /user
function(req, id, name){
  list(
    id = id,
    name = name,
    raw = req$postBody
  )
}

Running curl --data "id=123&name=Jennifer" "http://localhost:8000/user" will return:

{
  "id": [123],
  "name": ["Jennifer"],
  "raw": ["id=123&name=Jennifer"]
}

I found that arrays can also be passed to functions.

Alex
  • 2,603
  • 4
  • 40
  • 73