Skip to content

Commit 3f312b7

Browse files
committed
add some tooltip tests; excepting handling for tooltip formatting
1 parent 00ad628 commit 3f312b7

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

R/ggplotly.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
225225
# text aestheic should be taken verbatim (for custom tooltips)
226226
prefix <- if (identical(aesName, "text")) "" else paste0(varName, ": ")
227227
# look for the domain, if that's not found, provide the range (useful for identity scales)
228-
suffix <- forMat(x[[paste0(aesName, "_plotlyDomain")]] %||% x[[aesName]])
228+
suffix <- tryCatch(
229+
forMat(x[[paste0(aesName, "_plotlyDomain")]] %||% x[[aesName]]),
230+
error = function(e) ""
231+
)
229232
x$hovertext <- paste0(x$hovertext, prefix, suffix)
230233
}
231234
x

tests/testthat/test-ggplot-density.R

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,3 @@ test_that("traces are ordered correctly in geom_density", {
7272
expect_identical(nms, c("4", "6", "8"))
7373
})
7474

75-
test_that("tooltip argument respects ordering", {
76-
p <- qplot(mpg, fill = factor(cyl), data = mtcars, geom = "density")
77-
p <- ggplotly(p, tooltip = c("y", "x"))
78-
info <- expect_traces(p, 3, "tooltip-order")
79-
txt <- strsplit(info$data[[1]]$text, "<br>")
80-
expect_true(all(grepl("^density", sapply(txt, "[[", 1))))
81-
expect_true(all(grepl("^mpg", sapply(txt, "[[", 2))))
82-
})
83-
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
context("tooltip")
2+
3+
test <- data.frame(
4+
time = strptime("2016-03-12 16:32:56", format = "%Y-%m-%d %X") + 60 * 1:100,
5+
x = cos(1:100)
6+
)
7+
p <- ggplot(test, aes(time, x)) + geom_point()
8+
9+
test_that("datetimes are displayed in tooltip properly", {
10+
l <- save_outputs(p, "tooltip-datetime")
11+
txt <- strsplit(l$data[[1]]$text, "<br>")
12+
expect_identical(
13+
paste0("time: ", test$time), sapply(txt, "[[", 1)
14+
)
15+
})
16+
17+
test <- data.frame(
18+
time = strptime("2016-03-12", format = "%Y-%m-%d") + 1:100,
19+
x = sin(1:100)
20+
)
21+
p <- ggplot(test, aes(time, x)) + geom_point()
22+
23+
test_that("dates are displayed in tooltip properly", {
24+
l <- save_outputs(p, "tooltip-date")
25+
txt <- strsplit(l$data[[1]]$text, "<br>")
26+
expect_identical(
27+
paste0("time: ", test$time), sapply(txt, "[[", 1)
28+
)
29+
})
30+
31+
test_that("tooltip argument respects ordering", {
32+
p <- qplot(mpg, fill = factor(cyl), data = mtcars, geom = "density")
33+
p <- ggplotly(p, tooltip = c("y", "x"))
34+
info <- plotly_build(p)
35+
txt <- strsplit(info$data[[1]]$text, "<br>")
36+
expect_true(all(grepl("^density", sapply(txt, "[[", 1))))
37+
expect_true(all(grepl("^mpg", sapply(txt, "[[", 2))))
38+
})
39+
40+
test_that("can hide x values in tooltip", {
41+
gg2 <- ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(cyl))) + geom_violin()
42+
p <- ggplotly(gg2, tooltip = "y")
43+
l <- plotly_build(p)
44+
expect_equal(sum(grepl("cyl", l$data[[1]]$text)), 0)
45+
})

tests/testthat/test-ggplot-violin.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,4 @@ test_that("geom_violin with fill aes works", {
2323
expect_equal(sum(unlist(lapply(L$data, "[[", "showlegend"))), 3)
2424
})
2525

26-
test_that("can hide x values in tooltip", {
27-
p <- ggplotly(gg2, tooltip = "y")
28-
l <- plotly_build(p)
29-
expect_equal(sum(grepl("cyl", l$data[[1]]$text)), 0)
30-
})
31-
3226

0 commit comments

Comments
 (0)