From b9ab1e44ee7155520b9e96c9b10088b1a6821018 Mon Sep 17 00:00:00 2001 From: Salim B Date: Tue, 11 Feb 2020 19:58:36 +0100 Subject: [PATCH 1/7] Translate `\n` to `
` in fct labels, too Before, this only worked for colums of type character. --- R/utils.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index cc3395d4ac..8224f4ca62 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 } From 437b0e85f3104f56550366a3a3e9c12ffe9bd6ef Mon Sep 17 00:00:00 2001 From: Salim B Date: Tue, 11 Feb 2020 20:20:32 +0100 Subject: [PATCH 2/7] add @salim-b as ctb --- DESCRIPTION | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 499d55f0a3..df632e2e3e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,7 +15,9 @@ Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"), email = "marianne.corvellec@igdore.org", comment = c(ORCID = "0000-0002-1994-3581")), person("Pedro", "Despouy", role = "aut", email = "pedro@plot.ly"), - person("Plotly Technologies Inc.", role = "cph")) + person("Plotly Technologies Inc.", role = "cph"), + person("Salim", "Brüggemann", role = "ctb", + email = "salim-b@pm.me", 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 From 841e7e9731b81b6d1d0f73182ac7951845220a41 Mon Sep 17 00:00:00 2001 From: Salim B Date: Thu, 23 Apr 2020 23:13:29 +0200 Subject: [PATCH 3/7] Add test for `translate_linebreaks()` --- tests/testthat/test-plotly.R | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/testthat/test-plotly.R b/tests/testthat/test-plotly.R index 8dd43a405a..d6b5fbead1 100644 --- a/tests/testthat/test-plotly.R +++ b/tests/testthat/test-plotly.R @@ -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) +}) From 53c9ef8d115dd1adc6f4097099686f3904c7debc Mon Sep 17 00:00:00 2001 From: Salim B Date: Thu, 21 May 2020 03:59:00 +0200 Subject: [PATCH 4/7] remove superfluous char conversion --- tests/testthat/test-plotly.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-plotly.R b/tests/testthat/test-plotly.R index d6b5fbead1..f9c52f9f35 100644 --- a/tests/testthat/test-plotly.R +++ b/tests/testthat/test-plotly.R @@ -326,7 +326,6 @@ test_that("Line breaks are properly translated (R -> HTML)", { target_labels <- iris$Species %>% unique() %>% - as.character() %>% paste0(suffix) %>% gsub(pattern = "\n", replacement = br(), From 57f6f9b3a0d3bd05beb6ff5c7d0c2b276fa46c98 Mon Sep 17 00:00:00 2001 From: Salim B Date: Thu, 21 May 2020 04:00:29 +0200 Subject: [PATCH 5/7] change order of authors --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8b3450cae5..401f048cf1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,9 +15,9 @@ Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"), email = "marianne.corvellec@igdore.org", comment = c(ORCID = "0000-0002-1994-3581")), person("Pedro", "Despouy", role = "aut", email = "pedro@plot.ly"), - person("Plotly Technologies Inc.", role = "cph"), person("Salim", "Brüggemann", role = "ctb", - email = "salim-b@pm.me", comment = c(ORCID = "0000-0002-5329-5987"))) + email = "salim-b@pm.me", 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. URL: https://plotly-r.com, https://github.com/ropensci/plotly#readme, https://plot.ly/r From 67c8f125e68e49cd55c4ee4062db8293c50b2f40 Mon Sep 17 00:00:00 2001 From: Salim B Date: Thu, 21 May 2020 04:27:45 +0200 Subject: [PATCH 6/7] replace forcats code with base R --- tests/testthat/test-plotly.R | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-plotly.R b/tests/testthat/test-plotly.R index f9c52f9f35..9f42656291 100644 --- a/tests/testthat/test-plotly.R +++ b/tests/testthat/test-plotly.R @@ -319,8 +319,6 @@ test_that("toWebGL() shouldn't complain if it's already webgl", { }) 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)" @@ -333,9 +331,8 @@ test_that("Line breaks are properly translated (R -> HTML)", { fixed = TRUE) # test factor column - d <- iris %>% dplyr::mutate(Species = forcats::fct_relabel(Species, - paste0, - suffix)) + d <- iris + levels(d$Species) <- paste0(levels(d$Species), suffix) p1 <- d %>% plot_ly(x = ~Sepal.Length, y = ~Species) From d60898a60fffc016c16f675a503c59d432a41d2a Mon Sep 17 00:00:00 2001 From: Salim B Date: Mon, 22 Jun 2020 14:29:03 +0200 Subject: [PATCH 7/7] add news item --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 9f4fe92385..998d7a4c91 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ * All HTTP requests are now retried upon failure (#1656) +* R linebreaks (`\n`) in _factor labels_ are now translated to HTML linebreaks (`
`), 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).