Skip to content

Let gg2list return a figure object #210

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

Merged
merged 8 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"),
person("Scott", "Chamberlain", role = "aut",
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion R/plotly-package.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
#' }
Expand Down
12 changes: 8 additions & 4 deletions R/plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")

# 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) {
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,
Expand Down Expand Up @@ -139,11 +139,15 @@ For more help, see https://plot.ly/R or contact <[email protected]>.")
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) {
Expand Down
2 changes: 1 addition & 1 deletion man/ensure_file_exist.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/get_config_file.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/get_credentials_file.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
4 changes: 2 additions & 2 deletions man/gg2list.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion man/ggplot_build2.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/group2NA.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/layer2traces.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/paramORdefault.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
4 changes: 2 additions & 2 deletions man/plotly-package.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion man/plotly.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/set_config_file.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/set_credentials_file.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/show_config_file.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/show_credentials_file.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/signup.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/toFill.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
2 changes: 1 addition & 1 deletion man/toRGB.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
11 changes: 5 additions & 6 deletions tests/testthat/test-cookbook-axes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-ggplot-abline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
14 changes: 7 additions & 7 deletions tests/testthat/test-ggplot-area.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Loading