8000 Fix. Account for the "data_array" property of the "values" attribute of the "dimensions" attribute. Closes #2385 by trekonom · Pull Request #2387 · plotly/plotly.R · GitHub
[go: up one dir, main page]

Skip to content

Fix. Account for the "data_array" property of the "values" attribute of the "dimensions" attribute. Closes #2385 #2387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
8000
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix. Add special treatment of dimensions attrbute to account for th…
…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
commit c40657a0884452836ec0c3fd556f7ac956ed4e82
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") {
Copy link
Collaborator
@cpsievert cpsievert Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems there is actually a more general problem here that's beyond just dimensions. Some attributes say role: object, but are actually expecting a list of objects. Here is the schema for both dimensions and transforms:

Screenshot 2024-09-03 at 5 09 49 PM Screenshot 2024-09-03 at 5 10 03 PM

So, I think this code should be closer to:

if (identical(role, "object") && is.recursive(proposed[[attr]])) {
      # Some attributes (e.g., dimensions, transforms) are actually 
      # a list of objects (even though they, confusingly, have role: object)
      # In those cases, we actually want to verify each list element
      attr_ <- sub("s$", "", attr)
      is_list_attr <- ("items" %in% names(attrSchema)) && 
        (attr_ %in% names(attrSchema$items))
      if (is_list_attr) {
        proposed[[attr]] <- lapply(proposed[[attr]], function(x) {
          verify_attr(x, attrSchema$items[[attr_]])
        })
      } else {
        proposed[[attr]] <- verify_attr(proposed[[attr]], attrSchema, layoutAttr = layoutAttr)
      }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the review and the suggestions. Just added and comitted.

proposed[[attr]] <- lapply(proposed[[attr]], \(x) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\(x) was added fairly recently to R, so please use function(x) instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙈 Thanks for the reminder. Should have thought of that myself.

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"))
})
Loading
0