rstudio/vetiver-r

Allow Custom Models to Assign Packages

Closed this issue · 2 comments

Background

This issue is in reference to the discussion had on #214.

Our team has a custom package responsible for making a model, and we want to set up and deploy a vetiver API endpoint from that same package. When working with the custom model wrapped by vetiver_model(), we had difficulty getting our deployed API on Posit Connect to correctly find the new methods we had made (i.e. handler_predict.my_model, etc.) and would get the error:

There is no method available to build a prediction handler for `x`.

Current Output

Indicating those methods weren't being carried over. The default output file for vetiver::vetiver_write_plumber() looks something like:

# Generated by the vetiver package; edit with care

library(pins)
library(plumber)
library(rapidoc)
library(vetiver)

# Packages needed to generate model predictions
if (FALSE) {
    library(myCustomPackage)
    library(otherusefulpackages)
    ...
}
b <- board_connect(auth = "envvar")
v <- vetiver_pin_read(b, "my_model")

#* @plumber
function(pr) {
    pr %>% vetiver_api(v)
}

What we found worked was moving the library() call for the custom package out of if (FALSE) { }. Then the deployed API correctly picked up the custom methods for our model.

Proposed Solution

It might be helpful to allow users to edit what packages are attached to the default plumber output, currently they are rigidly set in infra_pkgs:

infra_pkgs <- c("pins", "plumber", "rapidoc", "vetiver")

Thanks @rsh52! 🙌

This is merged now and available in the development version of vetiver: pak::pak("rstudio/vetiver-r").