## ----include = FALSE----------------------------------------------------------
has_bus <- exists("rxEventListen", asNamespace("rxode2"))
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5,
  eval = has_bus
)

## ----echo = FALSE, eval = !has_bus, results = "asis"--------------------------
# cat("> **Note:** the installed `rxode2` has no event bus",
#     "(`rxode2::rxEventListen()` does not exist), so the code in this",
#     "vignette is shown but not run.  `nlmixr2plot` then emits nothing and",
#     "behaves exactly as before.\n")

## ----results = "hide"---------------------------------------------------------
library(nlmixr2plot)
library(nlmixr2est)

one.cmt <- function() {
  ini({
    tka <- log(1.57); tcl <- log(2.72); tv <- log(31.5)
    eta.ka ~ 0.6; eta.cl ~ 0.3; eta.v ~ 0.1
    add.sd <- 0.7
  })
  model({
    ka <- exp(tka + eta.ka)
    cl <- exp(tcl + eta.cl)
    v <- exp(tv + eta.v)
    linCmt() ~ add(add.sd)
  })
}

fit <- suppressMessages(
  nlmixr2(one.cmt, nlmixr2data::theo_sd, est = "posthoc",
          control = list(print = 0))
)

## -----------------------------------------------------------------------------
seen <- list()
rxode2::rxEventListen("demo", function(event, ...) {
  p <- list(...)
  seen[[length(seen) + 1L]] <<- data.frame(
    event = event,
    kind = if (is.null(p$kind)) NA_character_ else p$kind,
    hasFit = !is.null(p$fit),
    resultClass = class(p$result)[1],
    call = paste(deparse(p$call), collapse = " ")
  )
})

p <- plot(fit)
v <- suppressMessages(suppressWarnings(vpcPlot(fit, n = 50)))
ap <- suppressMessages(augPred(fit))
a <- plot(ap)

rxode2::rxEventUnlisten("demo")
do.call(rbind, seen)

## -----------------------------------------------------------------------------
n <- length(seen)
p <- plot(fit)
length(seen) == n

