File: test-plotly-customdata.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (32 lines) | stat: -rw-r--r-- 1,011 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


# TODO: use shinytest to make sure we can access the right value in shiny
test_that("ggplotly relays customdata", {
  nms <- row.names(mtcars)
  p <- ggplot(mtcars, aes(x = mpg, y = wt, customdata = nms)) + geom_point()
  l <- plotly_build(p)
  trace <- l$x$data[[1]]
  expect_equivalent(trace$customdata, nms)
})


test_that("Can provide list-columns to customdata", {
  l <- txhousing %>%
    group_by(city) %>%
    highlight_key(~city) %>%
    plot_ly(x = ~date, y = ~median, hoverinfo = "name") %>%
    add_lines(customdata = ~purrr::map2(date, median, ~list(.x, .y))) %>%
    plotly_build()
  
  trace <- l$x$data[[1]]
  expect_true(length(trace$customdata) == length(trace$x))
  
  # make sure customdata have been arranged properly
  customx <- unlist(lapply(trace$customdata, function(x) x[1] %||% NA))
  expect_equivalent(customx, trace$x)
  
  # check there is no customdata where x values are null
  nullcd <- trace$customdata[which(is.na(trace$x))]
  expect_true(unique(lengths(nullcd)) == 0)
})