Skip to content

Commit f85a9dd

Browse files
committed
translate linebreaks. fixes #851
1 parent 3b6f12d commit f85a9dd

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

R/plotly_build.R

+2
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
324324
# ensure we get the order of categories correct
325325
# (plotly.js uses the order in which categories appear by default)
326326
p <- populate_categorical_axes(p)
327+
# translate '\n' to '<br />' in text strings
328+
p <- translate_linebreaks(p)
327329
# verify plot attributes are legal according to the plotly.js spec
328330
p <- verify_attr_names(p)
329331
# box up 'data_array' attributes where appropriate

R/utils.R

+23
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,29 @@ relay_type <- function(type) {
327327
type
328328
}
329329

330+
# Searches a list for character strings and translates R linebreaks to HTML
331+
# linebreaks (i.e., '\n' -> '<br />'). JavaScript function definitions created
332+
# via `htmlwidgets::JS()` are ignored
333+
translate_linebreaks <- function(p) {
334+
# maintain trace classes (important for colorbars)...
335+
# is there a more general way maintain list element classes?
336+
cl <- lapply(p$x$data, class)
337+
recurse <- function(a) {
338+
typ <- typeof(a)
339+
if (typ == "list") {
340+
lapply(a, recurse)
341+
} else if (typ == "character" && !inherits(a, "JS_EVAL")) {
342+
b <- gsub("\n", "<br />", a, fixed = TRUE)
343+
attributes(b) <- attributes(a)
344+
b
345+
} else a
346+
}
347+
p$x$data <- lapply(p$x$data, recurse)
348+
p$x$data <- Map(function(x, y) structure(x, class = y), p$x$data, cl)
349+
p$x$layout <- lapply(p$x$layout, recurse)
350+
p
351+
}
352+
330353
verify_orientation <- function(trace) {
331354
xNumeric <- !is.discrete(trace[["x"]]) && !is.null(trace[["x"]] %||% NULL)
332355
yNumeric <- !is.discrete(trace[["y"]]) && !is.null(trace[["y"]] %||% NULL)

0 commit comments

Comments
 (0)