Skip to content

Translate \n to <br /> in fct labels, too #1700

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 11 commits into from
Jun 22, 2020
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ 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("Plotly Technologies Inc.", role = "cph"))
person("Plotly Technologies Inc.", role = "cph"),
person("Salim", "Brüggemann", role = "ctb",
email = "[email protected]", comment = c(ORCID = "0000-0002-5329-5987")))
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.
URL: https://plotly-r.com, https://github.com/ropensci/plotly#readme, https://plot.ly/r
Expand Down
4 changes: 3 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
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
35 changes: 35 additions & 0 deletions tests/testthat/test-plotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,38 @@ 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)", {
skip_if_not_installed(pkg = "forcats")

# create target labels
suffix <- "\n\n(third line)\n(fourth line)"

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

# test factor column
d <- iris %>% dplyr::mutate(Species = forcats::fct_relabel(Species,
paste0,
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)
})