plumber icon indicating copy to clipboard operation
plumber copied to clipboard

Improve docs regarding shipping APIs in R packages

Open avhz opened this issue 9 months ago • 0 comments

I am writing an API and shipping it in an R package, as described briefly here:

https://www.rplumber.io/reference/plumb_api.html

It would be helpful to have a bit more information on how this is done.

In particular, for my case, I am interested in how I would register an exit handler, as described here:

https://www.rplumber.io/articles/execution-model.html#exit-handlers

Since I would call the package's API like:

plumber::plumb_api("pkg", "api") |> plumber::pr_run()

Do I register the exit handler in the API file inside the package ? Or do I do it when I 'plumb' the package's API, like so:

plumber::plumb_api("pkg", "api") |> 
    plumber::pr_hook("exit", function(){ print("Bye bye!")})
    plumber::pr_run()

What if the exit handler depends on an object defined in the package API ? e.g.

plumber::plumb_api("pkg", "api") |> 
    plumber::pr_hook("exit", function(){ dbDisconnect(connection) }) ## <- 'connection' does not exist here
    plumber::pr_run()

avhz avatar Mar 19 '25 12:03 avhz