Skip to content

Commit 8324fae

Browse files
pull[bot]trekonomcpsievertsalim-b
authored
[pull] master from ropensci:master (#4)
* Fix bug in verify_attr. Closes issue plotly#1720. * Add test to check that ggplotly does not break discrete x-axis in facet panels with only one category. * Update tests/testthat/test-facet-axis.R Co-authored-by: Carson Sievert <cpsievert1@gmail.com> * Add news item. * Translate `\n` to `<br />` in fct labels, too (plotly#1700) * Translate `\n` to `<br />` in fct labels, too Before, this only worked for colums of type character. * add @salim-b as ctb * Add test for `translate_linebreaks()` * remove superfluous char conversion * change order of authors * replace forcats code with base R * add news item Co-authored-by: Stefan Moog <moogs@gmx.de> Co-authored-by: Carson Sievert <cpsievert1@gmail.com> Co-authored-by: Salim B <salim@posteo.de>
1 parent 657a7bb commit 8324fae

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
1515
email = "marianne.corvellec@igdore.org", comment = c(ORCID = "0000-0002-1994-3581")),
1616
person("Pedro", "Despouy", role = "aut",
1717
email = "pedro@plot.ly"),
18+
person("Salim", "Brüggemann", role = "ctb",
19+
email = "salim-b@pm.me", comment = c(ORCID = "0000-0002-5329-5987")),
1820
person("Plotly Technologies Inc.", role = "cph"))
1921
License: MIT + file LICENSE
2022
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.

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ This is minor patch release with a few minor bug fixes and updates test expectat
4646

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

49+
* 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)
50+
4951
## BUG FIXES
5052

53+
* `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).
54+
5155
* `ggplotly()` now handles `element_blank()` and `factor()` labels in positional scales correctly (#1731 and #1772).
5256

5357
# 4.9.2.1

R/utils.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ verify_attr <- function(proposed, schema, layoutAttr = FALSE) {
521521

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

@@ -609,12 +609,14 @@ translate_linebreaks <- function(p) {
609609
typ <- typeof(a)
610610
if (typ == "list") {
611611
# retain the class of list elements
612-
# which important for many things, such as colorbars
612+
# which is important for many things, such as colorbars
613613
a[] <- lapply(a, recurse)
614614
} else if (typ == "character" && !inherits(a, "JS_EVAL")) {
615615
attrs <- attributes(a)
616616
a <- gsub("\n", br(), a, fixed = TRUE)
617617
attributes(a) <- attrs
618+
} else if (is.factor(a)) {
619+
levels(a) <- gsub("\n", br(), levels(a), fixed = TRUE)
618620
}
619621
a
620622
}

tests/testthat/test-facet-axis.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
test_that("ggplotly does not break discrete x-axis with facet_yyyy in panels > 1 with only one category", {
2+
d <- data.frame(cat = c("A", "A", "A"), pan = paste("Panel", c(1, 2, 2)))
3+
gp <- ggplot(d, aes(cat)) +
4+
geom_bar() +
5+
facet_wrap(~pan)
6+
L <- plotly_build(ggplotly(gp))
7+
# tickvals, ticktext and categoryarray have class 'AsIs'
8+
lapply(L$x$layout[c("xaxis", "xaxis2")], function(axis) {
9+
expect_s3_class(axis$tickvals, "AsIs")
10+
expect_s3_class(axis$ticktext, "AsIs")
11+
expect_true(axis$ticktext == "A")
12+
expect_s3_class(axis$categoryarray, "AsIs")
13+
expect_true(axis$categoryarray == "A")
14+
})
15+
})

tests/testthat/test-plotly.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,34 @@ test_that("toWebGL() shouldn't complain if it's already webgl", {
317317
toWebGL()
318318
expect_silent(plotly_build(p))
319319
})
320+
321+
test_that("Line breaks are properly translated (R -> HTML)", {
322+
# create target labels
323+
suffix <- "\n\n(third line)\n(fourth line)"
324+
325+
target_labels <- iris$Species %>%
326+
unique() %>%
327+
paste0(suffix) %>%
328+
gsub(pattern = "\n",
329+
replacement = br(),
330+
x = .,
331+
fixed = TRUE)
332+
333+
# test factor column
334+
d <- iris
335+
levels(d$Species) <- paste0(levels(d$Species), suffix)
336+
p1 <- d %>% plot_ly(x = ~Sepal.Length,
337+
y = ~Species)
338+
339+
expect_equivalent(plotly_build(p1)[["x"]][["layout"]][["yaxis"]][["categoryarray"]],
340+
target_labels)
341+
342+
# test character column
343+
p2 <- d %>%
344+
dplyr::mutate(Species = as.character(Species)) %>%
345+
plot_ly(x = ~Sepal.Length,
346+
y = ~Species)
347+
348+
expect_equivalent(plotly_build(p2)[["x"]][["layout"]][["yaxis"]][["categoryarray"]],
349+
target_labels)
350+
})

0 commit comments

Comments
 (0)