## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  # Evaluated wherever INLA and the serving stack are installed.
  eval     = all(vapply(
    c("INLA", "fmesher", "sf", "vetiver", "pins", "plumber"),
    requireNamespace, logical(1), quietly = TRUE
  ))
)


## ----message = FALSE----------------------------------------------------------
library(inlabru)
library(INLA)
library(sf)
library(vetiver)
library(pins)
library(plumber)
library(inlaops)


## -----------------------------------------------------------------------------
set.seed(42)
n_pts <- 80
pts   <- sf::st_as_sf(
  data.frame(
    x = runif(n_pts),
    y = runif(n_pts)
  ),
  coords = c("x", "y"),
  crs    = NA
)

domain <- sf::st_polygon(list(rbind(
  c(0, 0), c(1, 0), c(1, 1), c(0, 1), c(0, 0)
)))
mesh <- fmesher::fm_mesh_2d_inla(
  loc.domain = sf::st_coordinates(domain)[, 1:2],
  max.edge   = c(0.1, 0.3)
)
spde <- inla.spde2.pcmatern(mesh,
                            prior.range = c(0.3, 0.5),
                            prior.sigma = c(1.0, 0.01))

# inlabru >= 2.12 uses bru_obs() (like() is deprecated). For LGCP, the
# integration domain goes through the `domain` argument as a named list
# matching the predictor variable in the formula RHS — here `geometry`.
fit <- bru(
  ~ field(geometry, model = spde) + Intercept(1),
  bru_obs(
    formula = geometry ~ .,
    family  = "cp",
    data    = pts,
    domain  = list(geometry = mesh)
  ),
  options = bru_options(control.compute = list(config = TRUE))
)


## -----------------------------------------------------------------------------
v <- vetiver_model(
  fit, "lgcp_example",
  metadata = list(
    pred_formula = "~ exp(Intercept + field)",   # intensity, not counts
    n_samples    = 500L,
    coord_names  = c("X", "Y"),
    crs          = NA
  )
)

board <- board_temp()
vetiver_pin_write(board, v)


## ----eval = FALSE-------------------------------------------------------------
# pr() |> vetiver_api(v) |> pr_run(port = 8088)


## ----eval = FALSE-------------------------------------------------------------
# library(httr2)
# 
# query <- data.frame(
#   X = c(0.2, 0.5, 0.8),
#   Y = c(0.5, 0.5, 0.5)
# )
# 
# resp <- request("http://127.0.0.1:8088/predict") |>
#   req_body_raw(
#     jsonlite::toJSON(query, dataframe = "rows"),
#     type = "application/json"
#   ) |>
#   req_perform()
# 
# tibble::as_tibble(resp_body_json(resp, simplifyVector = TRUE))
# #> # A tibble: 3 × 4
# #>   .pred .pred_low .pred_high .pred_sd
# #>   <dbl>     <dbl>      <dbl>    <dbl>
# #> 1  48.9      18.3       101.     21.5
# #> 2  80.0      32.8       169.     35.8
# #> 3  89.5      41.1       174.     33.4


## ----eval = FALSE-------------------------------------------------------------
# board <- board_folder("model-store")
# pin_write(board, fit, name = "lgcp_custom",
#           metadata = list(
#             description  = "LGCP intensity model on the unit square",
#             pred_formula = "~ exp(Intercept + field)",
#             n_samples    = 500L,
#             crs          = NA
#           ))

