File: test-ggplot-ggplotly.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 (30 lines) | stat: -rw-r--r-- 1,156 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

p <- ggplot(txhousing, aes(x = date, y = median, group = city)) +
  geom_line(alpha = 0.3)

test_that("ggplotly returns original data with special attributes", {
  dat <- ggplotly(p) %>% plotly_data()
  expect_equivalent(dat, p$data)
  expect_equivalent(as.character(dplyr::groups(dat)), "city")
})

test_that("can filter data returned by ggplotly", {
  dat <- ggplotly(p) %>% filter(city == "Houston") %>% plotly_data()
  expect_equivalent(dat, subset(p$data, city == "Houston"))
  expect_equivalent(as.character(dplyr::groups(dat)), "city")
})

test_that("can add traces with original _and_ scaled data", {
  l1 <- ggplotly(p) %>% add_lines() %>% plotly_build()
  expect_equivalent(length(l1$x$data), 2)
  l2 <- ggplotly(p, originalData = FALSE) %>% 
    add_lines() %>% plotly_build()
  # ideally we'd test that the two plots have the same data, but 
  # for some reason R CMD check throws an error which I can't replicate :(
  expect_equivalent(length(l2$x$data), 2)
})

test_that("can access ggplot data in layout()", {
  l <- ggplotly(p) %>% layout(title = ~range(date))
  expect_equivalent(plotly_build(l)$x$layout$title, range(txhousing$date))
})