Skip to content

Commit fa088a2

Browse files
committed
add ... argument to add_fun(); fix annotation accumulation logic; add test
1 parent c6d77a1 commit fa088a2

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

R/add.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ special_attrs <- function(trace) {
490490
#' @param p a plotly object.
491491
#' @param fun a function. Should take a plotly object as input and return a
492492
#' modified plotly object.
493+
#' @param ... arguments passed to \code{fun}.
493494
#' @export
494495
#' @examples
495496
#'
@@ -515,9 +516,9 @@ special_attrs <- function(trace) {
515516
#' add_annotations(text = "Bad mileage")
516517
#' })
517518
#'
518-
add_fun <- function(p, fun) {
519+
add_fun <- function(p, fun, ...) {
519520
oldDat <- p$x$cur_data
520-
p <- fun(p)
521+
p <- fun(p, ...)
521522
p$x$cur_data <- oldDat
522523
p$x$attrs[length(p$x$attrs)] <- setNames(
523524
list(p$x$attrs[[length(p$x$attrs)]]), oldDat
@@ -539,12 +540,12 @@ add_fun <- function(p, fun) {
539540
#' # single annotation
540541
#' plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
541542
#' slice(which.max(mpg)) %>%
542-
#' annotation(text = "Good mileage")
543+
#' add_annotations(text = "Good mileage")
543544
#'
544545
#' # multiple annotations
545546
#' plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
546547
#' filter(gear == 5) %>%
547-
#' annotation("five cylinder", ax = 40)
548+
#' add_annotations("five cylinder", ax = 40)
548549
#'
549550

550551
add_annotations <- function(p, text = NULL, ..., data = NULL) {

R/plotly_build.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ plotly_build.plotly <- function(p) {
5555

5656
# accumulate, rather than override, annotations.
5757
annotations <- Reduce(c, c(
58-
if (is.null(names(p$x$layout$annotations))) p$x$layout$annotations else list(p$x$layout$annotations),
59-
compact(lapply(layouts, "[", "annotations"))
58+
list(p$x$layout$annotations),
59+
setNames(compact(lapply(layouts, "[[", "annotations")), NULL)
6060
))
61-
# annotations shouldn't have names
62-
annotations <- setNames(annotations[[1]], NULL)
6361

6462
# merge layouts into a single layout (more recent layouts will override older ones)
6563
p$x$layout <- modify_list(p$x$layout, Reduce(modify_list, layouts))

man/add_annotations.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/add_fun.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-plotly-data.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,28 @@ test_that("plotly_data preserves groups in data", {
1919
expect_true(dplyr::groups(d)[[1]] == "vs")
2020
})
2121

22+
test_that("add_fun can apply two different chunks of data to a plot", {
23+
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
24+
add_markers() %>%
25+
add_fun(function(p) {
26+
p %>% slice(which.max(mpg)) %>% add_annotations("Good mileage")
27+
}) %>%
28+
add_fun(function(p) {
29+
p %>% slice(which.min(mpg)) %>% add_annotations(text = "Bad mileage")
30+
})
31+
l <- plotly_build(p)[["x"]]
32+
expect_equal(length(l$layout$annotations), 2)
33+
expect_equal(
34+
sort(sapply(l$layout$annotations, "[[", "text")),
35+
c("Bad mileage", "Good mileage")
36+
)
37+
expect_equal(
38+
sort(sapply(l$layout$annotations, "[[", "x")),
39+
c(1.835, 5.250)
40+
)
41+
expect_equal(
42+
sort(sapply(l$layout$annotations, "[[", "y")),
43+
c(10.4, 33.9)
44+
)
45+
})
46+

0 commit comments

Comments
 (0)