Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: forestly
Title: Interactive Forest Plot
Version: 0.1.8
Version: 0.1.9
Authors@R: c(
person("Yilong", "Zhang", role = c("aut")),
person("Benjamin", "Wang", role = c("aut")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Reveal a truncated table cell's full text on hover; figure cells, the drill-down detail, and the control-bar and per-column filter rows (whose chips, funnels, and popovers must stay visible) are exempt.
- Speed up `format_ae_listing()` by vectorizing the per-row loops that map `AEACN`, `AEOUT`, and missing `ADURN` durations to display labels, avoiding quadratic-time column copies on large AE listings.
- Rename the misspelled `ae_forestly()` argument `dowload_button` to `download_button` (#173).
- Deprecate the `display_diff_toggle` argument of `ae_forestly()`: the numeric risk-difference columns are always offered in the "Show/Hide columns" menu. With `display_soc_toggle = FALSE`, the SOC column is now left out of the table entirely, and the "Show/Hide columns" menu is always shown.
- Add a treatment-group picker to the drill-down: a checklist in the table's control bar chooses which arm(s) appear in each expanded AE listing, filtering the detail client-side (built on `lt`'s `LT.ui` popover/checklist widgets and `el._lt.resetDetail`).

# forestly 0.1.6
Expand Down
24 changes: 17 additions & 7 deletions R/ae_forestly.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
#' Display interactive forest plot
#'
#' @param outdata An `outdata` object created by [format_ae_forestly()].
#' @param display_soc_toggle A boolean value to display SOC toggle button.
#' @param display_diff_toggle A boolean value to display risk difference toggle button.
#' @param display_soc_toggle Whether to include the SOC column in the table. If
#' `TRUE`, it is offered in the "Show/Hide columns" menu (hidden by default); if
#' `FALSE`, it is left out of the table entirely. Including it makes the saved
#' HTML file slightly larger, which only matters for very large data sets.
#' @param display_diff_toggle Deprecated and ignored. The numeric risk-difference
#' columns are always in the table and, unless `"diff"` is in the `display`
#' argument of [format_ae_forestly()], offered in the "Show/Hide columns" menu
#' (hidden by default).
#' @param filter The variable the incidence slider (the "Incidence (%) in One or
#' More Treatment Groups" chip in the control bar) filters on: `"prop"` for
#' incidence (%) or `"n"` for the number of AEs. Use `NULL` to hide the slider.
Expand Down Expand Up @@ -151,14 +157,19 @@
#' }
ae_forestly <- function(outdata,
display_soc_toggle = TRUE,
display_diff_toggle = FALSE,
display_diff_toggle = NULL,
filter = c("prop", "n"),
filter_label = NULL,
filter_range = NULL,
ae_label = NULL,
max_page = NULL,
download_button = FALSE,
width = NULL) {
if (!is.null(display_diff_toggle)) warning(
"`display_diff_toggle` is deprecated and will be removed in future; ",
"the risk-difference columns are always offered in the Show/Hide columns menu."
)

# Set filter parameter
if (!is.null(filter)) {
display_filter = TRUE
Expand Down Expand Up @@ -443,8 +454,7 @@ ae_forestly <- function(outdata,
# ---- Build the interactive lt table ----
built <- format_lt_forestly(
outdata,
display_soc_toggle = display_soc_toggle,
display_diff_toggle = display_diff_toggle
display_soc_toggle = display_soc_toggle
)
# forestly-ae on lt's container scopes cell styles and sizes the table (see
# inst/css/forestly-widgets.css); per-column widths from format_lt_forestly() kept.
Expand All @@ -469,9 +479,9 @@ ae_forestly <- function(outdata,
parameter = list(
type = "select", choices = param_levels, selected = param_levels[1],
label = ae_label, box = FALSE # single-choice only; no custom AE-criteria set
),
soc_name = "select"
)
)
if (display_soc_toggle) filter_cfg$soc_name <- "select"
for (nm in built$range_cols) filter_cfg[[nm]] <- "range"
if (display_filter) {
slider_col <- if (filter == "prop") "hide_prop" else "hide_n"
Expand Down
123 changes: 66 additions & 57 deletions R/lt_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,17 @@
#' tested in isolation.
#'
#' @param outdata An `outdata` object from [format_ae_forestly()].
#' @param display_soc_toggle Offer the SOC column via the column show/hide menu
#' (starts hidden) instead of dropping it outright.
#' @param display_diff_toggle Offer the numeric risk-difference columns via the
#' column show/hide menu.
#' @param display_soc_toggle Include the SOC column in the table, offered via the
#' column show/hide menu (starts hidden). If `FALSE`, the column is left out of
#' the table data altogether.
#'
#' @return A list with `x` (an `lt_tbl`) and `hide_menu` (the argument to pass to
#' `lt_interactive(hide=)`: `NULL`, `TRUE`, or a character vector of columns to
#' start hidden).
#' `lt_interactive(hide=)`: `TRUE`, or a character vector of columns to start
#' hidden).
#'
#' @noRd
format_lt_forestly <- function(outdata,
display_soc_toggle = TRUE,
display_diff_toggle = FALSE) {
display_soc_toggle = TRUE) {
tbl <- outdata$tbl
digits <- outdata$digits %||% 1
m <- outdata$m_group # arms shown as n/(%) columns (incl. Total if displayed)
Expand All @@ -58,24 +56,38 @@ format_lt_forestly <- function(outdata,
lo_name <- outdata$ci_lower_name
up_name <- outdata$ci_upper_name
nd <- length(diff_name)
diff_shown <- "diff" %in% outdata$display
display <- outdata$display
diff_shown <- "diff" %in% display

# Each figure draws into its own dedicated column via `lt_*(into=)`: lt ships
# only the column name (an `add_col` op) and the runtime materializes the
# column, so no duplicated value array travels in the JSON payload. Every
# column, so no duplicated value array is saved in the HTML. Every
# proportion / diff stays visible as text (`hide = FALSE` below) and is read by
# reference, and the CI bounds come from the hidden `lo_name`/`up_name`.
prop_cols <- paste0("prop_", seq_len(ng))
pfig <- "prop_fig" # proportion dot-plot target
dfig <- if (nd > 0) "diff_fig" else NULL # risk-difference error-bar target
# A figure is built only when listed in `display`; the CI bounds are carried
# only for the error bar.
pfig <- if ("fig_prop" %in% display) "prop_fig" # dot-plot target
dfig <- if (nd > 0 && "fig_diff" %in% display) "diff_fig" # error-bar target
ci_cols <- if (!is.null(dfig)) c(lo_name, up_name)

# Column order of the real data: name, SOC, per-arm n/(%), numeric diffs,
# hidden helpers last. The two figure columns are synthesized by the plots and
# positioned after SOC by lt_move() below, so the figures lead the numeric
# columns as in the forest-plot convention.
arm_cols <- as.vector(rbind(name_n, name_prop)) # n_1, prop_1, n_2, prop_2, ...
visible <- c("name", "soc_name", arm_cols, diff_name)
hidden_tail <- c("parameter", "hide_prop", "hide_n", lo_name, up_name)
#
# `n` / `prop` shown only when listed in `display`. The per-arm proportions are
# still carried, hidden, when the dot plot reads them; otherwise they, and any
# unshown column, are left out of the data.
show_n <- if ("n" %in% display) name_n
show_prop <- if ("prop" %in% display) name_prop
prop_hidden <- if (!is.null(pfig)) setdiff(prop_cols, show_prop)
arm_cols <- intersect( # n_1, prop_1, n_2, prop_2, ...
as.vector(rbind(name_n, name_prop)), c(show_n, show_prop, prop_hidden)
)
shown_arm_cols <- setdiff(arm_cols, prop_hidden)
visible <- c("name", if (display_soc_toggle) "soc_name", arm_cols, diff_name)
hidden_tail <- c("parameter", "hide_prop", "hide_n", ci_cols)
tbl <- tbl[, c(visible, hidden_tail), drop = FALSE]

# The parameter dropdown filters on the stored value, so ship a plain string
Expand All @@ -85,14 +97,17 @@ format_lt_forestly <- function(outdata,
x <- lt::lt(tbl, auto_format = FALSE, auto_label = FALSE)

# Column labels: arm n/(%), numeric diff header per comparison arm.
labels <- list(name = outdata$ae_col_header, soc_name = "SOC")
for (nm in name_n) labels[[nm]] <- "n"
for (nm in name_prop) labels[[nm]] <- "(%)"
labels <- list(name = outdata$ae_col_header)
if (display_soc_toggle) labels$soc_name <- "SOC"
for (nm in show_n) labels[[nm]] <- "n"
for (nm in show_prop) labels[[nm]] <- "(%)"
for (k in seq_len(nd)) labels[[diff_name[k]]] <- group[outdata$index_diff[k]]
x <- lt::lt_label(x, labels)

# Numeric formatting: proportions wrapped as "(x.x)", diffs to `digits`.
x <- lt::lt_format(x, name_prop, decimals = digits, prefix = "(", suffix = ")")
if (length(show_prop)) {
x <- lt::lt_format(x, show_prop, decimals = digits, prefix = "(", suffix = ")")
}
if (nd > 0) x <- lt::lt_format(x, diff_name, decimals = digits)

# Per-arm treatment-group spanners "<Group><br>(N=...)" over each n/(%) pair.
Expand All @@ -101,7 +116,8 @@ format_lt_forestly <- function(outdata,
"<span title=\"%s (N=%s)\">%s<br>(N=%s)</span>",
group[i], outdata$n_pop[i], group[i], outdata$n_pop[i]
)
x <- lt::lt_spanner(x, I(header), c(name_n[i], name_prop[i]))
arm_i <- intersect(c(name_n[i], name_prop[i]), shown_arm_cols)
if (length(arm_i)) x <- lt::lt_spanner(x, I(header), arm_i)
}
if (diff_shown && nd > 0) {
x <- lt::lt_spanner(x, I(outdata$diff_col_header), diff_name)
Expand All @@ -110,18 +126,20 @@ format_lt_forestly <- function(outdata,
# Inline proportion dot plot: one dot per arm on a shared scale, footer legend
# keyed by arm color. Dots are staggered onto separate tracks so near-equal
# per-arm proportions stay distinguishable (as in the original forest plot).
x <- lt::lt_dotplot(
x, prop_cols,
into = pfig,
color = outdata$fig_prop_color,
labels = group[seq_len(ng)],
limits = outdata$fig_prop_range,
width = w$fig,
stagger = TRUE,
axis = TRUE,
hide = FALSE # the per-arm proportions stay visible as numeric columns
)
x <- lt::lt_label(x, list(pfig, "AE Proportion (%)"))
if (!is.null(pfig)) {
x <- lt::lt_dotplot(
x, prop_cols,
into = pfig,
color = outdata$fig_prop_color,
labels = group[seq_len(ng)],
limits = outdata$fig_prop_range,
width = w$fig,
stagger = TRUE,
axis = TRUE,
hide = FALSE # the per-arm proportions stay visible as numeric columns
)
x <- lt::lt_label(x, list(pfig, "AE Proportion (%)"))
}

# Inline risk-difference error-bar: one point + 95% CI per comparison, stacked,
# shared scale, zero reference line, favor-direction axis label. Color + legend
Expand All @@ -132,7 +150,7 @@ format_lt_forestly <- function(outdata,
})
eb_color <- if (nd > 1) outdata$fig_diff_color else FALSE
eb_labels <- if (nd > 1) group[outdata$index_diff] else NULL
if (nd > 0) {
if (!is.null(dfig)) {
x <- do.call(lt::lt_errorbar, c(
list(x), eb_series,
list(
Expand All @@ -149,44 +167,35 @@ format_lt_forestly <- function(outdata,
x <- lt::lt_label(x, list(dfig, I(outdata$diff_fig_header)))
}

# Place the synthesized figure columns right after SOC (before the numeric
# columns); the plots appended them last via `into=`.
x <- lt::lt_move(x, c(pfig, dfig), after = "soc_name")
# Place the synthesized figure columns right after SOC, or the term column when
# SOC is excluded (before the numeric columns); the plots appended them last
# via `into=`.
figs <- c(pfig, dfig)
if (length(figs)) x <- lt::lt_move(x, figs, after = if (display_soc_toggle) "soc_name" else "name")

# Column widths (px). The synthesized figure columns carry the figure width.
width_cols <- c("name", "soc_name", arm_cols, pfig, diff_name, if (nd > 0) dfig)
width_cols <- c("name", if (display_soc_toggle) "soc_name", arm_cols, pfig, diff_name, dfig)
width_px <- c(
rep(w$term, 2), rep(c(w$n, w$prop), m), w$fig,
rep(w$diff, nd), if (nd > 0) w$fig
rep(w$term, 1 + display_soc_toggle), ifelse(arm_cols %in% name_n, w$n, w$prop),
rep(w$fig, length(pfig)), rep(w$diff, nd), rep(w$fig, length(dfig))
)
x <- lt::lt_width(x, list(width_cols, paste0(width_px, "px")))

# Decide show/hide handling for SOC and numeric diff columns.
fully_hidden <- c("parameter", "hide_prop", "hide_n", lo_name, up_name)
# Numeric diff columns not shown at start are offered in the show/hide menu
# (SOC is either in the menu or excluded from the data above).
fully_hidden <- c("parameter", "hide_prop", "hide_n", ci_cols, prop_hidden)
menu_hidden <- character(0)

if (display_soc_toggle) {
menu_hidden <- c(menu_hidden, "soc_name")
} else {
fully_hidden <- c(fully_hidden, "soc_name")
}
if (display_soc_toggle) menu_hidden <- "soc_name"

if (nd > 0 && !diff_shown) {
if (display_diff_toggle) {
menu_hidden <- c(menu_hidden, diff_name)
} else {
fully_hidden <- c(fully_hidden, diff_name)
}
}
if (nd > 0 && !diff_shown) menu_hidden <- c(menu_hidden, diff_name)

x <- lt::lt_hide(x, unique(fully_hidden))

hide_menu <- if (display_soc_toggle || display_diff_toggle) {
if (length(menu_hidden)) menu_hidden else TRUE
}
hide_menu <- if (length(menu_hidden)) menu_hidden else TRUE

# Numeric columns shown at start (n, (%), difference) for ae_forestly() sliders.
range_cols <- setdiff(c(name_n, name_prop, diff_name), unique(c(fully_hidden, menu_hidden)))
range_cols <- setdiff(c(shown_arm_cols, diff_name), unique(c(fully_hidden, menu_hidden)))

list(x = x, hide_menu = hide_menu, range_cols = range_cols)
}
12 changes: 9 additions & 3 deletions man/ae_forestly.Rd

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

9 changes: 6 additions & 3 deletions tests/testthat/test-ae_forestly.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("ae_forestly(): no incidence filter when filter is NULL", {
expect_false(grepl("One or More Treatment Groups", html_text, fixed = TRUE))
})

test_that("ae_forestly(): both diff-toggle settings render without error", {
test_that("ae_forestly(): deprecated display_diff_toggle warns but still renders", {
outdata <- meta_ae_test() |>
prepare_ae_forestly(
population = "apat",
Expand All @@ -52,6 +52,9 @@ test_that("ae_forestly(): both diff-toggle settings render without error", {
) |>
format_ae_forestly(display = c("n", "prop", "fig_prop", "fig_diff"))

expect_s3_class(ae_forestly(outdata, display_diff_toggle = FALSE), "shiny.tag.list")
expect_s3_class(ae_forestly(outdata, display_diff_toggle = TRUE), "shiny.tag.list")
expect_warning(
res <- ae_forestly(outdata, display_diff_toggle = TRUE),
"deprecated"
)
expect_s3_class(res, "shiny.tag.list")
})
45 changes: 34 additions & 11 deletions tests/testthat/test-lt_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,46 @@ test_that("format_lt_forestly(): returns an lt_tbl and a hide-menu spec", {
expect_true("hide_menu" %in% names(built))
})

test_that("format_lt_forestly(): SOC toggle controls the hide menu vs. hard hide", {
test_that("format_lt_forestly(): SOC toggle offers or excludes the SOC column", {
out <- helper_lt_outdata()

# No toggles -> no column-visibility menu at all.
built_none <- format_lt_forestly(out, display_soc_toggle = FALSE, display_diff_toggle = FALSE)
expect_null(built_none$hide_menu)

# SOC toggle -> SOC column offered in the menu (starts hidden).
built_soc <- format_lt_forestly(out, display_soc_toggle = TRUE, display_diff_toggle = FALSE)
expect_true("soc_name" %in% built_soc$hide_menu)
expect_true("soc_name" %in% format_lt_forestly(out, display_soc_toggle = TRUE)$hide_menu)

# Excluded: not in the menu, and nowhere in the rendered table.
expect_false("soc_name" %in% format_lt_forestly(out, display_soc_toggle = FALSE)$hide_menu)
html <- paste(as.character(ae_forestly(out, display_soc_toggle = FALSE)), collapse = "")
expect_false(grepl("soc_name", html, fixed = TRUE))
})

test_that("format_lt_forestly(): diff toggle adds the numeric diff columns to the menu", {
test_that("format_lt_forestly(): diff columns are offered in the show/hide menu", {
out <- helper_lt_outdata()
built <- format_lt_forestly(out, display_soc_toggle = TRUE, display_diff_toggle = TRUE)
built <- format_lt_forestly(out)

# diff columns are named after the comparison arms (diff_<k>).
diff_cols <- out$diff_name
expect_true(all(diff_cols %in% built$hide_menu))
expect_true(all(out$diff_name %in% built$hide_menu))
})

test_that("ae_forestly(): figures appear only when listed in `display`", {
m <- meta_ae_test() |>
prepare_ae_forestly(population = "apat", observation = "wk12", parameter = "any;rel;ser")
has_figs <- function(display) {
html <- paste(as.character(ae_forestly(format_ae_forestly(m, display = display))), collapse = "")
c(prop = grepl("AE Proportion (%)", html, fixed = TRUE),
diff = grepl("Risk Difference", html, fixed = TRUE))
}
expect_equal(has_figs(c("n", "prop", "fig_prop", "fig_diff")), c(prop = TRUE, diff = TRUE))
expect_equal(has_figs(c("n", "prop", "fig_prop")), c(prop = TRUE, diff = FALSE))
expect_equal(has_figs(c("n", "prop", "fig_diff")), c(prop = FALSE, diff = TRUE))
expect_equal(has_figs(c("n", "prop")), c(prop = FALSE, diff = FALSE))
})

test_that("format_lt_forestly(): n and (%) columns follow `display`", {
m <- meta_ae_test() |>
prepare_ae_forestly(population = "apat", observation = "wk12", parameter = "any;rel;ser")
shown <- function(display) format_lt_forestly(format_ae_forestly(m, display = display))$range_cols

expect_false(any(grepl("^(n|prop)_", shown("fig_prop"))))
expect_true(length(shown("n")) > 0 && all(grepl("^n_", shown("n"))))
expect_true(any(grepl("^prop_", shown("prop"))))
})
Loading
Loading