From c170737ca643444ce339539722ede2cd1e987230 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Mon, 27 Apr 2015 20:23:43 -0400 Subject: [PATCH 1/7] Let gg2list() return a figure object --- R/ggplotly.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index e99cbc7ee9..6778c7b3d8 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -886,7 +886,8 @@ gg2list <- function(p){ flipped.layout[["yaxis"]] <- x } - flipped.traces$kwargs <- list(layout=flipped.layout) + fig <- list(data=flipped.traces, layout=flipped.layout) + + fig - flipped.traces } From 7f4435c710252eb61acba42002f89e28f223dd08 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 28 Apr 2015 03:41:45 -0400 Subject: [PATCH 2/7] Adapt use of gg2list return accordingly --- R/plotly.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/plotly.R b/R/plotly.R index 50c73631b8..4d4dba7d96 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -139,11 +139,15 @@ For more help, see https://plot.ly/R or contact .") if(!is.ggplot(gg)){ stop("gg must be a ggplot") } - pargs <- gg2list(gg) + fig <- gg2list(gg) if (!"auto_open" %in% names(kwargs)) { kwargs <- c(kwargs, auto_open=TRUE) } - pargs$kwargs <- c(pargs$kwargs, kwargs) + + pargs <- fig$data + pargs$kwargs <- kwargs + pargs$kwargs$layout <- fig$layout + if (session == "interactive") { # we are on the command line resp <- do.call(pub$plotly, pargs) if (pargs$kwargs$auto_open) { From 6b2a8e8a8f97bbcd642a627e97434494eca2963a Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 28 Apr 2015 03:43:37 -0400 Subject: [PATCH 3/7] Make use of kwargs default values consistently --- R/plotly.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plotly.R b/R/plotly.R index 4d4dba7d96..fc84463752 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -89,7 +89,7 @@ For more help, see https://plot.ly/R or contact .") if (is.null(kwargs$filename)) kwargs$filename <- pub$filename if (is.null(kwargs$fileopt)) - kwargs$fileopt <- NULL + kwargs$fileopt <- pub$fileopt url <- paste(base_url, "/clientresp", sep="") respst <- postForm(url, platform="R", version=pub$version, From e6f5aa7e894563813905d83c1aa5f9b09e62c54b Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 28 Apr 2015 15:58:41 -0400 Subject: [PATCH 4/7] Update tests with gg2list return as figure --- tests/testthat/test-cookbook-axes.R | 11 ++-- tests/testthat/test-ggplot-abline.R | 16 ++--- tests/testthat/test-ggplot-area.R | 14 ++--- tests/testthat/test-ggplot-bar.R | 47 +++++++------- tests/testthat/test-ggplot-boxplot.R | 18 +++--- tests/testthat/test-ggplot-categorical.R | 12 ++-- tests/testthat/test-ggplot-contour.R | 6 +- tests/testthat/test-ggplot-date.R | 26 ++++---- tests/testthat/test-ggplot-density.R | 5 +- tests/testthat/test-ggplot-density2d.R | 6 +- .../test-ggplot-errorbar-horizontal.R | 10 +-- tests/testthat/test-ggplot-errorbar.R | 7 +-- tests/testthat/test-ggplot-facets.R | 14 ++--- tests/testthat/test-ggplot-heatmap.R | 8 +-- tests/testthat/test-ggplot-histogram.R | 19 +++--- tests/testthat/test-ggplot-hline.R | 40 ++++++------ tests/testthat/test-ggplot-labels.R | 30 ++++----- tests/testthat/test-ggplot-legend.R | 27 ++++---- tests/testthat/test-ggplot-linetype.R | 16 ++--- tests/testthat/test-ggplot-names.R | 8 +-- tests/testthat/test-ggplot-path.R | 63 +++++++++---------- tests/testthat/test-ggplot-polygons.R | 9 ++- tests/testthat/test-ggplot-rect.R | 9 ++- tests/testthat/test-ggplot-ribbon.R | 5 +- tests/testthat/test-ggplot-segment.R | 4 +- tests/testthat/test-ggplot-size.R | 18 +++--- tests/testthat/test-ggplot-smooth.R | 5 +- tests/testthat/test-ggplot-stack.R | 14 ++--- tests/testthat/test-ggplot-step.R | 24 +++---- tests/testthat/test-ggplot-text.R | 22 +++---- tests/testthat/test-ggplot-theme.R | 20 +++--- tests/testthat/test-ggplot-ticks.R | 27 ++++---- tests/testthat/test-ggplot-vline.R | 34 +++++----- tests/testthat/test-ggplot-ylim.R | 9 ++- tests/testthat/test-mean-error-bars.R | 44 ++++++------- 35 files changed, 314 insertions(+), 333 deletions(-) diff --git a/tests/testthat/test-cookbook-axes.R b/tests/testthat/test-cookbook-axes.R index c77928ad84..29375d1c30 100644 --- a/tests/testthat/test-cookbook-axes.R +++ b/tests/testthat/test-cookbook-axes.R @@ -8,14 +8,13 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("cookbook-axes-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } # Reverse the order of a discrete-valued axis @@ -44,13 +43,13 @@ test_that("ylim hides points", { bp.scale.hide <- bp + scale_y_continuous(limits=c(5, 7.5)) test_that("scale_y(limits) hides points", { info <- expect_traces(bp.scale.hide, 3, "scale.hide") - expect_equal(info$kwargs$layout$yaxis$range, c(5, 7.5)) + expect_equal(info$layout$yaxis$range, c(5, 7.5)) }) bp.coord <- bp + coord_cartesian(ylim=c(5, 7.5)) test_that("Using coord_cartesian zooms into the area", { info <- expect_traces(bp.coord, 3, "coord-ylim") - expect_equal(info$kwargs$layout$yaxis$range, c(5, 7.5)) + expect_equal(info$layout$yaxis$range, c(5, 7.5)) }) # Create some noisy exponentially-distributed data @@ -148,7 +147,7 @@ bp.fonts <- bp + test_that("element_text face, colour, size, angle, vjust, size", { info <- expect_traces(bp.fonts, 3, "fonts") - x <- info$kwargs$layout$xaxis + x <- info$layout$xaxis xtitle <- x[["titlefont"]] xtick <- x[["tickfont"]] expect_identical(xtitle$color, toRGB("#990000")) diff --git a/tests/testthat/test-ggplot-abline.R b/tests/testthat/test-ggplot-abline.R index ebff610cd8..ef2e059832 100644 --- a/tests/testthat/test-ggplot-abline.R +++ b/tests/testthat/test-ggplot-abline.R @@ -12,15 +12,15 @@ test_that("Second trace be the a-b line", { L <- gg2list(gg) - expect_equal(length(L), 3) - expect_true(L[[2]]$x[1] <= 0) - expect_true(L[[2]]$x[2] >= 3.5) - expect_identical(L[[2]]$mode, "lines") - expect_identical(L[[2]]$line$shape, "linear") - expect_equal(L[[2]]$line$width, 8) + expect_equal(length(L$data), 2) + expect_true(L$data[[2]]$x[1] <= 0) + expect_true(L$data[[2]]$x[2] >= 3.5) + expect_identical(L$data[[2]]$mode, "lines") + expect_identical(L$data[[2]]$line$shape, "linear") + expect_equal(L$data[[2]]$line$width, 8) - expect_identical(L[[1]]$showlegend, FALSE) - expect_identical(L[[2]]$showlegend, FALSE) + expect_identical(L$data[[1]]$showlegend, FALSE) + expect_identical(L$data[[2]]$showlegend, FALSE) save_outputs(gg, "abline") }) diff --git a/tests/testthat/test-ggplot-area.R b/tests/testthat/test-ggplot-area.R index 49ec966aec..1c13496f0e 100644 --- a/tests/testthat/test-ggplot-area.R +++ b/tests/testthat/test-ggplot-area.R @@ -7,11 +7,11 @@ ar <- ggplot(huron) + geom_area(aes(x=year, y=level)) L <- gg2list(ar) test_that("sanity check for geom_area", { - expect_equal(length(L), 2) - expect_identical(L[[1]]$type, "scatter") - expect_equal(L[[1]]$x, c(huron$year[1], huron$year, tail(huron$year, n=1))) - expect_equal(L[[1]]$y, c(0, huron$level, 0)) - expect_identical(L[[1]]$line$color, "transparent") + expect_equal(length(L$data), 1) + expect_identical(L$data[[1]]$type, "scatter") + expect_equal(L$data[[1]]$x, c(huron$year[1], huron$year, tail(huron$year, n=1))) + expect_equal(L$data[[1]]$y, c(0, huron$level, 0)) + expect_identical(L$data[[1]]$line$color, "transparent") }) save_outputs(ar, "area") @@ -21,8 +21,8 @@ gg <- ggplot(huron) + geom_area(aes(x=year, y=level), alpha=0.4) L <- gg2list(gg) test_that("transparency alpha in geom_area is converted", { - expect_identical(L[[1]]$line$color, "transparent") - expect_identical(L[[1]]$fillcolor, "rgba(51,51,51,0.4)") + expect_identical(L$data[[1]]$line$color, "transparent") + expect_identical(L$data[[1]]$fillcolor, "rgba(51,51,51,0.4)") }) save_outputs(gg, "area-fillcolor") diff --git a/tests/testthat/test-ggplot-bar.R b/tests/testthat/test-ggplot-bar.R index e4235a8097..2922321f0e 100644 --- a/tests/testthat/test-ggplot-bar.R +++ b/tests/testthat/test-ggplot-bar.R @@ -1,18 +1,17 @@ context("bar") -expect_traces <- function(gg, n.traces, name){ +expect_traces <- function(gg, n.traces, name) { stopifnot(is.ggplot(gg)) stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("bar-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } researchers <- @@ -29,7 +28,7 @@ test_that("position_dodge is translated to barmode=group", { trs <- info$traces trace.names <- sapply(trs[1:2], "[[", "name") expect_true(all(c("Math", "Bio") %in% trace.names)) - expect_identical(info$kwargs$layout$barmode, "group") + expect_identical(info$layout$barmode, "group") # Check x values expect_identical(as.character(trs[[1]]$x), c("Canada", "Germany")) expect_identical(as.character(trs[[2]]$x), c("Canada", "USA")) @@ -41,7 +40,7 @@ test_that("position_stack is translated to barmode=stack", { trs <- info$traces trace.names <- sapply(trs[1:2], "[[", "name") expect_true(all(c("Math", "Bio") %in% trace.names)) - expect_identical(info$kwargs$layout$barmode, "stack") + expect_identical(info$layout$barmode, "stack") }) test_that("position_identity is translated to barmode=stack", { @@ -50,7 +49,7 @@ test_that("position_identity is translated to barmode=stack", { trs <- info$traces trace.names <- sapply(trs[1:2], "[[", "name") expect_true(all(c("Math", "Bio") %in% trace.names)) - expect_identical(info$kwargs$layout$barmode, "stack") + expect_identical(info$layout$barmode, "stack") }) test_that("dates work well with bar charts", { @@ -60,7 +59,7 @@ test_that("dates work well with bar charts", { geom_bar(stat="identity") info <- expect_traces(gd, 2, "dates") trs <- info$traces - expect_identical(info$kwargs$layout$xaxis$type, "date") + expect_identical(info$layout$xaxis$type, "date") # plotly likes time in milliseconds t <- as.numeric(unique(researchers$month)) * 24 * 60 * 60 * 1000 expect_identical(trs[[1]]$x, t) @@ -79,8 +78,8 @@ test_that("Very basic bar graph", { expect_null(tr$marker$line$color) expect_null(tr$marker$line$width) } - expect_null(info$kwargs$layout$annotations) - expect_false(info$kwargs$layout$showlegend) + expect_null(info$layout$annotations) + expect_false(info$layout$showlegend) }) test_that("Map the time of day to different fill colors", { @@ -93,8 +92,8 @@ test_that("Map the time of day to different fill colors", { expect_null(tr$marker$line$width) expect_true(tr$showlegend) } - expect_match(info$kwargs$layout$annotations[[1]]$text, "time") - expect_true(info$kwargs$layout$showlegend) + expect_match(info$layout$annotations[[1]]$text, "time") + expect_true(info$layout$showlegend) }) test_that("Add a black outline", { @@ -107,8 +106,8 @@ test_that("Add a black outline", { expect_equal(tr$marker$line$width, 1) expect_true(tr$showlegend) } - expect_match(info$kwargs$layout$annotations[[1]]$text, "time") - expect_true(info$kwargs$layout$showlegend) + expect_match(info$layout$annotations[[1]]$text, "time") + expect_true(info$layout$showlegend) }) test_that("guides(fill=FALSE) hides fill legend", { @@ -121,8 +120,8 @@ test_that("guides(fill=FALSE) hides fill legend", { expect_identical(tr$marker$line$color, toRGB("black")) expect_equal(tr$marker$line$width, 1) } - expect_null(info$kwargs$layout$annotations) - expect_false(info$kwargs$layout$showlegend) + expect_null(info$layout$annotations) + expect_false(info$layout$showlegend) }) test_that('guides(fill="none") hides fill legend', { @@ -135,8 +134,8 @@ test_that('guides(fill="none") hides fill legend', { expect_identical(tr$marker$line$color, toRGB("black")) expect_equal(tr$marker$line$width, 1) } - expect_null(info$kwargs$layout$annotations) - expect_false(info$kwargs$layout$showlegend) + expect_null(info$layout$annotations) + expect_false(info$layout$showlegend) }) test_that('guides(colour="none") does not affect fill legend', { @@ -150,8 +149,8 @@ test_that('guides(colour="none") does not affect fill legend', { expect_equal(tr$marker$line$width, 1) expect_true(tr$showlegend) } - expect_match(info$kwargs$layout$annotations[[1]]$text, "time") - expect_true(info$kwargs$layout$showlegend) + expect_match(info$layout$annotations[[1]]$text, "time") + expect_true(info$layout$showlegend) }) test_that("guides(fill=FALSE) does not affect colour legend", { @@ -165,8 +164,8 @@ test_that("guides(fill=FALSE) does not affect colour legend", { expect_equal(tr$marker$line$width, 1) expect_true(tr$showlegend) } - expect_match(info$kwargs$layout$annotations[[1]]$text, "time") - expect_true(info$kwargs$layout$showlegend) + expect_match(info$layout$annotations[[1]]$text, "time") + expect_true(info$layout$showlegend) }) @@ -174,7 +173,7 @@ base <- ggplot(mtcars, aes(factor(vs), fill=factor(cyl))) test_that("geom_bar() stacks counts", { info <- expect_traces(base + geom_bar(), 3, "position-stack") - expect_identical(info$kwargs$layout$barmode, "stack") + expect_identical(info$layout$barmode, "stack") trs <- info$traces # sum of y values for each trace test <- as.numeric(sort(sapply(trs, function(x) sum(x$y)))) @@ -184,7 +183,7 @@ test_that("geom_bar() stacks counts", { test_that("geom_bar(position = 'fill') stacks proportions", { info <- expect_traces(base + geom_bar(position = "fill"), 3, "position-fill") - expect_identical(info$kwargs$layout$barmode, "stack") + expect_identical(info$layout$barmode, "stack") trs <- info$traces # sum of y-values *conditioned* on a x-value prop <- sum(sapply(sapply(trs, "[[", "y"), "[", 1)) diff --git a/tests/testthat/test-ggplot-boxplot.R b/tests/testthat/test-ggplot-boxplot.R index 3849fd399c..ef9ac283f7 100644 --- a/tests/testthat/test-ggplot-boxplot.R +++ b/tests/testthat/test-ggplot-boxplot.R @@ -6,11 +6,11 @@ test_that("geom_boxplot gives a boxplot", { L <- gg2list(gg) # right nb. traces - expect_equal(length(L), 4) + expect_equal(length(L$data), 3) # right type for 1st trace - expect_identical(L[[1]]$type, "box") + expect_identical(L$data[[1]]$type, "box") # right data for 1st trace - expect_identical(sort(L[[1]]$y), + expect_identical(sort(L$data[[1]]$y), sort(mtcars$mpg[mtcars$cyl == 4])) save_outputs(gg, "boxplot") @@ -22,11 +22,11 @@ test_that("geom_violin is equated to geom_boxplot for now", { L <- gg2list(gg) # right nb. traces - expect_equal(length(L), 4) + expect_equal(length(L$data), 3) # right type for 1st trace - expect_identical(L[[1]]$type, "box") + expect_identical(L$data[[1]]$type, "box") # right data for 1st trace - expect_identical(sort(L[[1]]$y), + expect_identical(sort(L$data[[1]]$y), sort(mtcars$mpg[mtcars$cyl == 4])) save_outputs(gg, "violin") @@ -42,9 +42,9 @@ test_that("you can make a boxplot for a distribution of datetimes", { L <- gg2list(bp) - expect_equal(length(L), 2) # 1 trace + layout - expect_identical(L[[1]]$type, "box") - expect_identical(L[[1]]$y, as.character(df$y)) + expect_equal(length(L$data), 1) # 1 trace + expect_identical(L$data[[1]]$type, "box") + expect_identical(L$data[[1]]$y, as.character(df$y)) save_outputs(bp, "boxplot-datetime") }) diff --git a/tests/testthat/test-ggplot-categorical.R b/tests/testthat/test-ggplot-categorical.R index 01ca967c8d..ebe9cf773c 100644 --- a/tests/testthat/test-ggplot-categorical.R +++ b/tests/testthat/test-ggplot-categorical.R @@ -5,7 +5,7 @@ d <- head(diamonds, 50) test_that("axis type=category when we plot factors", { gg <- qplot(cut, price, data=d) info <- gg2list(gg) - l <- info$kwargs$layout + l <- info$layout expect_identical(l$xaxis$type, "category") expect_identical(l$yaxis$type, "linear") @@ -16,12 +16,12 @@ test_that("Bar charts of type=category show category names", { gbar <- ggplot(d, aes(cut, price)) + geom_bar(stat="identity") info <- gg2list(gbar) - expect_equal(length(info), 2) # 1 trace + layout - expect_identical(info$kwargs$layout$xaxis$type, "category") - expect_identical(info$kwargs$layout$xaxis$title, "cut") - expect_identical(info$kwargs$layout$yaxis$type, "linear") + expect_equal(length(info$data), 1) # 1 trace + expect_identical(info$layout$xaxis$type, "category") + expect_identical(info$layout$xaxis$title, "cut") + expect_identical(info$layout$yaxis$type, "linear") expect_true(all(c("Fair", "Good", "Very Good", "Premium", "Ideal") %in% - info[[1]]$x)) + info$data[[1]]$x)) save_outputs(gbar, "bar-category-names") }) diff --git a/tests/testthat/test-ggplot-contour.R b/tests/testthat/test-ggplot-contour.R index d7f3317e62..e0865a9524 100644 --- a/tests/testthat/test-ggplot-contour.R +++ b/tests/testthat/test-ggplot-contour.R @@ -7,12 +7,12 @@ gg <- ggplot(volcano3d) + geom_contour(aes(x=x, y=y, z=z)) L <- gg2list(gg) test_that("geom_contour is translated to type=contour", { - expect_equal(length(L), 2) - expect_identical(L[[1]]$type, "contour") + expect_equal(length(L$data), 1) + expect_identical(L$data[[1]]$type, "contour") }) test_that("geom_contour uses line contours by default", { - expect_identical(L[[1]]$contours$coloring, "lines") + expect_identical(L$data[[1]]$contours$coloring, "lines") }) save_outputs(gg, "contour") diff --git a/tests/testthat/test-ggplot-date.R b/tests/testthat/test-ggplot-date.R index e739984223..c806d96ed9 100644 --- a/tests/testthat/test-ggplot-date.R +++ b/tests/testthat/test-ggplot-date.R @@ -9,9 +9,9 @@ test_that("datetimes are converted to e.g. 2013-01-02 05:00:00", { data.frame(who="you", time.obj, dollars=c(10.2, 0))) gg <- qplot(time.obj, dollars, data=df, color=who, geom="line") info <- gg2list(gg) - expect_equal(length(info), 3) - expect_identical(info$kwargs$layout$xaxis$type, "date") - for(trace in info[1:2]){ + expect_equal(length(info$data), 2) + expect_identical(info$layout$xaxis$type, "date") + for(trace in info$data[1:2]){ expect_identical(trace$x, out.str) } @@ -24,9 +24,9 @@ test_that("class Date is supported", { df$x <- as.Date(df$x) gg <- ggplot(df) + geom_line(aes(x=x, y=y)) info <- gg2list(gg) - expect_equal(length(info), 2) - expect_identical(info$kwargs$layout$xaxis$type, "date") - expect_identical(info[[1]]$x[1], "2013-01-01 00:00:00") + expect_equal(length(info$data), 1) + expect_identical(info$layout$xaxis$type, "date") + expect_identical(info$data[[1]]$x[1], "2013-01-01 00:00:00") save_outputs(gg, "date-class-Date") }) @@ -46,13 +46,13 @@ test_that("scale_x_date and irregular time series work", { info <- gg2list(dt) info_w_scale <- gg2list(g) - expect_equal(length(info), 2) # one trace + layout - expect_identical(info[[1]]$x[31], "2122-02-09 00:00:00") - expect_equal(length(info_w_scale), 2) # one trace + layout - expect_identical(info_w_scale[[1]]$x[31], "2122-02-09 00:00:00") - expect_identical(info$kwargs$layout$xaxis$type, "date") - expect_identical(info_w_scale$kwargs$layout$xaxis$type, "date") - expect_equal(length(info_w_scale[[2]]), length(info[[2]])) # similar layout + expect_equal(length(info$data), 1) # one trace + expect_identical(info$data[[1]]$x[31], "2122-02-09 00:00:00") + expect_equal(length(info_w_scale$data), 1) # one trace + expect_identical(info_w_scale$data[[1]]$x[31], "2122-02-09 00:00:00") + expect_identical(info$layout$xaxis$type, "date") + expect_identical(info_w_scale$layout$xaxis$type, "date") + expect_equal(length(info_w_scale$layout), length(info$layout)) # similar layout save_outputs(dt, "date-irregular-time-series") }) diff --git a/tests/testthat/test-ggplot-density.R b/tests/testthat/test-ggplot-density.R index 0d77b191d4..8c0f4a4517 100644 --- a/tests/testthat/test-ggplot-density.R +++ b/tests/testthat/test-ggplot-density.R @@ -5,14 +5,13 @@ expect_traces <- function(gg, n.traces, name) { stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("density-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } # Draw a probability density estimation using geom_density diff --git a/tests/testthat/test-ggplot-density2d.R b/tests/testthat/test-ggplot-density2d.R index 93eaebf744..8747fb5a71 100644 --- a/tests/testthat/test-ggplot-density2d.R +++ b/tests/testthat/test-ggplot-density2d.R @@ -5,12 +5,12 @@ m <- ggplot(MASS::geyser) + geom_density2d(aes(x=duration, y=waiting)) L <- gg2list(m) test_that("geom_density2d is translated to type=histogram2dcontour", { - expect_equal(length(L), 2) - expect_identical(L[[1]]$type, "histogram2dcontour") + expect_equal(length(L$data), 1) + expect_identical(L$data[[1]]$type, "histogram2dcontour") }) test_that("geom_density2d uses line contours by default", { - expect_identical(L[[1]]$contours$coloring, "lines") + expect_identical(L$data[[1]]$contours$coloring, "lines") }) save_outputs(m, "density2d") diff --git a/tests/testthat/test-ggplot-errorbar-horizontal.R b/tests/testthat/test-ggplot-errorbar-horizontal.R index 917fc2d38b..5f97289f8a 100644 --- a/tests/testthat/test-ggplot-errorbar-horizontal.R +++ b/tests/testthat/test-ggplot-errorbar-horizontal.R @@ -15,13 +15,13 @@ test_that("geom_errorbarh gives horizontal errorbars", { L <- gg2list(g) # Expect 2 traces - expect_equal(length(L), 3) + expect_equal(length(L$data), 2) # Expect scatter plot and its error bars to have the same color - expect_identical(L[[1]]$marker$color, L[[1]]$error_x$color) - expect_identical(L[[2]]$marker$color, L[[2]]$error_x$color) + expect_identical(L$data[[1]]$marker$color, L$data[[1]]$error_x$color) + expect_identical(L$data[[2]]$marker$color, L$data[[2]]$error_x$color) # Expect given errorbar values - expect_equal(L[[1]]$error_x$array, c(0.1, 0.3)) - expect_true(L[[1]]$error_x$symmetric) + expect_equal(L$data[[1]]$error_x$array, c(0.1, 0.3)) + expect_true(L$data[[1]]$error_x$symmetric) save_outputs(g, "errorbar-horizontal") }) diff --git a/tests/testthat/test-ggplot-errorbar.R b/tests/testthat/test-ggplot-errorbar.R index 62f9f61b26..24a61fbfc5 100644 --- a/tests/testthat/test-ggplot-errorbar.R +++ b/tests/testthat/test-ggplot-errorbar.R @@ -10,12 +10,11 @@ test_that("geom_errorbar gives errorbars", { L <- gg2list(g) # right nb. traces (1) - expect_equal(length(L), 2) + expect_equal(length(L$data), 1) # trace #1 should be errorbar - expect_more_than(length(L[[1]]$error_y), 1) + expect_more_than(length(L$data[[1]]$error_y), 1) # right data for errorbar ymax - expect_equal(L[[1]]$error_y$array, c(3.74, 1.26, 1.15)) + expect_equal(L$data[[1]]$error_y$array, c(3.74, 1.26, 1.15)) save_outputs(g, "errorbar") }) - diff --git a/tests/testthat/test-ggplot-facets.R b/tests/testthat/test-ggplot-facets.R index e39575f853..e5c0919686 100644 --- a/tests/testthat/test-ggplot-facets.R +++ b/tests/testthat/test-ggplot-facets.R @@ -26,18 +26,18 @@ context("Facets") test_that("3 facets becomes 3 panels", { df <- data.frame(x=runif(99), y=runif(99), z=rep(c('a','b','c'), 33)) - gg <- qplot(x, y, data=df, facets=z~., pch=I(1))+ - theme_bw()+ + gg <- qplot(x, y, data=df, facets=z~., pch=I(1)) + + theme_bw() + theme(panel.margin=grid::unit(0, "cm")) info <- gg2list(gg) - traces <- info[names(info)==""] + traces <- info$data trace.axes <- list() for(N in c("xaxis", "yaxis")){ trace.axes[[N]] <- axes.vec <- - sapply(traces, function(t){ - if(N %in% names(t)){ + sapply(traces, function(t) { + if(N %in% names(t)) { t[[N]] - }else{ + } else { NA } }) @@ -51,7 +51,7 @@ test_that("3 facets becomes 3 panels", { # expect a certain number of _unique_ [x/y] axes expect_axes <- function(info, n, axis = "x") { pattern <- paste0("^", axis, "axis([0-9]+)?$") - axes <- with(info$kwargs, layout[grepl(pattern, names(layout))]) + axes <- with(info, layout[grepl(pattern, names(layout))]) n.axes <- length(axes) ranges <- do.call("rbind", lapply(axes, function(x) x$range)) expect_identical(nrow(unique(ranges)), as.integer(n)) diff --git a/tests/testthat/test-ggplot-heatmap.R b/tests/testthat/test-ggplot-heatmap.R index 1dc941507c..5b8f277340 100644 --- a/tests/testthat/test-ggplot-heatmap.R +++ b/tests/testthat/test-ggplot-heatmap.R @@ -13,10 +13,10 @@ hm <- ggplot(ww) + geom_tile(aes(x=day, y=time, fill=value)) test_that("geom_tile is translated to type=heatmap", { L <- gg2list(hm) - expect_equal(length(L), 2) - expect_identical(L[[1]]$type, "heatmap") - expect_identical(as.character(L[[1]]$x), wdays) - expect_identical(as.character(L[[1]]$y), dtimes) + expect_equal(length(L$data), 1) + expect_identical(L$data[[1]]$type, "heatmap") + expect_identical(as.character(L$data[[1]]$x), wdays) + expect_identical(as.character(L$data[[1]]$y), dtimes) }) save_outputs(hm, "heatmap") diff --git a/tests/testthat/test-ggplot-histogram.R b/tests/testthat/test-ggplot-histogram.R index d0aaeb7ea5..4c11fd122c 100644 --- a/tests/testthat/test-ggplot-histogram.R +++ b/tests/testthat/test-ggplot-histogram.R @@ -1,25 +1,24 @@ context("Histogram") -expect_traces <- function(gg, n.traces, name){ +expect_traces <- function(gg, n.traces, name) { stopifnot(is.ggplot(gg)) stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("histogram-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } -base <- ggplot(mtcars, aes(wt)) +base <- ggplot(mtcars, aes(wt)) test_that("geom_histogram() is a bar chart of counts with no bargap", { info <- expect_traces(base + geom_histogram(), 1, "counts") - expect_identical(info$kwargs$layout$bargap, 0) + expect_identical(info$layout$bargap, 0) tr <- info$traces[[1]] expect_identical(tr$type, "bar") expect_equal(sum(tr$y), nrow(mtcars)) @@ -27,7 +26,7 @@ test_that("geom_histogram() is a bar chart of counts with no bargap", { test_that("geom_histogram(aes(y = ..density..)) displays a density", { info <- expect_traces(base + geom_histogram(aes(y=..density..)), 1, "density") - expect_identical(info$kwargs$layout$bargap, 0) + expect_identical(info$layout$bargap, 0) tr <- info$traces[[1]] expect_identical(tr$type, "bar") #default binwidth @@ -72,7 +71,7 @@ noram$month <- as.Date(noram$month) test_that("dates work well with histograms", { hist <- ggplot(noram, aes(month)) + geom_histogram() info <- expect_traces(hist, 1, "dates") - expect_identical(info$kwargs$layout$xaxis$type, "date") + expect_identical(info$layout$xaxis$type, "date") #test <- with(info[[1]], setNames(y, x)) #true <- table(noram$month) # these are off by 1 day, not sure why, but I don't think it's worth @@ -186,7 +185,7 @@ test_that("datetime binning for class POSIXt works in histograms", { kP$date <- as.POSIXlt(kP$date) histP <- ggplot(kP, aes(x=date)) + geom_histogram(binwidth=2592000) info <- expect_traces(histP, 1, "POSIXt-bins") - expect_identical(info$kwargs$layout$xaxis$type, "date") + expect_identical(info$layout$xaxis$type, "date") }) test_that("datetime binning for class Date works in histograms", { @@ -194,5 +193,5 @@ test_that("datetime binning for class Date works in histograms", { kD$date <- as.Date(kD$date) histD <- ggplot(kD, aes(x=date)) + geom_histogram(binwidth=30) info <- expect_traces(histD, 1, "Date-bins") - expect_identical(info$kwargs$layout$xaxis$type, "date") + expect_identical(info$layout$xaxis$type, "date") }) diff --git a/tests/testthat/test-ggplot-hline.R b/tests/testthat/test-ggplot-hline.R index e47a36d475..de8701f54b 100644 --- a/tests/testthat/test-ggplot-hline.R +++ b/tests/testthat/test-ggplot-hline.R @@ -11,14 +11,14 @@ test_that("second trace be the hline", { L <- gg2list(gg) - expect_equal(length(L), 3) - expect_equal(L[[2]]$y[1], 1.1) - expect_true(L[[2]]$x[1] <= 0) - expect_true(L[[2]]$x[2] >= 3.5) - expect_identical(L[[2]]$mode, "lines") - expect_identical(L[[2]]$line$shape, "linear") - expect_equal(L[[2]]$line$width, 6) - expect_identical(L[[2]]$line$color, "rgb(0,255,0)") + expect_equal(length(L$data), 2) + expect_equal(L$data[[2]]$y[1], 1.1) + expect_true(L$data[[2]]$x[1] <= 0) + expect_true(L$data[[2]]$x[2] >= 3.5) + expect_identical(L$data[[2]]$mode, "lines") + expect_identical(L$data[[2]]$line$shape, "linear") + expect_equal(L$data[[2]]$line$width, 6) + expect_identical(L$data[[2]]$line$color, "rgb(0,255,0)") save_outputs(gg, "hline") }) @@ -28,16 +28,16 @@ test_that("vector yintercept results in multiple horizontal lines", { L <- gg2list(gg) - expect_equal(length(L), 5) - expect_equal(L[[2]]$y[1], 1) - expect_equal(L[[3]]$y[1], 2) - expect_equal(L[[4]]$y[1], 3) - expect_true(L[[4]]$x[1] <= 0) - expect_true(L[[4]]$x[2] >= 3.325) - expect_identical(L[[3]]$mode, "lines") - expect_identical(L[[3]]$line$shape, "linear") - expect_equal(L[[3]]$line$width, 6) - expect_identical(L[[3]]$line$color, "rgb(255,0,0)") + expect_equal(length(L$data), 4) + expect_equal(L$data[[2]]$y[1], 1) + expect_equal(L$data[[3]]$y[1], 2) + expect_equal(L$data[[4]]$y[1], 3) + expect_true(L$data[[4]]$x[1] <= 0) + expect_true(L$data[[4]]$x[2] >= 3.325) + expect_identical(L$data[[3]]$mode, "lines") + expect_identical(L$data[[3]]$line$shape, "linear") + expect_equal(L$data[[3]]$line$width, 6) + expect_identical(L$data[[3]]$line$color, "rgb(255,0,0)") save_outputs(gg, "hline-multiple") }) @@ -48,8 +48,8 @@ test_that("hline can be drawn over range of factors", { geom_bar(position="dodge", stat="identity") + geom_hline(aes(yintercept=12)) L <- gg2list(gg) - expect_equal(length(L), 3) # 1 trace for bar chart, 1 trace for hline, layout - expect_true(all(c("control", "treatment") %in% L[[2]]$x)) + expect_equal(length(L$data), 2) # 1 trace for bar chart, 1 trace for hline + expect_true(all(c("control", "treatment") %in% L$data[[2]]$x)) save_outputs(gg, "hline-factor") }) diff --git a/tests/testthat/test-ggplot-labels.R b/tests/testthat/test-ggplot-labels.R index 52f61670f2..3fce947a3f 100644 --- a/tests/testthat/test-ggplot-labels.R +++ b/tests/testthat/test-ggplot-labels.R @@ -1,45 +1,45 @@ context("labels") test_that("ggtitle is translated correctly", { - ggiris <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width))+ + ggiris <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width)) + ggtitle("My amazing plot!") info <- gg2list(ggiris) - expect_identical(info$kwargs$layout$title, "My amazing plot!") + expect_identical(info$layout$title, "My amazing plot!") save_outputs(ggiris, "labels-ggtitle") }) test_that("ylab is translated correctly", { - ggiris <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width))+ + ggiris <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width)) + ylab("sepal width") info <- gg2list(ggiris) - expect_identical(info$kwargs$layout$xaxis$title, "Petal.Width") - expect_identical(info$kwargs$layout$yaxis$title, "sepal width") + expect_identical(info$layout$xaxis$title, "Petal.Width") + expect_identical(info$layout$yaxis$title, "sepal width") save_outputs(ggiris, "labels-ylab") }) test_that("scale_x_continuous(name) is translated correctly", { - ggiris <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width))+ + ggiris <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width)) + scale_x_continuous("petal width") info <- gg2list(ggiris) - expect_identical(info$kwargs$layout$xaxis$title, "petal width") - expect_identical(info$kwargs$layout$yaxis$title, "Sepal.Width") + expect_identical(info$layout$xaxis$title, "petal width") + expect_identical(info$layout$yaxis$title, "Sepal.Width") save_outputs(ggiris, "labels-scale_x_continuous_name") }) test_that("angled ticks are translated correctly", { - ggiris <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width))+ + ggiris <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width)) + theme(axis.text.x=element_text(angle=45)) info <- gg2list(ggiris) - expect_identical(info$kwargs$layout$xaxis$tickangle, -45) + expect_identical(info$layout$xaxis$tickangle, -45) save_outputs(ggiris, "labels-angles") }) -##TODO: test label colors. +# TODO: test label colors. diff --git a/tests/testthat/test-ggplot-legend.R b/tests/testthat/test-ggplot-legend.R index 56c26b590c..c38b3de4cc 100644 --- a/tests/testthat/test-ggplot-legend.R +++ b/tests/testthat/test-ggplot-legend.R @@ -5,22 +5,21 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("legend-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } test_that("legend can be hidden", { - ggiris <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width, color=Species))+ + ggiris <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width, color=Species)) + theme(legend.position="none") info <- expect_traces(ggiris, 3, "iris-position-none") - expect_identical(info$kwargs$layout$showlegend, FALSE) + expect_identical(info$layout$showlegend, FALSE) }) getnames <- function(traces){ @@ -31,7 +30,7 @@ getnames <- function(traces){ } test_that("legend entries appear in the correct order", { - ggiris <- ggplot(iris)+ + ggiris <- ggplot(iris) + geom_point(aes(Petal.Width, Sepal.Width, color=Species)) info <- expect_traces(ggiris, 3, "iris-default") computed.showlegend <- sapply(info$traces, "[[", "showlegend") @@ -48,8 +47,8 @@ test_that("legend entries appear in the correct order", { test_that("2 breaks -> 1 named trace with showlegend=FALSE", { two.breaks <- c("setosa", "versicolor") - two.legend.entries <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width, color=Species))+ + two.legend.entries <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width, color=Species)) + scale_color_discrete(breaks=two.breaks) info <- expect_traces(two.legend.entries, 3, "iris-trace-showlegend-FALSE") expected.names <- levels(iris$Species) @@ -61,8 +60,8 @@ test_that("2 breaks -> 1 named trace with showlegend=FALSE", { test_that("1 break -> 2 traces with showlegend=FALSE", { one.break <- c("setosa") - one.legend.entry <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width, color=Species))+ + one.legend.entry <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width, color=Species)) + scale_color_discrete(breaks=one.break) info <- expect_traces(one.legend.entry, 3, "iris-2traces-showlegend-FALSE") expected.names <- levels(iris$Species) @@ -74,11 +73,11 @@ test_that("1 break -> 2 traces with showlegend=FALSE", { test_that("0 breaks -> 3 traces with showlegend=FALSE", { no.breaks <- c() - no.legend.entries <- ggplot(iris)+ - geom_point(aes(Petal.Width, Sepal.Width, color=Species))+ + no.legend.entries <- ggplot(iris) + + geom_point(aes(Petal.Width, Sepal.Width, color=Species)) + scale_color_discrete(breaks=no.breaks) info <- expect_traces(no.legend.entries, 3, "iris-3traces-showlegend-FALSE") - expect_equal(length(info$kwargs$layout$annotations), 0) + expect_equal(length(info$layout$annotations), 0) expected.names <- levels(iris$Species) expected.showlegend <- expected.names %in% no.breaks expect_identical(getnames(info$traces), expected.names) diff --git a/tests/testthat/test-ggplot-linetype.R b/tests/testthat/test-ggplot-linetype.R index 8aa32b332f..88813ea427 100644 --- a/tests/testthat/test-ggplot-linetype.R +++ b/tests/testthat/test-ggplot-linetype.R @@ -12,7 +12,7 @@ test_that("6 different automatic lty converted to plotly's 6 types", { "longdash", "longdashdot") info <- gg2list(gg) - generated <- sapply(info[1:6], function(L) L$line$dash) + generated <- sapply(info$data[1:6], function(L) L$line$dash) expect_true(all(generated %in% expected)) expect_true(all(expected %in% generated)) @@ -33,14 +33,14 @@ test_that("different colored lines become different colored traces", { geom_line()+ scale_color_manual(values=c(y1="blue", y2="red")) info <- gg2list(gg) - expect_equal(length(info), 3) - expect_identical(info[[1]]$line$color, toRGB("blue")) + expect_equal(length(info$data), 2) + expect_identical(info$data[[1]]$line$color, toRGB("blue")) n <- length(x) - expect_identical(info[[1]]$y[1:n], y1) - expect_identical(info[[1]]$x[1:n], x) - expect_identical(info[[2]]$line$color, toRGB("red")) - expect_identical(info[[2]]$y[1:n], y2) - expect_identical(info[[2]]$x[1:n], x) + expect_identical(info$data[[1]]$y[1:n], y1) + expect_identical(info$data[[1]]$x[1:n], x) + expect_identical(info$data[[2]]$line$color, toRGB("red")) + expect_identical(info$data[[2]]$y[1:n], y2) + expect_identical(info$data[[2]]$x[1:n], x) save_outputs(gg, "linetype-colors") }) diff --git a/tests/testthat/test-ggplot-names.R b/tests/testthat/test-ggplot-names.R index 9745189d53..391e1a7a2e 100644 --- a/tests/testthat/test-ggplot-names.R +++ b/tests/testthat/test-ggplot-names.R @@ -3,13 +3,13 @@ context("names") test_that("name param is passed to plotly", { library(maps) data(canada.cities) - gg <- ggplot(canada.cities, aes(long, lat))+ - borders(regions="canada", name="borders")+ + gg <- ggplot(canada.cities, aes(long, lat)) + + borders(regions="canada", name="borders") + geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, pch=1, name="cities") info <- gg2list(gg) - expect_identical(info[[1]]$name, "borders") - expect_identical(info[[2]]$name, "cities") + expect_identical(info$data[[1]]$name, "borders") + expect_identical(info$data[[2]]$name, "cities") save_outputs(gg, "names") }) diff --git a/tests/testthat/test-ggplot-path.R b/tests/testthat/test-ggplot-path.R index 9d6a70b2a1..16e9729518 100644 --- a/tests/testthat/test-ggplot-path.R +++ b/tests/testthat/test-ggplot-path.R @@ -5,13 +5,12 @@ test_that("lines are different from paths", { y=c(0, 0, 1)) p <- qplot(x, y, data=df, geom="path") info <- gg2list(p) - p.tr <- info[[1]] - expect_identical(p.tr$x[1:3], c(1, 3, 2)) - expect_identical(p.tr$y[1:3], c(0, 0, 1)) + expect_identical(info$data[[1]]$x[1:3], c(1, 3, 2)) + expect_identical(info$data[[1]]$y[1:3], c(0, 0, 1)) l <- qplot(x, y, data=df, geom="line") - l.tr <- gg2list(l)[[1]] - expect_identical(l.tr$x[1:3], c(1, 2, 3)) - expect_identical(l.tr$y[1:3], c(0, 1, 0)) + l.tr <- gg2list(l)$data + expect_identical(l.tr[[1]]$x[1:3], c(1, 2, 3)) + expect_identical(l.tr[[1]]$y[1:3], c(0, 1, 0)) save_outputs(p, "path-lines-diff-from-paths") }) @@ -24,24 +23,24 @@ test_that("paths with different colors become different traces", { gg <- ggplot()+ geom_path(aes(x, y, group=y, color=y), data=two.paths) info <- gg2list(gg) - expect_equal(length(info), 3) - trace.names <- sapply(info[1:2], "[[", "name") + expect_equal(length(info$data), 2) + trace.names <- sapply(info$data[1:2], "[[", "name") expect_identical(as.character(trace.names), c("1", "2")) - expect_identical(info[[1]]$x[1:2], c(1,2)) - expect_identical(info[[2]]$x[1:2], c(1,2)) - expect_identical(info[[1]]$y[1:2], c(1,1)) - expect_identical(info[[2]]$y[1:2], c(2,2)) + expect_identical(info$data[[1]]$x[1:2], c(1,2)) + expect_identical(info$data[[2]]$x[1:2], c(1,2)) + expect_identical(info$data[[1]]$y[1:2], c(1,1)) + expect_identical(info$data[[2]]$y[1:2], c(2,2)) ## Categorical color. gg <- ggplot()+ geom_path(aes(x, y, group=y, color=paste0("FOO", y)), data=two.paths) info <- gg2list(gg) - expect_equal(length(info), 3) - trace.names <- sapply(info[1:2], "[[", "name") + expect_equal(length(info$data), 2) + trace.names <- sapply(info$data[1:2], "[[", "name") expect_identical(as.character(trace.names), c("FOO1", "FOO2")) - expect_identical(info[[1]]$x[1:2], c(1,2)) - expect_identical(info[[2]]$x[1:2], c(1,2)) - expect_identical(info[[1]]$y[1:2], c(1,1)) - expect_identical(info[[2]]$y[1:2], c(2,2)) + expect_identical(info$data[[1]]$x[1:2], c(1,2)) + expect_identical(info$data[[2]]$x[1:2], c(1,2)) + expect_identical(info$data[[1]]$y[1:2], c(1,1)) + expect_identical(info$data[[2]]$y[1:2], c(2,2)) save_outputs(gg, "path-colors") }) @@ -53,13 +52,13 @@ test_that("paths with the same color but different groups stay together", { gg <- ggplot()+ geom_path(aes(x, y, group=y, color=g), data=four.paths) info <- gg2list(gg) - expect_equal(length(info), 3) - expect_identical(info[[1]]$name, "positive") - expect_identical(info[[2]]$name, "negative") - expect_true(any(is.na(info[[1]]$x))) - expect_true(any(is.na(info[[1]]$y))) - expect_true(any(is.na(info[[2]]$x))) - expect_true(any(is.na(info[[2]]$y))) + expect_equal(length(info$data), 2) + expect_identical(info$data[[1]]$name, "positive") + expect_identical(info$data[[2]]$name, "negative") + expect_true(any(is.na(info$data[[1]]$x))) + expect_true(any(is.na(info$data[[1]]$y))) + expect_true(any(is.na(info$data[[2]]$x))) + expect_true(any(is.na(info$data[[2]]$y))) save_outputs(gg, "path-colored-groups-stay-together") }) @@ -73,14 +72,14 @@ test_that("lines work with aesthetic shape", { geom_line() + geom_point() info <- gg2list(gg) - expect_equal(length(info), 3) # 2 traces + layout - expect_identical(info[[1]]$name, "Female") - expect_identical(info[[1]]$marker$symbol, "circle") - expect_identical(info[[2]]$name, "Male") - expect_identical(info[[2]]$marker$symbol, "triangle-up") + expect_equal(length(info$data), 2) # 2 traces + expect_identical(info$data[[1]]$name, "Female") + expect_identical(info$data[[1]]$marker$symbol, "circle") + expect_identical(info$data[[2]]$name, "Male") + expect_identical(info$data[[2]]$marker$symbol, "triangle-up") # Layout - expect_identical(info[[3]]$layout$xaxis$title, "time") - expect_identical(info[[3]]$layout$xaxis$type, "category") + expect_identical(info$layout$xaxis$title, "time") + expect_identical(info$layout$xaxis$type, "category") save_outputs(gg, "path-line-symbols") }) diff --git a/tests/testthat/test-ggplot-polygons.R b/tests/testthat/test-ggplot-polygons.R index 097d9588ee..ddd17f6a99 100644 --- a/tests/testthat/test-ggplot-polygons.R +++ b/tests/testthat/test-ggplot-polygons.R @@ -5,14 +5,13 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("polygon-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } poly.df <- data.frame(x=c(0, 1, 1, 0, 2, 3, 3, 2)+10, @@ -144,8 +143,8 @@ test_that("borders become one trace with NA", { gg <- ggplot(canada.cities, aes(long, lat))+ borders(regions="canada", name="borders") info <- gg2list(gg) - expect_identical(length(info), 2L) - tr <- info[[1]] + expect_equal(length(info$data), 1) + tr <- info$data[[1]] expect_true(any(is.na(tr$x))) save_outputs(gg, "polygons-canada-borders") diff --git a/tests/testthat/test-ggplot-rect.R b/tests/testthat/test-ggplot-rect.R index 964698a8a3..d5ea9a9923 100644 --- a/tests/testthat/test-ggplot-rect.R +++ b/tests/testthat/test-ggplot-rect.R @@ -1,18 +1,17 @@ context("geom_rect") -expect_traces <- function(gg, n.traces, name){ +expect_traces <- function(gg, n.traces, name) { stopifnot(is.ggplot(gg)) stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("rect-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } set.seed(1) @@ -30,7 +29,7 @@ test_that('geom_rect becomes 1 trace with mode="lines" fill="tozerox"', { expect_identical(tr$fill, "tozerox") expect_identical(tr$type, "scatter") expect_identical(tr$mode, "lines") - for(xy in c("x", "y")){ + for(xy in c("x", "y")) { expect_true(anyNA(tr[[xy]])) } }) diff --git a/tests/testthat/test-ggplot-ribbon.R b/tests/testthat/test-ggplot-ribbon.R index 7ec354a8aa..40c7f0a489 100644 --- a/tests/testthat/test-ggplot-ribbon.R +++ b/tests/testthat/test-ggplot-ribbon.R @@ -5,14 +5,13 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("ribbon-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron)) diff --git a/tests/testthat/test-ggplot-segment.R b/tests/testthat/test-ggplot-segment.R index bcc3018f0d..d1ec1dfaed 100644 --- a/tests/testthat/test-ggplot-segment.R +++ b/tests/testthat/test-ggplot-segment.R @@ -5,10 +5,10 @@ test_that("segments become one path", { y=c(0, 1), xend=c(1, 1), yend=c(0, 1)) - gg <- ggplot()+ + gg <- ggplot() + geom_segment(aes(x, y, xend=xend, yend=yend), data=seg.df) info <- gg2list(gg) - tr <- info[[1]] + tr <- info$data[[1]] expect_true(any(is.na(tr$x))) expect_true(any(is.na(tr$y))) diff --git a/tests/testthat/test-ggplot-size.R b/tests/testthat/test-ggplot-size.R index c688558e93..66274dad0c 100644 --- a/tests/testthat/test-ggplot-size.R +++ b/tests/testthat/test-ggplot-size.R @@ -1,10 +1,10 @@ context("size") test_that("size is not a vector if it is not specified", { - iplot <- ggplot(iris)+ + iplot <- ggplot(iris) + geom_point(aes(Petal.Width, Sepal.Width)) L <- gg2list(iplot) - m <- L[[1]]$marker + m <- L$data[[1]]$marker expect_that(m, is_a("list")) expect_true(length(m$size) <= 1) @@ -12,14 +12,14 @@ test_that("size is not a vector if it is not specified", { }) test_that("size is a vector if it is specified", { - iplot <- ggplot(iris)+ + iplot <- ggplot(iris) + geom_point(aes(Petal.Width, Sepal.Width, size=Petal.Length)) L <- gg2list(iplot) - m <- L[[1]]$marker + m <- L$data[[1]]$marker expect_that(m, is_a("list")) expect_true(length(m$size) > 1) - expect_identical(L[[1]]$showlegend, FALSE) + expect_identical(L$data[[1]]$showlegend, FALSE) save_outputs(iplot, "size-is-a-vector") }) @@ -34,9 +34,11 @@ gg <- ggplot(countrypop, aes(edu, illn, colour=country, size=population)) + test_that("global scaling works for sizes over different traces", { L <- gg2list(gg) - expect_equal(length(L), 4) # 1 trace per country (3) + layout - expect_true(as.numeric(L[[1]]$marker$size) < as.numeric(L[[2]]$marker$size)) - expect_true(as.numeric(L[[2]]$marker$size) < as.numeric(L[[3]]$marker$size)) + expect_equal(length(L$data), 3) # 1 trace per country (3) + expect_true(as.numeric(L$data[[1]]$marker$size) < + as.numeric(L$data[[2]]$marker$size)) + expect_true(as.numeric(L$data[[2]]$marker$size) < + as.numeric(L$data[[3]]$marker$size)) }) save_outputs(gg, "size-global-scaling") diff --git a/tests/testthat/test-ggplot-smooth.R b/tests/testthat/test-ggplot-smooth.R index cc1b9064bf..4326a0e39c 100644 --- a/tests/testthat/test-ggplot-smooth.R +++ b/tests/testthat/test-ggplot-smooth.R @@ -5,14 +5,13 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("smooth-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + geom_smooth() diff --git a/tests/testthat/test-ggplot-stack.R b/tests/testthat/test-ggplot-stack.R index c650c305ed..fc47cc23b5 100644 --- a/tests/testthat/test-ggplot-stack.R +++ b/tests/testthat/test-ggplot-stack.R @@ -12,14 +12,14 @@ test_that("y value is non-cumulative in stacked bar charts", { gg <- ggplot(instructors, aes(x=topic, y=number, fill=level)) + geom_bar(stat="identity") L <- gg2list(gg) - expect_equal(length(L), 4) - expect_identical(L$kwargs$layout$barmode, "stack") - trace.names <- sapply(L[1:3], "[[", "name") + expect_equal(length(L$data), 3) + expect_identical(L$layout$barmode, "stack") + trace.names <- sapply(L$data[1:3], "[[", "name") expect_true(all(c("1) Novice", "2) Intermediate") %in% trace.names)) - expect_equal(L[[2]]$y[1], instructors$number[2]) - expect_equal(L[[3]]$y[1], instructors$number[3]) - expect_equal(L[[2]]$y[2], instructors$number[5]) - expect_equal(L[[3]]$y[2], instructors$number[6]) + expect_equal(L$data[[2]]$y[1], instructors$number[2]) + expect_equal(L$data[[3]]$y[1], instructors$number[3]) + expect_equal(L$data[[2]]$y[2], instructors$number[5]) + expect_equal(L$data[[3]]$y[2], instructors$number[6]) save_outputs(gg, "stack") }) diff --git a/tests/testthat/test-ggplot-step.R b/tests/testthat/test-ggplot-step.R index 4fe6298f85..0aa8bb837d 100644 --- a/tests/testthat/test-ggplot-step.R +++ b/tests/testthat/test-ggplot-step.R @@ -10,8 +10,8 @@ gg <- ggplot(oranges, aes(x=age, y=circumference, test_that("direction of geom_line is translated to shape=linear", { gg.linear <- gg + geom_line() L <- gg2list(gg.linear) - expect_equal(length(L), 3) - expect_identical(L[[1]]$line$shape, "linear") + expect_equal(length(L$data), 2) + expect_identical(L$data[[1]]$line$shape, "linear") save_outputs(gg.linear, "step-gg.linear-geom_line") }) @@ -19,8 +19,8 @@ test_that("direction of geom_line is translated to shape=linear", { test_that("direction of geom_path is translated to shape=linear", { gg.lin <- gg + geom_path() L <- gg2list(gg.lin) - expect_equal(length(L), 3) - expect_identical(L[[1]]$line$shape, "linear") + expect_equal(length(L$data), 2) + expect_identical(L$data[[1]]$line$shape, "linear") save_outputs(gg.lin, "step-gg.linear-geom_path") }) @@ -28,8 +28,8 @@ test_that("direction of geom_path is translated to shape=linear", { test_that("direction hv is translated to shape=hv", { gg.hv <- gg + geom_step() L <- gg2list(gg.hv) - expect_equal(length(L), 3) - expect_identical(L[[1]]$line$shape, "hv") + expect_equal(length(L$data), 2) + expect_identical(L$data[[1]]$line$shape, "hv") save_outputs(gg.hv, "step-gg.hv") }) @@ -37,8 +37,8 @@ test_that("direction hv is translated to shape=hv", { test_that("direction vh is translated to shape=vh", { gg.vh <- gg + geom_step(direction="vh") L <- gg2list(gg.vh) - expect_equal(length(L), 3) - expect_identical(L[[1]]$line$shape, "vh") + expect_equal(length(L$data), 2) + expect_identical(L$data[[1]]$line$shape, "vh") save_outputs(gg.vh, "step-gg.vh") }) @@ -46,8 +46,8 @@ test_that("direction vh is translated to shape=vh", { test_that("direction hvh is translated to shape=hvh", { gg.hvh <- gg + geom_step(direction="hvh") L <- gg2list(gg.hvh) - expect_equal(length(L), 3) - expect_identical(L[[1]]$line$shape, "hvh") + expect_equal(length(L$data), 2) + expect_identical(L$data[[1]]$line$shape, "hvh") save_outputs(gg.hvh, "step-gg.hvh", TRUE) }) @@ -55,8 +55,8 @@ test_that("direction hvh is translated to shape=hvh", { test_that("direction vhv is translated to shape=vhv", { gg.vhv <- gg + geom_step(direction="vhv") L <- gg2list(gg.vhv) - expect_equal(length(L), 3) - expect_identical(L[[1]]$line$shape, "vhv") + expect_equal(length(L$data), 2) + expect_identical(L$data[[1]]$line$shape, "vhv") save_outputs(gg.vhv, "step-gg.vhv", TRUE) }) diff --git a/tests/testthat/test-ggplot-text.R b/tests/testthat/test-ggplot-text.R index 3ee10a058e..c2c8658270 100644 --- a/tests/testthat/test-ggplot-text.R +++ b/tests/testthat/test-ggplot-text.R @@ -5,16 +5,16 @@ gg <- ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + info <- gg2list(gg) test_that("label is translated correctly", { - expect_identical(as.character(info[[1]]$text), rownames(mtcars)) + expect_identical(as.character(info$data[[1]]$text), rownames(mtcars)) }) test_that("position is translated correctly", { - expect_identical(info[[1]]$x, mtcars$wt) - expect_identical(info[[1]]$y, mtcars$mpg) + expect_identical(info$data[[1]]$x, mtcars$wt) + expect_identical(info$data[[1]]$y, mtcars$mpg) }) test_that("textsize is translated correctly", { - expect_identical(info[[1]]$textfont$size, 18) + expect_identical(info$data[[1]]$textfont$size, 18) }) save_outputs(gg, "text") @@ -36,15 +36,15 @@ test_that("geom_text splits along colour", { L <- gg2list(gg) - expect_equal(length(L), 3) # 2 traces + layout + expect_equal(length(L$data), 2) # 2 traces # Proper type and mode conversion - expect_identical(L[[1]]$type, "scatter") - expect_identical(L[[1]]$mode, "text") - expect_identical(L[[2]]$type, "scatter") - expect_identical(L[[2]]$mode, "text") + expect_identical(L$data[[1]]$type, "scatter") + expect_identical(L$data[[1]]$mode, "text") + expect_identical(L$data[[2]]$type, "scatter") + expect_identical(L$data[[2]]$mode, "text") # Right colour for each trace - expect_identical(L[[1]]$textfont$color, "#F8766D") - expect_identical(L[[2]]$textfont$color, "#00BFC4") + expect_identical(L$data[[1]]$textfont$color, "#F8766D") + expect_identical(L$data[[2]]$textfont$color, "#00BFC4") save_outputs(gg, "text-colour") }) diff --git a/tests/testthat/test-ggplot-theme.R b/tests/testthat/test-ggplot-theme.R index f0f5692607..64fe04ca18 100644 --- a/tests/testthat/test-ggplot-theme.R +++ b/tests/testthat/test-ggplot-theme.R @@ -8,7 +8,7 @@ test_that("background translated correctly",{ ggiris <- iris.base + theme(panel.background=element_rect(fill="blue")) + theme(plot.background=element_rect(fill="green")) info <- gg2list(ggiris) - L <- info$kwargs$layout + L <- info$layout expect_identical(L$plot_bgcolor, toRGB("blue")) expect_identical(L$paper_bgcolor, toRGB("green")) @@ -20,7 +20,7 @@ test_that("grid/ticks translated correctly",{ theme(panel.grid.major=element_line(colour="violet")) info <- gg2list(ggiris) for (xy in c("x", "y")) { - ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] + ax.list <- info$layout[[paste0(xy, "axis")]] expect_identical(ax.list$tickcolor, toRGB("red")) expect_identical(ax.list$gridcolor, toRGB("violet")) } @@ -32,7 +32,7 @@ test_that("show ticks as 'outside' by default", { ggiris <- iris.base info <- gg2list(ggiris) for (xy in c("x", "y")) { - ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] + ax.list <- info$layout[[paste0(xy, "axis")]] expect_identical(ax.list$ticks, "outside") } @@ -43,7 +43,7 @@ test_that("do not show zeroline by default", { ggiris <- iris.base info <- gg2list(ggiris) for (xy in c("x", "y")) { - ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] + ax.list <- info$layout[[paste0(xy, "axis")]] expect_identical(ax.list$zeroline, FALSE) } @@ -54,7 +54,7 @@ test_that("dotted/dashed grid translated as line with alpha=0.1",{ ggiris <- iris.base + theme(panel.grid.major=element_line(linetype="dashed")) info <- gg2list(ggiris) for (xy in c("x", "y")) { - ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] + ax.list <- info$layout[[paste0(xy, "axis")]] expect_identical(ax.list$gridcolor, toRGB("white", 0.1)) } @@ -71,8 +71,8 @@ gg <- ggplot(countrypop) + test_that("marker default shape is a circle", { info <- gg2list(gg) for (i in c(1:3)) { - expect_identical(info[[i]]$marker$symbol, "circle") - expect_true(info[[i]]$showlegend) + expect_identical(info$data[[i]]$marker$symbol, "circle") + expect_true(info$data[[i]]$showlegend) } save_outputs(gg, "theme-marker-default") @@ -82,19 +82,19 @@ test_that("plot panel border is translated correctly", { ggiris <- iris.base + theme_grey() # has no panel.border info <- gg2list(ggiris) for (xy in c("x", "y")) { - ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] + ax.list <- info$layout[[paste0(xy, "axis")]] expect_identical(ax.list$showline, FALSE) } save_outputs(ggiris, "theme-panel-border-1") red <- ggplot(iris) + - theme_grey()+ + theme_grey() + geom_point(aes(Petal.Width, Sepal.Width)) + theme(panel.border=element_rect(colour="red", fill=NA)) info <- gg2list(red) for (xy in c("x", "y")) { - ax.list <- info$kwargs$layout[[paste0(xy, "axis")]] + ax.list <- info$layout[[paste0(xy, "axis")]] expect_identical(ax.list$showline, TRUE) expect_identical(ax.list$linecolor, toRGB("red")) } diff --git a/tests/testthat/test-ggplot-ticks.R b/tests/testthat/test-ggplot-ticks.R index 1383af37d8..b40bcfbe08 100644 --- a/tests/testthat/test-ggplot-ticks.R +++ b/tests/testthat/test-ggplot-ticks.R @@ -1,7 +1,7 @@ context("ggplot ticks") PlantGrowth$type <- - ifelse(PlantGrowth$group=="ctrl", "control", "treatment") + ifelse(PlantGrowth$group == "ctrl", "control", "treatment") boxes <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot() expect_traces <- function(gg, n.traces, name){ @@ -9,14 +9,13 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("ticks-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } plant.list <- split(PlantGrowth, PlantGrowth$group) @@ -216,7 +215,7 @@ no.breaks <- boxes + scale_x_discrete(breaks=NULL) test_that("hide x ticks, lines, and labels", { info <- expect_traces(no.breaks, 3, "hide-ticks-lines-labels") - x <- info$kwargs$layout$xaxis + x <- info$layout$xaxis expect_identical(x[["showticklabels"]], FALSE) ##expect_identical(x[["showline"]], FALSE) #irrelevant. expect_identical(x[["showgrid"]], FALSE) @@ -241,7 +240,7 @@ test_that("Hide X ticks and labels, but keep the gridlines", { boxes.grid <- boxes + theme(axis.ticks = element_blank(), axis.text.x = element_blank()) info <- expect_traces(boxes.grid, 3, "hide-ticks-labels") - x <- info$kwargs$layout$xaxis + x <- info$layout$xaxis expect_identical(x[["showticklabels"]], FALSE) expect_identical(x[["showgrid"]], TRUE) expect_identical(x[["ticks"]], "") @@ -250,14 +249,14 @@ test_that("Hide X ticks and labels, but keep the gridlines", { test_that("scale_y_continuous(limits) means yaxis$ranges", { boxes.range <- boxes + scale_y_continuous(limits=c(0,8)) info <- expect_traces(boxes.range, 3, "ycontinuous-ranges") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_equal(y.axis$range, c(0, 8)) }) test_that("ylim() means yaxis$ranges", { boxes.range <- boxes + ylim(0,8) info <- expect_traces(boxes.range, 3, "ylim-ranges") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_equal(y.axis$range, c(0, 8)) ## ensure correct positive values without reverse scale. for(tr in info$traces){ @@ -271,7 +270,7 @@ test_that("ylim() means yaxis$ranges", { test_that("scale_y_reverse() -> yaxis$ranges reversed", { boxes.reverse <- boxes + scale_y_reverse() info <- expect_traces(boxes.reverse, 3, "yreverse-ranges") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_that(y.axis$range[2], is_less_than(y.axis$range[1])) ## ensure correct positive values, despite the reverse scale. for(tr in info$traces){ @@ -286,7 +285,7 @@ test_that("scale_y_reverse(limits) -> yaxis$ranges reversed", { y.lim <- c(10, -2) boxes.reverse <- boxes + scale_y_reverse(limits=y.lim) info <- expect_traces(boxes.reverse, 3, "yreverse-limits-ranges") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_equal(y.axis$range, y.lim) ## ensure correct positive values, despite the reverse scale. for(tr in info$traces){ @@ -300,7 +299,7 @@ test_that("scale_y_reverse(limits) -> yaxis$ranges reversed", { test_that("ylim(reversed) -> yaxis$ranges reversed", { boxes.reverse <- boxes + ylim(7.5, -1) info <- expect_traces(boxes.reverse, 3, "ylim-reversed-ranges") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_equal(y.axis$range, c(7.5, -1)) ## ensure correct positive values, despite the reverse scale. for(tr in info$traces){ @@ -316,7 +315,7 @@ test_that("Set the X tick mark locations", { ## show only the ones that are within range (3.50-6.25 in this case) boxes.ticks <- boxes + scale_y_continuous(breaks=seq(1,10,1/4)) info <- expect_traces(boxes.ticks, 3, "evenly-spaced-ticks") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_equal(y.axis$dtick, 0.25) expect_identical(y.axis$autotick, FALSE) }) @@ -331,7 +330,7 @@ test_that("The breaks can be spaced unevenly", { test_that("hide y ticks, lines, and labels", { no.breaks <- boxes + scale_y_continuous(breaks=NULL) info <- expect_traces(no.breaks, 3, "hide-y") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_identical(y.axis[["showgrid"]], FALSE) expect_identical(y.axis[["ticks"]], "") expect_identical(y.axis[["showticklabels"]], FALSE) @@ -341,7 +340,7 @@ test_that("hide y ticks and labels, but keep the gridlines", { boxes.ygrid <- boxes + theme(axis.ticks = element_blank(), axis.text.y = element_blank()) info <- expect_traces(boxes.ygrid, 3, "hide-y-keep-grid") - y.axis <- info$kwargs$layout$yaxis + y.axis <- info$layout$yaxis expect_identical(y.axis[["showgrid"]], TRUE) expect_identical(y.axis[["ticks"]], "") expect_identical(y.axis[["showticklabels"]], FALSE) diff --git a/tests/testthat/test-ggplot-vline.R b/tests/testthat/test-ggplot-vline.R index 94f2288ed5..a2bee6d5a0 100644 --- a/tests/testthat/test-ggplot-vline.R +++ b/tests/testthat/test-ggplot-vline.R @@ -11,14 +11,14 @@ test_that("second trace be the vline", { L <- gg2list(gg) - expect_equal(length(L), 3) - expect_equal(L[[2]]$x[1], 1.1) - expect_true(L[[2]]$y[1] <= 0) - expect_true(L[[2]]$y[2] >= 3.325) - expect_identical(L[[2]]$mode, "lines") - expect_identical(L[[2]]$line$shape, "linear") - expect_equal(L[[2]]$line$width, 6) - expect_identical(L[[2]]$line$color, "rgb(0,255,0)") + expect_equal(length(L$data), 2) + expect_equal(L$data[[2]]$x[1], 1.1) + expect_true(L$data[[2]]$y[1] <= 0) + expect_true(L$data[[2]]$y[2] >= 3.325) + expect_identical(L$data[[2]]$mode, "lines") + expect_identical(L$data[[2]]$line$shape, "linear") + expect_equal(L$data[[2]]$line$width, 6) + expect_identical(L$data[[2]]$line$color, "rgb(0,255,0)") save_outputs(gg, "vline") }) @@ -28,15 +28,15 @@ test_that("vector xintercept results in multiple vertical lines", { L <- gg2list(gg) - expect_equal(length(L), 4) - expect_equal(L[[2]]$x[1], 1) - expect_equal(L[[3]]$x[1], 2) - expect_true(L[[3]]$y[1] <= 0) - expect_true(L[[3]]$y[2] >= 3.325) - expect_identical(L[[3]]$mode, "lines") - expect_identical(L[[3]]$line$shape, "linear") - expect_equal(L[[3]]$line$width, 6) - expect_identical(L[[3]]$line$color, "rgb(0,0,255)") + expect_equal(length(L$data), 3) + expect_equal(L$data[[2]]$x[1], 1) + expect_equal(L$data[[3]]$x[1], 2) + expect_true(L$data[[3]]$y[1] <= 0) + expect_true(L$data[[3]]$y[2] >= 3.325) + expect_identical(L$data[[3]]$mode, "lines") + expect_identical(L$data[[3]]$line$shape, "linear") + expect_equal(L$data[[3]]$line$width, 6) + expect_identical(L$data[[3]]$line$color, "rgb(0,0,255)") save_outputs(gg, "vline-multiple") }) diff --git a/tests/testthat/test-ggplot-ylim.R b/tests/testthat/test-ggplot-ylim.R index 725787a621..e381b363f5 100644 --- a/tests/testthat/test-ggplot-ylim.R +++ b/tests/testthat/test-ggplot-ylim.R @@ -1,6 +1,6 @@ context("ggplot ylim") -## http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/ +# http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/ df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), total_bill = c(14.89, 17.23)) @@ -18,20 +18,19 @@ expect_traces <- function(gg, n.traces, name){ stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("ylim-", name)) L <- gg2list(gg) - is.trace <- names(L) == "" - all.traces <- L[is.trace] + all.traces <- L$data no.data <- sapply(all.traces, function(tr) { is.null(tr[["x"]]) && is.null(tr[["y"]]) }) has.data <- all.traces[!no.data] expect_equal(length(has.data), n.traces) - list(traces=has.data, kwargs=L$kwargs) + list(traces=has.data, layout=L$layout) } test_that("ylim is respected for 1 trace", { info <- expect_traces(gg.ylim, 1, "one-trace") expected.ylim <- c(0, max(df$total_bill)) - expect_equal(info$kwargs$layout$yaxis$range, expected.ylim) + expect_equal(info$layout$yaxis$range, expected.ylim) expect_identical(info$traces[[1]]$showlegend, FALSE) }) diff --git a/tests/testthat/test-mean-error-bars.R b/tests/testthat/test-mean-error-bars.R index 45dff776ae..3352adc522 100644 --- a/tests/testthat/test-mean-error-bars.R +++ b/tests/testthat/test-mean-error-bars.R @@ -23,11 +23,10 @@ none.json <- list( ) test_that("only asymmetric error bars", { - error.gg <- ggplot(one.line.df, aes(x, y))+ + error.gg <- ggplot(one.line.df, aes(x, y)) + geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array)) generated.json <- gg2list(error.gg) - is.trace <- names(generated.json) == "" - traces <- generated.json[is.trace] + traces <- generated.json$data expect_identical(length(traces), 1L) tr <- traces[[1]] expect_identical(tr$mode, "none") @@ -54,16 +53,15 @@ one.line.json <- list( ) test_that("asymmetric error bars, geom_errorbar last", { - one.line.gg <- ggplot(one.line.df, aes(x, y))+ - geom_line()+ - geom_point()+ + one.line.gg <- ggplot(one.line.df, aes(x, y)) + + geom_line() + + geom_point() + geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array)) generated.json <- gg2list(one.line.gg) ## when there is 1 trace with error bars, lines, and markers, plotly ## shows error bars in the background, lines in the middle and ## markers in front. - is.trace <- names(generated.json) == "" - traces <- generated.json[is.trace] + traces <- generated.json$data expect_identical(length(traces), 1L) tr <- traces[[1]] expect_identical(tr$mode, "lines+markers") @@ -76,13 +74,12 @@ test_that("asymmetric error bars, geom_errorbar last", { }) test_that("asymmetric error bars, geom_errorbar first", { - one.line.gg <- ggplot(one.line.df, aes(x, y))+ - geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array))+ - geom_line()+ + one.line.gg <- ggplot(one.line.df, aes(x, y)) + + geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array)) + + geom_line() + geom_point() generated.json <- gg2list(one.line.gg) - is.trace <- names(generated.json) == "" - traces <- generated.json[is.trace] + traces <- generated.json$data expect_identical(length(traces), 1L) tr <- traces[[1]] expect_identical(tr$mode, "lines+markers") @@ -112,13 +109,12 @@ colors.json <- list( ) test_that("different colors for error bars, points, and lines", { - one.line.gg <- ggplot(one.line.df, aes(x, y))+ - geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array), color="red")+ - geom_line(color="violet")+ + one.line.gg <- ggplot(one.line.df, aes(x, y)) + + geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array), color="red") + + geom_line(color="violet") + geom_point(color="blue", size=14) generated.json <- gg2list(one.line.gg) - is.trace <- names(generated.json) == "" - traces <- generated.json[is.trace] + traces <- generated.json$data expect_identical(length(traces), 1L) tr <- traces[[1]] expect_identical(tr$mode, "lines+markers") @@ -179,11 +175,10 @@ test_that("errorbar(aes(color)) + other geoms", { geom_point() before.json <- gg2list(before) - is.trace <- names(before.json) == "" - traces <- before.json[is.trace] + traces <- before.json$data expect_identical(length(traces), 2L) - for(tr in traces){ + for(tr in traces) { expected.color <- toRGB(color.code[[tr$name]]) expected.data <- supp.list[[tr$name]] expect_identical(tr$mode, "lines+markers") @@ -204,12 +199,11 @@ test_that("other geoms + errorbar(aes(color))", { ggplot(dfc, aes(x=dose, y=len, colour=supp)) + geom_line() + geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1) + - geom_point()+ + geom_point() + scale_color_manual(values=color.code) after.json <- gg2list(after) - is.trace <- names(after.json) == "" - traces <- after.json[is.trace] + traces <- after.json$data expect_identical(length(traces), 2L) for(tr in traces){ @@ -227,5 +221,3 @@ test_that("other geoms + errorbar(aes(color))", { expect_equal(ey$array, expected.data$se) } }) - - From 3bddd5a028e45e241042a56dbf99b4c66861ad48 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 28 Apr 2015 16:21:45 -0400 Subject: [PATCH 5/7] Update docstring for gg2list() --- R/ggplotly.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/ggplotly.R b/R/ggplotly.R index 6778c7b3d8..02973ca2d1 100644 --- a/R/ggplotly.R +++ b/R/ggplotly.R @@ -70,9 +70,9 @@ markUnique <- as.character(unique(unlist(markLegends))) #' Convert a ggplot to a list. #' @import ggplot2 #' @param p ggplot2 plot. -#' @return list representing a ggplot. +#' @return figure object (list with names "data" and "layout"). #' @export -gg2list <- function(p){ +gg2list <- function(p) { if(length(p$layers) == 0) { stop("No layers in plot") } From f370a36a099424fb1fd92e90cd4981d4acfbf839 Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Tue, 28 Apr 2015 16:22:53 -0400 Subject: [PATCH 6/7] Run roxygen2::roxygenise() --- NAMESPACE | 2 +- man/ensure_file_exist.Rd | 2 +- man/get_config_file.Rd | 2 +- man/get_credentials_file.Rd | 2 +- man/gg2list.Rd | 4 ++-- man/ggplot_build2.Rd | 2 +- man/group2NA.Rd | 2 +- man/layer2traces.Rd | 2 +- man/paramORdefault.Rd | 2 +- man/plotly-package.Rd | 2 +- man/plotly.Rd | 2 +- man/set_config_file.Rd | 2 +- man/set_credentials_file.Rd | 2 +- man/show_config_file.Rd | 2 +- man/show_credentials_file.Rd | 2 +- man/signup.Rd | 2 +- man/toFill.Rd | 2 +- man/toRGB.Rd | 2 +- 18 files changed, 19 insertions(+), 19 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 50bca0d434..ef4d1bbd1d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,4 @@ -# Generated by roxygen2 (4.1.0): do not edit by hand +# Generated by roxygen2 (4.1.1): do not edit by hand export(gg2list) export(ggplot_build2) diff --git a/man/ensure_file_exist.Rd b/man/ensure_file_exist.Rd index ab1c577a2f..4b1586acbe 100644 --- a/man/ensure_file_exist.Rd +++ b/man/ensure_file_exist.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{ensure_file_exist} \alias{ensure_file_exist} diff --git a/man/get_config_file.Rd b/man/get_config_file.Rd index 55de20aa9e..1af0d1dcb1 100644 --- a/man/get_config_file.Rd +++ b/man/get_config_file.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{get_config_file} \alias{get_config_file} diff --git a/man/get_credentials_file.Rd b/man/get_credentials_file.Rd index 4c3178d002..b45712ccb4 100644 --- a/man/get_credentials_file.Rd +++ b/man/get_credentials_file.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{get_credentials_file} \alias{get_credentials_file} diff --git a/man/gg2list.Rd b/man/gg2list.Rd index 67016d2a0d..b2bd08b50e 100644 --- a/man/gg2list.Rd +++ b/man/gg2list.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/ggplotly.R \name{gg2list} \alias{gg2list} @@ -10,7 +10,7 @@ gg2list(p) \item{p}{ggplot2 plot.} } \value{ -list representing a ggplot. +figure object (list with names "data" and "layout"). } \description{ Convert a ggplot to a list. diff --git a/man/ggplot_build2.Rd b/man/ggplot_build2.Rd index a37efa75c4..5b1d6eba63 100644 --- a/man/ggplot_build2.Rd +++ b/man/ggplot_build2.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/build_function.R \name{ggplot_build2} \alias{ggplot_build2} diff --git a/man/group2NA.Rd b/man/group2NA.Rd index c1114f4c35..3685dd7aad 100644 --- a/man/group2NA.Rd +++ b/man/group2NA.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/trace_generation.R \name{group2NA} \alias{group2NA} diff --git a/man/layer2traces.Rd b/man/layer2traces.Rd index d57083fe5b..ce362768d5 100644 --- a/man/layer2traces.Rd +++ b/man/layer2traces.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/trace_generation.R \name{layer2traces} \alias{layer2traces} diff --git a/man/paramORdefault.Rd b/man/paramORdefault.Rd index c0cb1cb457..8d56947364 100644 --- a/man/paramORdefault.Rd +++ b/man/paramORdefault.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/marker_conversion.R \name{paramORdefault} \alias{paramORdefault} diff --git a/man/plotly-package.Rd b/man/plotly-package.Rd index 2643bf2dd2..208b5fe705 100644 --- a/man/plotly-package.Rd +++ b/man/plotly-package.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/plotly-package.r \docType{package} \name{plotly-package} diff --git a/man/plotly.Rd b/man/plotly.Rd index 0c4db5bd35..055b425439 100644 --- a/man/plotly.Rd +++ b/man/plotly.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/plotly.R \name{plotly} \alias{plotly} diff --git a/man/set_config_file.Rd b/man/set_config_file.Rd index d37aaf672b..7fef7dc742 100644 --- a/man/set_config_file.Rd +++ b/man/set_config_file.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{set_config_file} \alias{set_config_file} diff --git a/man/set_credentials_file.Rd b/man/set_credentials_file.Rd index b5145446c8..2830ee586c 100644 --- a/man/set_credentials_file.Rd +++ b/man/set_credentials_file.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{set_credentials_file} \alias{set_credentials_file} diff --git a/man/show_config_file.Rd b/man/show_config_file.Rd index 1f781075f7..5b7f337b81 100644 --- a/man/show_config_file.Rd +++ b/man/show_config_file.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{show_config_file} \alias{show_config_file} diff --git a/man/show_credentials_file.Rd b/man/show_credentials_file.Rd index 70a9bda657..467be630f9 100644 --- a/man/show_credentials_file.Rd +++ b/man/show_credentials_file.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/tools.R \name{show_credentials_file} \alias{show_credentials_file} diff --git a/man/signup.Rd b/man/signup.Rd index 577d1d1c5d..aec5165512 100644 --- a/man/signup.Rd +++ b/man/signup.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/signup.R \name{signup} \alias{signup} diff --git a/man/toFill.Rd b/man/toFill.Rd index 05e3f49dff..ac0db8ceb8 100644 --- a/man/toFill.Rd +++ b/man/toFill.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/colour_conversion.R \name{toFill} \alias{toFill} diff --git a/man/toRGB.Rd b/man/toRGB.Rd index 5e75af9c0b..252b654777 100644 --- a/man/toRGB.Rd +++ b/man/toRGB.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0): do not edit by hand +% Generated by roxygen2 (4.1.1): do not edit by hand % Please edit documentation in R/colour_conversion.R \name{toRGB} \alias{toRGB} From aff299724bcef81fa7a8a9cb163e4254fa36906b Mon Sep 17 00:00:00 2001 From: Marianne Corvellec Date: Mon, 4 May 2015 16:53:09 -0400 Subject: [PATCH 7/7] Update version number to 0.5.30 --- DESCRIPTION | 2 +- NEWS | 4 ++++ R/plotly-package.r | 2 +- R/plotly.R | 2 +- man/plotly-package.Rd | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8ed1f20701..f5215cf99f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: plotly Type: Package Title: Interactive, publication-quality graphs online. -Version: 0.5.29 +Version: 0.5.30 Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"), email = "chris@plot.ly"), person("Scott", "Chamberlain", role = "aut", diff --git a/NEWS b/NEWS index 1509920b1a..6145218eea 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.5.30 -- 4 May 2015 + +Let gg2list() return a figure object. + 0.5.29 -- 16 April 2015 geom_density() as filled area chart #202 diff --git a/R/plotly-package.r b/R/plotly-package.r index 96ec60eb3f..e4a4eabd88 100644 --- a/R/plotly-package.r +++ b/R/plotly-package.r @@ -7,7 +7,7 @@ #' \itemize{ #' \item Package: plotly #' \item Type: Package -#' \item Version: 0.5.20 +#' \item Version: 0.5.30 #' \item Date: 2014-03-07 #' \item License: MIT #' } diff --git a/R/plotly.R b/R/plotly.R index fc84463752..8cf23792a7 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -82,7 +82,7 @@ For more help, see https://plot.ly/R or contact .") # public attributes/methods that the user has access to pub <- list(username=username, key=key, filename="from api", fileopt=NULL, - version="0.5.20") + version="0.5.30") priv <- list() pub$makecall <- function(args, kwargs, origin) { diff --git a/man/plotly-package.Rd b/man/plotly-package.Rd index 208b5fe705..8842b30d47 100644 --- a/man/plotly-package.Rd +++ b/man/plotly-package.Rd @@ -15,7 +15,7 @@ An example of an interactive graph made from the R API: https://plot.ly/~chris/4 \itemize{ \item Package: plotly \item Type: Package - \item Version: 0.5.20 + \item Version: 0.5.30 \item Date: 2014-03-07 \item License: MIT }