Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Feature: convert ggplot to plotly by adding a layer #1599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
Loading
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test
  • Loading branch information
atusy committed Aug 15, 2019
commit bc7b88efffdd4c56cff5a5d807c089bb256f4432
20 changes: 20 additions & 0 deletions 20 tests/testthat/test-ggplot-ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,49 @@ 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)))
})
Morty Proxy This is a proxified and sanitized view of the page, visit original site.