tests/testthat/test-table-contracts.R

make_contract_data <- function() {
  data.frame(
    term = c("Age", "BMI", "Treatment"),
    estimate = c(0.30, -0.20, 0.40),
    conf.low = c(0.10, -0.40, 0.20),
    conf.high = c(0.50, 0.00, 0.60),
    sample_size = c(120, 115, 98),
    event_count = c(42, 39, 31),
    p_value = c(0.012, 0.031, 0.004)
  )
}

test_that("add_forest_table returns a two-panel patchwork", {
  p <- ggforestplot(make_contract_data(), n = "sample_size", events = "event_count", p.value = "p_value")

  out <- add_forest_table(
    p,
    position = "left",
    columns = c("term", "n", "events", "estimate", "p"),
    estimate_label = "Beta"
  )

  expect_s3_class(out, "patchwork")
  expect_s3_class(out, "ggplot")
  expect_length(out$patches$layout$widths, 2L)
  expect_length(out$patches$plots, 1L)
  expect_s3_class(out$patches$plots[[1]], "ggplot")
})

test_that("add_forest_table supports ggplot add syntax as a terminal step", {
  out <- ggforestplot(make_contract_data(), n = "sample_size", events = "event_count", p.value = "p_value") +
    ggplot2::labs(title = "Contract") +
    add_forest_table(position = "right", columns = c("term", "n", "events", "estimate", "p"))

  expect_s3_class(out, "patchwork")
  expect_s3_class(out, "ggplot")
  expect_length(out$patches$layout$widths, 2L)
  expect_length(out$patches$plots, 1L)
})

test_that("add_split_table returns left plot right panels in order", {
  p <- ggforestplot(make_contract_data(), n = "sample_size", events = "event_count", p.value = "p_value")

  out <- add_split_table(
    p,
    left_columns = c("term", "n", "events"),
    right_columns = c("estimate", "p"),
    estimate_label = "HR",
    estimate_fmt = "{estimate} [{conf.low}, {conf.high}]"
  )

  expect_s3_class(out, "patchwork")
  expect_s3_class(out, "ggplot")
  expect_length(out$patches$layout$widths, 3L)
  expect_length(out$patches$plots, 2L)
  expect_s3_class(out$patches$plots[[2]], "ggplot")
})

test_that("add_split_table ggplot add syntax preserves a forest-plot center panel", {
  out <- ggforestplot(make_contract_data(), n = "sample_size", events = "event_count", p.value = "p_value") +
    add_split_table(left_columns = c("term", "n", "events"), right_columns = c("estimate", "p"))

  center_plot <- out$patches$plots[[2]]

  expect_s3_class(out, "patchwork")
  expect_s3_class(center_plot, "ggplot")
  expect_true(!is.null(center_plot$ggforestplotR_state))
})

Try the ggforestplotR package in your browser

Any scripts or data that you put into this service are public.

ggforestplotR documentation built on June 5, 2026, 5:07 p.m.