0

I am struggling to find a solution that httr can do the same thing as plumber in R. In plumber here's the code that works:

1). Create a script hello_world_plumber.R

#' @post /hello_world
hi <- function(name) {
  return(paste0('Hello ', name, '!'))
}

2). In another script:

pr <-  plumb(file = '~/hello_world_plumber.R')
pr$run(swagger = F)

Starting server to listen on port 5852
Running the swagger UI at http://127.0.0.1:5852/__swagger__/

3). In the terminal

curl -s --data 'Matt' http://127.0.0.1:5852/hello_world
Out: Hello Matt!
Matt Elgazar
  • 707
  • 1
  • 8
  • 21
  • So you're trying to do `plumber` serving of API endpoints completely in `httr`? Is that reinventing the wheel? – r2evans May 08 '20 at 18:03
  • Is it that difficult to do what plumber does purely in httr? httr has way more daily downloads, so it makes me feel like plumber can get deprecated easily. – Matt Elgazar May 08 '20 at 18:15
  • 1
    I might be misguided, but I tend to think of `httr`'s strength in *retrieving* data (possibly with attached data), not *serving* data. `plumber` is doing a *lot* of handling under the hood, on top of merely answering queries with the output from an R function. I'm sure you could glue together some of `httpuv` to serve things in a different and perhaps smaller way. – r2evans May 08 '20 at 18:19
  • (BTW: I don't see any functions in `httr`, exported or internal, that include a semblance of "serve". That's not definitive by any stretch, and I'm not a guru on `httr`, but it is a data point.) – r2evans May 08 '20 at 18:20
  • I'm new to this as well.. I might be misguided too – Matt Elgazar May 08 '20 at 18:21
  • https://xkcd.com/1053/. Either I'm part of the 10000 or you are, I don't know yet which :-) (All too many times I've been *certain* about something until somebody proved how wrong I was.) – r2evans May 08 '20 at 18:22

1 Answers1

1

plumber is built on top of the httpuv library to help you define an API quickly with a few decorations.

httr package is to send http queries via the curl command.

So httr is like a client and plumber is your server. That's why there is more downloads. There is way more Chrome/Firefox download than Nginx/Apache, it is just the nature of the tools.

You could achieve the same thing without plumber just using httpuv if you don't care about decoration, async, query string and body parsing, openapi and other goodies. it is pretty bare though.

runServer("127.0.0.1", 8477,
  list(
    call = function(req) {
      list(
        status = 200L,
        headers = list(
          'Content-Type' = 'text/html'
        ),
        body = "Hello world!"
      )
    }
  )
)

There are multiple plumber alternatives too, RestRserve, OpenCPU to name a few.

Depending on your use case, you select what works best for you. If you need any plumber help, feel free to ask. I don't see it going away soon as it has been incorporated into RStudio IDE and I think it's decoration structure a la roxygen2 makes it simple to adopt.

Bruno Tremblay
  • 756
  • 4
  • 9
  • Wow great thanks for the detailed answer! plumber looks great and I hope it doesn't get deprecated like many other R packages that were widely used (ehm caret) – Matt Elgazar May 09 '20 at 01:53
  • I believe `caret` has a sibling `parsnip` in the tidyverse. In the space `caret` and `parsnip` are playing, you can get behind cutting edge in a matter of months, and it is entirely subjective to your work. Both packages are still updated more frequently than some paid software. – Bruno Tremblay May 10 '20 at 18:39
  • Hey @Bruno Tremblay I just posted another plumber question. It'd be great if you knew how to solve that one! Thanks – Matt Elgazar May 15 '20 at 22:08