plumber api deployed to Connect with entrypoint.r

I am trying to use plumber to deploy an API which uses a SQL connection.
I'd like to use the pool package to create pooled database connections. To do this, I think I need to be able to register a hook. To do that, I think I need to be able to use entrypoint.R when deploying to Rstudio Connect as in https://www.rplumber.io/docs/runtime.html#exit-handlers.

Issue 1.

I have copied the example files from https://github.com/trestletech/plumber/tree/master/inst/examples/12-entrypoint but they still don't seem to work.

plumber::plumb("entrypoint.R")
# Plumber router with 0 endpoints, 4 filters, and 0 sub-routers.
# Call run() on this object to start the API.
├──[queryString]
├──[postBody]
├──[cookieParser]
├──[sharedSecret]
├──/ ()

where as:

plumber::plumb("myplumberapi.R")
# Plumber router with 1 endpoint, 4 filters, and 0 sub-routers.
# Call run() on this object to start the API.
├──[queryString]
├──[postBody]
├──[cookieParser]
├──[sharedSecret]
└──/counter (GET)

How would that directory get deployed to Rstudio Connect or run locally?

Per the documentation for plumber::plumb():

if an entrypoint.R file is found, it will take precedence and be responsible for returning a runnable Plumber router

This means that entrypoint.R should be a file that returns a runnable Plumber router. See the following example files:

# plumber.R
library(plumber)

#* @apiTitle Entrypoint example

#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg = "") {
    list(msg = paste0("The message is: '", msg, "'"))
}
# entrypoint.R
pr <- plumb("plumber.R")

# Register an exit handler
pr$registerHook("exit", function(){
  print("See ya later")
})

pr

To run this API with the hook that is registered in entrypoint.R, you would run plumber::plumb(dir = "path/to/api/directory")$run() and Plumber would then recognize the entrypoint.R file. Alternatively, recent versions of RStudio provide an option to "Run API" when editing a plumber file, and this will automatically invoke the command referenced earlier.

When it comes to publishing to RStudio Connect, you should be able to click on the publish button while editing plumber.R. This will bring up the following options:

If you include an entrypoint.R file in your deployment (like what is shown), RStudio Connect will honor that file and use it to run the API, which will register any hooks that you've defined in entrypoint.R.

Hopefully this helps! If you have other questions or if I missed the mark here, let me know.

1 Like

I still get the same issue.

Can you look at https://github.com/harrismcgehee/plumber_reprex to see if you notice something?

Specifically:

# deploy.R

plumber::plumb(dir = getwd())$run()
Error in plumber::plumb(dir = getwd()) : 
  No plumber.R file found in the specified directory: /work/rstudio_community_reprex

Your file must be named plumber.R. If you re-name notplumber.R to plumber.R, then it should work.

4 Likes

You can run plumber::plumb('notplumber.R') by itself.

This wasn't correct:

If you debugonce(plumber::plumb) and then plumber::plumb(dir = getwd())$run() you see that it isn't even looking for the entrypoint.R.

And what I said wasn't actually true.
I see that it works if I name the file plumber.R!

Why can you run plumber::plumb('notplumber.R') by itself, but not within entrypoint.R?

That's a great question! I've filed an issue with the Plumber package since, IMO, you should be able to name your plumber file anything you want if you're using a valid entrypoint.R file.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.