Skip to content

Commit

Permalink
Fix. Add special treatment of dimensions attrbute to account for th…
Browse files Browse the repository at this point in the history
…e data_array propery of the "values" attribute. Closes #2385.

* Add tests to check that "values" property has class "AsIs" for "parcoords", "parcats" and "splom" traces.

* Add NEWS entry
  • Loading branch information
trekonom committed Aug 31, 2024
1 parent 3cf17c0 commit c40657a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-plotly-parcats.R
Original file line number Diff line number Diff line change
@@ -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"))
})
20 changes: 20 additions & 0 deletions tests/testthat/test-plotly-parcoords.R
Original file line number Diff line number Diff line change
@@ -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"))
})
22 changes: 20 additions & 2 deletions tests/testthat/test-plotly-splom.R
Original file line number Diff line number Diff line change
@@ -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", {

Expand All @@ -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"))
})

0 comments on commit c40657a

Please sign in to comment.