## ----include = FALSE----------------------------------------------------------
## Use ragg for better font rendering if available
if (requireNamespace("ragg", quietly = TRUE)) {
  knitr::opts_chunk$set(
    dev = "ragg_png",
    fig.retina = 1,
    collapse = TRUE,
    comment = "#>",
    message = FALSE,
    warning = FALSE,
    out.width = "100%",
    dpi = 150
  )
} else {
  knitr::opts_chunk$set(
    collapse = TRUE,
    comment = "#>",
    message = FALSE,
    warning = FALSE,
    out.width = "100%",
    dpi = 150
  )
}

## Dynamic figure sizing (see enrollment_diagrams vignette for details)
.flow_dims <- new.env(parent = emptyenv())
.flow_dims$width <- NULL
.flow_dims$height <- NULL

knitr::opts_hooks$set(use_rec_dims = function(options) {
  if (isTRUE(options$use_rec_dims)) {
    if (!is.null(.flow_dims$width))  options$fig.width  <- .flow_dims$width
    if (!is.null(.flow_dims$height)) options$fig.height <- .flow_dims$height
    .flow_dims$width <- NULL
    .flow_dims$height <- NULL
  }
  options
})

queue_flow <- function(flow, ...) {
  ## Measure on the same device family that renders the figures (ragg, set
  ## via dev = "ragg_png" above) so that non-default fonts---whose metrics
  ## differ between devices---are sized consistently and the canvas is not
  ## cropped. Falls back to recdims()'s default pdf measurement otherwise.
  md <- if (requireNamespace("ragg", quietly = TRUE)) {
    function() {
      tf <- tempfile(fileext = ".png")
      ragg::agg_png(tf, width = 10, height = 10, units = "in", res = 150)
      tf
    }
  } else NULL
  dims <- selecta::recdims(flow, ..., .measure_dev = md)
  .flow_dims$width  <- dims["width"]
  .flow_dims$height <- dims["height"]
  invisible(flow)
}

## ----eval = FALSE-------------------------------------------------------------
# sources(...) |>
#   phase("Identification") |>
#   combine(label) |>
#   exclude(label, n, reasons) |>
#   phase("Screening") |>
#   exclude(label, n) |>
#   phase("Included") |>
#   endpoint(label) |>
#   flowchart()

## ----eval = FALSE-------------------------------------------------------------
# flowsave(flow, "consort.pdf")
# flowsave(flow, "consort.png", dpi = 300)

## ----setup--------------------------------------------------------------------
library(selecta)

## -----------------------------------------------------------------------------
example1 <- sources(
    previous  = c("Previous review" = 12, "Previous reports" = 15),
    databases = c("PubMed" = 1234, "Embase" = 567, "CENTRAL" = 89),
    other     = c("Citation search" = 55, "Websites" = 34),
    headers   = c(previous  = "Previous studies",
                  databases = "Databases and registers",
                  other     = "Other methods")
) |>
    phase("Identification") |>
    combine("Records after deduplication") |>
    exclude("Duplicates removed", n = 340,
            included_label = "Records screened") |>
    phase("Screening") |>
    exclude("Records excluded", n = 800,
            reasons = c("Irrelevant title/abstract" = 600,
                        "No full text available" = 200),
            included_label = "Reports assessed") |>
    exclude("Reports excluded", n = 190,
            reasons = c("Wrong population" = 80,
                        "Wrong intervention" = 60,
                        "Wrong outcome" = 30,
                        "Insufficient data" = 20)) |>
    phase("Analysis") |>
    endpoint("Studies included in review")

## ----echo = FALSE-------------------------------------------------------------
queue_flow(example1)

## ----use_rec_dims = TRUE, echo = TRUE-----------------------------------------
flowchart(example1)

## ----echo = FALSE-------------------------------------------------------------
queue_flow(example1, count_first = TRUE)

## ----use_rec_dims = TRUE, echo = TRUE-----------------------------------------
flowchart(example1, count_first = TRUE)

## -----------------------------------------------------------------------------
example3 <- sources(
    databases = c("PubMed" = 1234, "Embase" = 567, "CENTRAL" = 89),
    other     = c("Citation search" = 55, "Gray literature" = 34),
    headers   = c(databases = "Databases and registers",
                  other     = "Other methods")
) |>
    phase("Identification") |>
    combine("Records after deduplication") |>
    exclude("Duplicates removed", n = 340,
            included_label = "Records screened") |>
    phase("Screen") |>
    exclude("Records excluded", n = 900) |>
    phase("Analysis") |>
    endpoint("Studies included in review")

## ----echo = FALSE-------------------------------------------------------------
queue_flow(example3)

## ----use_rec_dims = TRUE, echo = TRUE-----------------------------------------
flowchart(example3)

## -----------------------------------------------------------------------------
example4 <- sources(PubMed = 1234, Embase = 567, CENTRAL = 89) |>
    phase("Identification") |>
    combine("Records identified") |>
    exclude("Duplicates removed", n = 340,
            included_label = "Records screened") |>
    phase("Screen") |>
    exclude("Records excluded", n = 800) |>
    phase("Analysis") |>
    endpoint("Studies included")

## ----echo = FALSE-------------------------------------------------------------
queue_flow(example4)

## ----use_rec_dims = TRUE, echo = TRUE-----------------------------------------
flowchart(example4)

## -----------------------------------------------------------------------------
example5 <- sources(
    databases = c("PubMed" = 892, "Embase" = 445, "Scopus" = 312),
    gray      = c("Conference abstracts" = 67, "Dissertations" = 23),
    headers   = c(databases = "Electronic databases",
                  gray      = "Gray literature")
) |>
    phase("Identification") |>
    combine("Records after deduplication") |>
    exclude("Duplicates removed", n = 420,
            included_label = "Records screened") |>
    phase("Screen") |>
    exclude("Records excluded", n = 850,
            reasons = c("Not observational design" = 380,
                        "Irrelevant exposure" = 290,
                        "Pediatric population" = 180),
            included_label = "Full-text articles assessed") |>
    exclude("Full-text articles excluded", n = 195,
            reasons = c("No relevant outcome" = 85,
                        "Insufficient follow-up" = 60,
                        "High risk of bias" = 50)) |>
    phase("Analysis") |>
    endpoint("Studies included in meta-analysis")

## ----echo = FALSE-------------------------------------------------------------
queue_flow(example5)

## ----use_rec_dims = TRUE, echo = TRUE-----------------------------------------
flowchart(example5)

## ----eval = FALSE-------------------------------------------------------------
# flowsave(example1, "prisma_3col.pdf")
# flowsave(example1, "prisma_3col.png", dpi = 300)

## ----eval = FALSE-------------------------------------------------------------
# flowsave(example1, "prisma_3col.pdf", width = 10, height = 12)

## ----eval = FALSE-------------------------------------------------------------
# flowsave(example1, "prisma_poster.pdf",
#          cex = 1.1, cex_side = 0.85, cex_phase = 1.1)

