Skip to content

Commit 18f592e

Browse files
committed
Merge branch 'baobao-fix_exists' of https://github.com/ropensci/plotly into baobao-fix_exists
2 parents 69a118a + 6730d74 commit 18f592e

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

R/ggplotly.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ gg2list <- function(p) {
659659

660660
# Legend.
661661
layout$margin$r <- 10
662-
if (exists("increase_margin_r")) {
662+
if (exists("increase_margin_r", where = as.environment("package:plotly"))) {
663663
layout$margin$r <- 60
664664
}
665665
layout$legend <- list(bordercolor="transparent",
@@ -700,11 +700,10 @@ gg2list <- function(p) {
700700
legend.title <- colnames(p$data)[i]
701701
}
702702
legend.title <- paste0("<b>", legend.title, "</b>")
703-
704703
# Create legend title element as an annotation
705-
if (exists("annotations")) {
706-
nann <- nann + 1
707-
} else {
704+
if (exists("annotations", where = as.environment("package:plotly"))) {
705+
nann <- nann + 1
706+
} else{
708707
annotations <- list()
709708
nann <- 1
710709
}

R/trace_generation.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ layer2traces <- function(l, d, misc) {
250250

251251
# special handling for bars
252252
if (g$geom == "bar") {
253-
tr$bargap <- if (exists("bargap")) bargap else "default"
253+
tr$bargap <- if (exists("bargap", where = environment())) bargap else "default"
254254
pos <- l$position$.super$objname
255255
tr$barmode <-
256256
if (pos %in% "identity" && tr$bargap == 0) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
context("Objects and Environments")
2+
3+
# Expect trace function
4+
expect_traces <- function(gg, n_traces, name) {
5+
stopifnot(is.ggplot(gg))
6+
stopifnot(is.numeric(n_traces))
7+
save_outputs(gg, paste0("object_environments-", name))
8+
L <- gg2list(gg)
9+
all_traces <- L$data
10+
no_data <- sapply(all_traces, function(tr) {
11+
is.null(tr[["x"]]) && is.null(tr[["y"]])
12+
})
13+
has_data <- all_traces[!no_data]
14+
expect_equal(length(has_data), n_traces)
15+
list(traces = has_data, layout = L$layout)
16+
}
17+
18+
# make data
19+
set.seed(955)
20+
dat <- data.frame(cond = rep(c("A", "B"), each=10),
21+
xvar = 1:20 + rnorm(20,sd=3),
22+
yvar = 1:20 + rnorm(20,sd=3))
23+
24+
# make ggplot
25+
p <- ggplot(dat, aes(x = xvar, y = yvar, color = cond)) +
26+
geom_point() + xlab("X") + ylab("Y")
27+
28+
# Test 1: annotation
29+
test_that("object annotations in environment outside plotly", {
30+
annotations <- "outside of the plotly environment"
31+
info <- expect_traces(p, 2, "annotations")
32+
tr <- info$traces[[1]]
33+
la <- info$layout
34+
expect_identical(tr$type, "scatter")
35+
expect_identical(la$xaxis$title, "X")
36+
expect_identical(la$yaxis$title, "Y")
37+
expect_true(grepl("cond", la$annotations[[1]]$text))
38+
})
39+
40+
# Test 2: increase_margin_r
41+
test_that("object increase_margin_r in environment outside plotly", {
42+
increase_margin_r <- "outside of the plotly environment"
43+
info <- expect_traces(p, 2, "increase_margin_r")
44+
tr <- info$traces[[1]]
45+
la <- info$layout
46+
expect_identical(la$xaxis$title, "X")
47+
expect_identical(la$yaxis$title, "Y")
48+
expect_identical(tr$type, "scatter")
49+
expect_true(grepl("cond", la$annotations[[1]]$text))
50+
})
51+
52+
# Test 3: bargap
53+
test_that("object bargap in environment outside plotly", {
54+
bargap <- "outside of the plotly environment"
55+
info <- expect_traces(p, 2, "bargap")
56+
tr <- info$traces[[1]]
57+
la <- info$layout
58+
expect_identical(la$xaxis$title, "X")
59+
expect_identical(la$yaxis$title, "Y")
60+
expect_identical(tr$type, "scatter")
61+
expect_true(grepl("cond", la$annotations[[1]]$text))
62+
})
63+

0 commit comments

Comments
 (0)