-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathtest-ggplot-ggplotly.R
55 lines (47 loc) · 2.08 KB
/
test-ggplot-ggplotly.R
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
context("ggplotly+plotly")
p <- ggplot(txhousing, aes(x = date, y = median, group = city)) +
geom_line(alpha = 0.3)
expect_identical_plotly <- function(object, expected, ...) {
expect_identical(names(object$x), names(expected$x))
exceptions <- c("attrs", "cur_data", "visdat", "layoutAttrs")
object$x <- object$x[setdiff(names(object$x), exceptions)]
expected$x <- expected$x[setdiff(names(expected$x), exceptions)]
expect_identical(object, expected, ...)
}
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")
expect_equivalent(dat, plotly_data(p + gginteractive()))
})
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")
expect_equivalent(
dat, (p + gginteractive()) %>% filter(city == "Houston") %>% plotly_data()
)
})
test_that("can add traces with original _and_ scaled data", {
l1 <- ggplotly(p) %>% add_lines() %>% plotly_build()
expect_equivalent(length(l1$x$data), 2)
expect_identical_plotly(
l1, (p + gginteractive()) %>% add_lines() %>% plotly_build()
)
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)
expect_identical_plotly(
l2, (p + gginteractive(originalData = FALSE)) %>% add_lines() %>% plotly_build()
)
})
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))
expect_identical_plotly(l, (p + gginteractive()) %>% layout(title = ~range(date)))
})
test_that("can suppress conversion of ggplot to plotly", {
expect_s3_class(p + gginteractive(interactive = FALSE), class(p))
})