Skip to content

[pull] master from ropensci:master #4

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 6 commits into from
Jun 22, 2020
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: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
email = "[email protected]", comment = c(ORCID = "0000-0002-1994-3581")),
person("Pedro", "Despouy", role = "aut",
email = "[email protected]"),
person("Salim", "Brüggemann", role = "ctb",
email = "[email protected]", comment = c(ORCID = "0000-0002-5329-5987")),
person("Plotly Technologies Inc.", role = "cph"))
License: MIT + file LICENSE
Description: Create interactive web graphics from 'ggplot2' graphs and/or a custom interface to the (MIT-licensed) JavaScript library 'plotly.js' inspired by the grammar of graphics.
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

* All HTTP requests are now retried upon failure (#1656)

* R linebreaks (`\n`) in _factor labels_ are now translated to HTML linebreaks (`<br />`), too. Before, this conversion was only done for colums of type character. ([#1700](https://github.com/ropensci/plotly/pull/1700), @salim-b)

## BUG FIXES

* `ggplotly()` now handles discrete axes of a `facet_wrap` and `facet_grid` correctly when there is only one category in panels > 1 (#1577 and #1720).

* `ggplotly()` now handles `element_blank()` and `factor()` labels in positional scales correctly (#1731 and #1772).

# 4.9.2.1
Expand Down
6 changes: 4 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ verify_attr <- function(proposed, schema, layoutAttr = FALSE) {

# do the same for "sub-attributes"
if (identical(role, "object") && is.recursive(proposed[[attr]])) {
proposed[[attr]] <- verify_attr(proposed[[attr]], schema[[attr]], layoutAttr = layoutAttr)
proposed[[attr]] <- verify_attr(proposed[[attr]], attrSchema, layoutAttr = layoutAttr)
}
}

Expand Down Expand Up @@ -609,12 +609,14 @@ translate_linebreaks <- function(p) {
typ <- typeof(a)
if (typ == "list") {
# retain the class of list elements
# which important for many things, such as colorbars
# which is important for many things, such as colorbars
a[] <- lapply(a, recurse)
} else if (typ == "character" && !inherits(a, "JS_EVAL")) {
attrs <- attributes(a)
a <- gsub("\n", br(), a, fixed = TRUE)
attributes(a) <- attrs
} else if (is.factor(a)) {
levels(a) <- gsub("\n", br(), levels(a), fixed = TRUE)
}
a
}
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-facet-axis.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("ggplotly does not break discrete x-axis with facet_yyyy in panels > 1 with only one category", {
d <- data.frame(cat = c("A", "A", "A"), pan = paste("Panel", c(1, 2, 2)))
gp <- ggplot(d, aes(cat)) +
geom_bar() +
facet_wrap(~pan)
L <- plotly_build(ggplotly(gp))
# tickvals, ticktext and categoryarray have class 'AsIs'
lapply(L$x$layout[c("xaxis", "xaxis2")], function(axis) {
expect_s3_class(axis$tickvals, "AsIs")
expect_s3_class(axis$ticktext, "AsIs")
expect_true(axis$ticktext == "A")
expect_s3_class(axis$categoryarray, "AsIs")
expect_true(axis$categoryarray == "A")
})
})
31 changes: 31 additions & 0 deletions tests/testthat/test-plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,34 @@ test_that("toWebGL() shouldn't complain if it's already webgl", {
toWebGL()
expect_silent(plotly_build(p))
})

test_that("Line breaks are properly translated (R -> HTML)", {
# create target labels
suffix <- "\n\n(third line)\n(fourth line)"

target_labels <- iris$Species %>%
unique() %>%
paste0(suffix) %>%
gsub(pattern = "\n",
replacement = br(),
x = .,
fixed = TRUE)

# test factor column
d <- iris
levels(d$Species) <- paste0(levels(d$Species), suffix)
p1 <- d %>% plot_ly(x = ~Sepal.Length,
y = ~Species)

expect_equivalent(plotly_build(p1)[["x"]][["layout"]][["yaxis"]][["categoryarray"]],
target_labels)

# test character column
p2 <- d %>%
dplyr::mutate(Species = as.character(Species)) %>%
plot_ly(x = ~Sepal.Length,
y = ~Species)

expect_equivalent(plotly_build(p2)[["x"]][["layout"]][["yaxis"]][["categoryarray"]],
target_labels)
})