diff --git a/R/event.R b/R/event.R index 073201f..0bd0cf1 100644 --- a/R/event.R +++ b/R/event.R @@ -44,17 +44,26 @@ dyEvent <- function(dygraph, warning("Argument 'date' is deprecated, please use argument 'x' instead") } - # create event - event <- list() - event$pos <- ifelse(dygraph$x$format == "date", asISO8601Time(x), x) - event$label <- label - event$labelLoc <- match.arg(labelLoc) - event$color <- color - event$strokePattern <- resolveStrokePattern(strokePattern) - event$axis <- "x" + # create events + if (!is.null(label) && length(x) != length(label)) + stop("Length of 'x' and 'label' does not match") + events <- + lapply( + seq_along(x), function(i) + { + list( + pos = ifelse(dygraph$x$format == "date", asISO8601Time(x[i]), x[i]) + ,label = ifelse(is.null(label),NULL, label[i] ) + ,labelLoc = match.arg(labelLoc, c("top", "bottom")) + ,color = color + ,strokePattern = resolveStrokePattern(strokePattern) + ,axis = "x" + ) + } + ) # add it to list of events - dygraph$x$events[[length(dygraph$x$events) + 1]] <- event + dygraph$x$events <- c(dygraph$x$events, events) # return modified dygraph dygraph diff --git a/tests/testthat/test-event.R b/tests/testthat/test-event.R index 90e0ddf..76dd3b8 100644 --- a/tests/testthat/test-event.R +++ b/tests/testthat/test-event.R @@ -4,8 +4,11 @@ context("dyEvent") test_that("event creation", { d <- dygraph(presidents, main = "Presidential Approval") %>% dyEvent(x = "1950-6-30", "Korea", labelLoc = "bottom") %>% - dyEvent(x = "1965-2-09", "Vietnam", labelLoc = "bottom") + dyEvent(x = "1965-2-09", "Vietnam", labelLoc = "bottom") + dd <- dygraph(presidents, main = "Presidential Approval") %>% + dyEvent(x = c("1950-6-30", "1965-2-09"), c("Korea", "Vietnam"), labelLoc = "bottom") expect_equal(length(d$x$events), 2) + expect_identical(d, dd) expect_identical(d$x$events[[1]]$label, "Korea") expect_identical(d$x$events[[2]]$label, "Vietnam") })