diff --git a/NEWS.md b/NEWS.md index fed423fe24..cc6373aeb8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,8 @@ * Closed #2337: Creating a new `event_data()` handler no longer causes a spurious reactive update of existing `event_data()`s. (#2339) +* Closed #2385: `"parcoords"`, `"parcats"` and `"splom"` traces now work with one-dimensional data by accounting for the "data_array" property of the "values" attribute of "dimensions". + # 4.10.4 ## Improvements diff --git a/R/utils.R b/R/utils.R index da79d43cf2..3705fc1eb6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -551,7 +551,15 @@ verify_attr <- function(proposed, schema, layoutAttr = FALSE) { # do the same for "sub-attributes" if (identical(role, "object") && is.recursive(proposed[[attr]])) { - proposed[[attr]] <- verify_attr(proposed[[attr]], attrSchema, layoutAttr = layoutAttr) + # The "dimensions" attribute requires a special treatment as + # it is an unnamed list and hence will be skipped by `for (attr in names(proposed)) + if (attr == "dimensions") { + proposed[[attr]] <- lapply(proposed[[attr]], \(x) { + verify_attr(x, attrSchema$items$dimension, layoutAttr = layoutAttr) + }) + } else { + proposed[[attr]] <- verify_attr(proposed[[attr]], attrSchema, layoutAttr = layoutAttr) + } } } diff --git a/tests/testthat/test-plotly-parcats.R b/tests/testthat/test-plotly-parcats.R new file mode 100644 index 0000000000..7477bbfc09 --- /dev/null +++ b/tests/testthat/test-plotly-parcats.R @@ -0,0 +1,20 @@ +expect_traces <- function(p, n.traces, name) { + stopifnot(is.numeric(n.traces)) + L <- expect_doppelganger_built(p, paste0("plotly-", name)) + expect_equivalent(length(L$data), n.traces) + L +} + +test_that("values property has a class of AsIs", { + p <- plot_ly( + dimensions = list( + list(values = "A"), + list(values = "B") + ), + type = "parcats" + ) + l <- expect_traces(p, 1, "parcats-data-array") + tr <- l$data[[1]]$dimensions + expect_true(inherits(tr[[1]]$values, "AsIs")) + expect_true(inherits(tr[[2]]$values, "AsIs")) +}) diff --git a/tests/testthat/test-plotly-parcoords.R b/tests/testthat/test-plotly-parcoords.R new file mode 100644 index 0000000000..180b466903 --- /dev/null +++ b/tests/testthat/test-plotly-parcoords.R @@ -0,0 +1,20 @@ +expect_traces <- function(p, n.traces, name) { + stopifnot(is.numeric(n.traces)) + L <- expect_doppelganger_built(p, paste0("plotly-", name)) + expect_equivalent(length(L$data), n.traces) + L +} + +test_that("values property has a class of AsIs", { + p <- plot_ly( + dimensions = list( + list(label = "A", values = 3), + list(label = "B", values = 8) + ), + type = "parcoords" + ) + l <- expect_traces(p, 1, "parcoords-data-array") + tr <- l$data[[1]]$dimensions + expect_true(inherits(tr[[1]]$values, "AsIs")) + expect_true(inherits(tr[[2]]$values, "AsIs")) +}) diff --git a/tests/testthat/test-plotly-splom.R b/tests/testthat/test-plotly-splom.R index 5580bff1ee..ef13df0aaa 100644 --- a/tests/testthat/test-plotly-splom.R +++ b/tests/testthat/test-plotly-splom.R @@ -1,5 +1,9 @@ - - +expect_traces <- function(p, n.traces, name) { + stopifnot(is.numeric(n.traces)) + L <- expect_doppelganger_built(p, paste0("plotly-", name)) + expect_equivalent(length(L$data), n.traces) + L +} test_that("No cartesian axes are supplied to a splom chart", { @@ -12,3 +16,17 @@ test_that("No cartesian axes are supplied to a splom chart", { ) }) + +test_that("values property has a class of AsIs", { + p <- plot_ly( + type = "splom", + dimensions = list( + list(values = 1, label = "A"), + list(values = 2, label = "B") + ) + ) + l <- expect_traces(p, 1, "parcats-data-array") + tr <- l$data[[1]]$dimensions + expect_true(inherits(tr[[1]]$values, "AsIs")) + expect_true(inherits(tr[[2]]$values, "AsIs")) +})