From 7da98b3189c6edd1431acb07954d2a5e95c591a8 Mon Sep 17 00:00:00 2001 From: Justin Yap Date: Mon, 13 Jul 2026 15:16:24 +1000 Subject: [PATCH] RS-22593: Expose Price Sensitivity Meter chart title as ChartLabels$ChartTitle The chart title is forwarded to Line via ... but was not exposed on the output, so consumers reading attr(x, "ChartLabels")$ChartTitle (e.g. the LLM-readable generation) could not label the chart's data. Merge the title into the ChartLabels attribute alongside the existing ChartData, without overwriting the SeriesLabels that Line already sets. Co-Authored-By: Claude Opus 4.8 (1M context) --- R/pricesensitivitymeter.R | 11 +++++++++++ tests/testthat/test-pricesensitivitymeter.R | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/R/pricesensitivitymeter.R b/R/pricesensitivitymeter.R index 365d8d2..6adf1de 100644 --- a/R/pricesensitivitymeter.R +++ b/R/pricesensitivitymeter.R @@ -367,6 +367,17 @@ PriceSensitivityMeter <- function(x, edits = list(annotationPosition = FALSE, annotationText = FALSE, axisTitleText = FALSE, titleText = FALSE, legendText = FALSE)) attr(pp, "ChartData") <- psm.dat + # Expose the chart title (forwarded to Line via ...) as a ChartLabels$ChartTitle + # attribute so downstream consumers can label the chart's data. Merge into any + # ChartLabels already set by Line rather than overwriting it. + chart.title <- list(...)[["title"]] + if (!is.null(chart.title) && any(nzchar(chart.title))) { + chart.labels <- attr(pp, "ChartLabels") + if (is.null(chart.labels)) + chart.labels <- list() + chart.labels$ChartTitle <- chart.title + attr(pp, "ChartLabels") <- chart.labels + } return(pp) } diff --git a/tests/testthat/test-pricesensitivitymeter.R b/tests/testthat/test-pricesensitivitymeter.R index b78cd8e..52dbf93 100644 --- a/tests/testthat/test-pricesensitivitymeter.R +++ b/tests/testthat/test-pricesensitivitymeter.R @@ -231,3 +231,15 @@ test_that("PSM with two axes", y2.title.font.family = "Courier New", y2.title.font.color = "#0000FF", y2.bounds.minimum = 0.5, y2.bounds.maximum = 5, y2.title = "R"), NA) }) + +test_that("Chart title exposed as ChartLabels$ChartTitle", +{ + psm <- PriceSensitivityMeter(dat, title = "My price sensitivity meter") + expect_equal(attr(psm, "ChartLabels")$ChartTitle, "My price sensitivity meter") + # ChartData is still attached alongside the title + expect_false(is.null(attr(psm, "ChartData"))) + + # No ChartTitle is exposed when the title is empty + psm.no.title <- PriceSensitivityMeter(dat) + expect_null(attr(psm.no.title, "ChartLabels")$ChartTitle) +})