Skip to content

Commit cdd48e2

Browse files
committed
better support for customdata list-columns
1 parent aac30a4 commit cdd48e2

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

R/plotly_build.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
190190
tr <- trace[names(trace) %in% allAttrs]
191191
# TODO: does it make sense to "train" matrices/2D-tables (e.g. z)?
192192
tr <- tr[vapply(tr, function(x) is.null(dim(x)) && is.atomic(x), logical(1))]
193+
# white-list customdata as this can be a non-atomic vector
194+
tr$customdata <- trace$customdata
193195
builtData <- tibble::as_tibble(tr)
194196
# avoid clobbering I() (i.e., variables that shouldn't be scaled)
195197
for (i in seq_along(tr)) {
@@ -266,7 +268,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
266268

267269
# insert NAs to differentiate groups
268270
traces <- lapply(traces, function(x) {
269-
d <- data.frame(x[names(x) %in% x$.plotlyVariableMapping], stringsAsFactors = FALSE)
271+
d <- tibble::as_tibble(x[names(x) %in% x$.plotlyVariableMapping])
270272
d <- group2NA(
271273
d, if (has_group(x)) ".plotlyGroupIndex",
272274
ordered = if (inherits(x, "plotly_line")) "x",

tests/testthat/test-plotly-customdata.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,25 @@ test_that("ggplotly relays customdata", {
88
trace <- l$x$data[[1]]
99
expect_equivalent(trace$customdata, nms)
1010
})
11+
12+
13+
test_that("Can provide list-columns to customdata", {
14+
l <- txhousing %>%
15+
group_by(city) %>%
16+
highlight_key(~city) %>%
17+
plot_ly(x = ~date, y = ~median, hoverinfo = "name") %>%
18+
add_lines(customdata = ~map2(date, median, ~list(.x, .y))) %>%
19+
plotly_build()
20+
21+
trace <- l$x$data[[1]]
22+
expect_true(length(trace$customdata) == length(trace$x))
23+
24+
# make sure customdata have been arranged properly
25+
customx <- unlist(lapply(trace$customdata, function(x) x[1] %||% NA))
26+
expect_equivalent(customx, trace$x)
27+
28+
# check there is no customdata where x values are null
29+
nullcd <- trace$customdata[which(is.na(trace$x))]
30+
expect_true(unique(lengths(nullcd)) == 0)
31+
})
32+

0 commit comments

Comments
 (0)