Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move plumber to suggests #140

Merged
merged 13 commits into from
Oct 27, 2022
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Imports:
lifecycle,
magrittr (>= 2.0.3),
pins (>= 1.0.0),
plumber (>= 1.0.0),
purrr,
rapidoc,
readr (>= 1.4.0),
Expand Down Expand Up @@ -59,6 +58,7 @@ Suggests:
parsnip,
pingr,
plotly,
plumber (>= 1.0.0),
ranger,
recipes,
rmarkdown,
Expand Down
28 changes: 16 additions & 12 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#'
#' @return A Plumber router with the prediction endpoint added.
#'
#' @examples
#' @examplesIf rlang::is_installed("plumber")
#'
#' cars_lm <- lm(mpg ~ ., data = mtcars)
#' v <- vetiver_model(cars_lm, "cars_linear")
Expand Down Expand Up @@ -75,6 +75,7 @@ vetiver_pr_post <- function(pr,
debug = is_interactive(),
...,
check_ptype = TRUE) {
rlang::check_installed("plumber")
# `force()` all `...` arguments early; https://github.com/tidymodels/vetiver/pull/20
rlang::list2(...)
handler_startup(vetiver_model)
Expand Down Expand Up @@ -108,6 +109,7 @@ vetiver_pr_docs <- function(pr,
vetiver_model,
path = "/predict",
all_docs = TRUE) {
rlang::check_installed("plumber")
loadNamespace("rapidoc")
modify_spec <- function(spec) api_spec(spec, vetiver_model, path, all_docs)
pr <- plumber::pr_set_api_spec(pr, api = modify_spec)
Expand Down Expand Up @@ -141,21 +143,23 @@ vetiver_pr_predict <- function(pr,
path = "/predict",
debug = is_interactive(),
...) {

lifecycle::deprecate_warn(
lifecycle::deprecate_stop(
"0.1.2",
"vetiver_pr_predict()",
"vetiver_api()"
)
}

# `force()` all `...` arguments early; https://github.com/tidymodels/vetiver/pull/20
rlang::list2(...)
vetiver_api(
pr = pr,
vetiver_model = vetiver_model,
path = path,
debug = debug,
...,
docs = TRUE

local_plumber_session <- function(pr, port, docs = FALSE, env = parent.frame()) {
rlang::check_installed("plumber")
rs <- callr::r_session$new()
rs$call(
function(pr, port, docs) {
plumber::pr_run(pr = pr, port = port, docs = docs)
},
args = list(pr = pr, port = port, docs = docs)
)
withr::defer(rs$close(), envir = env)
rs
}
2 changes: 1 addition & 1 deletion R/open-api-spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ map_request_body.array <- function(ptype) {
#' character string.
#' @export
#'
#' @examples
#' @examplesIf rlang::is_installed("plumber")
#' library(plumber)
#' cars_lm <- lm(mpg ~ ., data = mtcars)
#' v <- vetiver_model(cars_lm, "cars_linear")
Expand Down
3 changes: 2 additions & 1 deletion R/write-plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'
#' @export
#'
#' @examples
#' @examplesIf rlang::is_installed("plumber")
#' library(pins)
#' tmp <- tempfile()
#' b <- board_temp(versioned = TRUE)
Expand All @@ -38,6 +38,7 @@ vetiver_write_plumber <- function(board, name, version = NULL,
file = "plumber.R",
rsconnect = TRUE) {

rlang::check_installed("plumber")
plumber_dots <- rlang::list2(...)

if (board$versioned) {
Expand Down
3 changes: 2 additions & 1 deletion man/api_spec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/vetiver_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/vetiver_write_plumber.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions tests/testthat/_snaps/api.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# old function is deprecated

Code
p <- pr() %>% vetiver_pr_predict(v)
Condition
Warning:
`vetiver_pr_predict()` was deprecated in vetiver 0.1.2.
i Please use `vetiver_api()` instead.
`vetiver_pr_predict()` was deprecated in vetiver 0.1.2 and is now defunct.
i Please use `vetiver_api()` instead.

24 changes: 0 additions & 24 deletions tests/testthat/helper-plumber.R

This file was deleted.

11 changes: 11 additions & 0 deletions tests/testthat/helper-redact.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
library(pins)

cars_lm <- lm(mpg ~ cyl + disp, data = mtcars)
v <- vetiver_model(cars_lm, "cars1")

root_path <- "http://localhost"
tmp_dir <- fs::path_real(withr::local_tempdir())
rel_dir <- fs::path_rel(tmp_dir)
port <- ifelse(rlang::is_installed("httpuv"), httpuv::randomPort(), 8088)

redact_vetiver <- function(snapshot) {
snapshot <- gsub(rel_dir, "<redacted>", snapshot, fixed = TRUE)
snapshot <- gsub(tmp_dir, "<redacted>", snapshot, fixed = TRUE)
snapshot <- gsub(getRversion(), "<r_version>", snapshot, fixed = TRUE)
}

redact_port <- function(snapshot) {
snapshot <- gsub(port, "<port>", snapshot, fixed = TRUE)
}
5 changes: 4 additions & 1 deletion tests/testthat/test-api.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
skip_if_not_installed("plumber")
library(plumber)

test_that("default endpoint", {
p <- pr() %>% vetiver_api(v)
expect_equal(names(p$routes), c("logo", "ping", "predict"))
Expand All @@ -6,7 +9,7 @@ test_that("default endpoint", {
})

test_that("old function is deprecated", {
expect_snapshot(p <- pr() %>% vetiver_pr_predict(v))
expect_snapshot_error(p <- pr() %>% vetiver_pr_predict(v))
})

test_that("default endpoint via modular functions", {
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-caret.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
skip_if_not_installed("caret")
skip_if_not_installed("ranger")
skip_if_not_installed("plumber")

library(caret)
library(plumber)

predictors <- mtcars[, c("cyl", "disp", "hp")]

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-create-ptype.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library(pins)

b <- board_temp()
cars_lm <- lm(mpg ~ cyl + disp, data = mtcars)

Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-gam.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
skip_if_not_installed("mgcv")
skip_if_not_installed("plumber")

library(plumber)
mtcars_gam <- mgcv::gam(mpg ~ s(disp, k = 3) + s(wt), data = mtcars)
v <- vetiver_model(mtcars_gam, "cars_gam")

Expand Down Expand Up @@ -53,7 +55,7 @@ test_that("default OpenAPI spec", {

})

test_that("create plumber.R for xgboost", {
test_that("create plumber.R for gam", {
skip_on_cran()
b <- board_folder(path = tmp_dir)
vetiver_pin_write(b, v)
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-glm.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
skip_if_not_installed("plumber")
library(plumber)

mtcars_glm <- glm(mpg ~ ., data = mtcars)
v <- vetiver_model(mtcars_glm, "cars_glm")

Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-predict.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
skip_on_cran()
skip_if_not_installed(c("pingr", "httr"))
skip_if_not_installed(c("pingr", "httr", "httpuv", "plumber"))

library(plumber)

pr <- pr() %>% vetiver_api(v, debug = TRUE)
rs <- local_plumber_session(pr, port)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-ranger.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
skip_if_not_installed("ranger")
skip_if_not_installed("plumber")

library(plumber)
set.seed(123)
cars_rf <- ranger::ranger(mpg ~ ., data = mtcars, quantreg = TRUE)
v <- vetiver_model(cars_rf, "cars3", ptype_data = mtcars[,-1])
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-stacks.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
skip_if_not_installed("stacks")
skip_on_cran()
skip_if_not_installed("stacks")
skip_if_not_installed("plumber")

library(plumber)
library(stacks)

data("tree_frogs", package = "stacks")
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-tidymodels.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
skip_if_not_installed("workflows")
skip_if_not_installed("parsnip")
skip_if_not_installed("ranger")
skip_if_not_installed("plumber")

library(plumber)
library(workflows)
library(parsnip)

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-write-docker.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
skip_if_not_installed("plumber")
b <- board_folder(path = tmp_dir)

test_that("create Dockerfile with packages", {
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-write-plumber.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
skip_if_not_installed("plumber")

test_that("create plumber.R with packages", {
skip_on_cran()
b <- board_folder(path = tmp_dir)
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-xgboost.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
skip_if_not_installed("xgboost")
skip_if_not_installed("plumber")

library(plumber)
set.seed(123)
cars_xgb <- xgboost::xgboost(as.matrix(mtcars[,-1]),
mtcars$mpg, nrounds = 3,
Expand Down