pretty_string: Truncate and wrap strings

Description Usage Arguments Functions Examples

View source: R/misc.R

Description

Truncates, trims, and wraps strings. Built for ggplot2 plots with long string labels.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
pretty_string(
  x,
  truncate_at = 80,
  truncate_with = "...",
  trim = TRUE,
  wrap_at = 40
)

format_pretty_string(
  truncate_at = 80,
  truncate_with = "...",
  trim = TRUE,
  wrap_at = 40
)

Arguments

x

Strings

truncate_at

Maximum total string length prior to wrapping. Default is 80 characters. Use NULL to skip truncation.

truncate_with

Character string that is added at the end of each string to indicate that the string was truncated. Eats into string length. Default is "..."; set to NULL or "" to skip.

trim

Should whitespace be trimmed? Default is TRUE.

wrap_at

Wraps string at given length, passed to stringr::str_wrap().

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
text <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
pretty_string(text, truncate_at = 20, wrap_at = NULL)
pretty_string(text, truncate_at = NULL, wrap_at = 10)

library(ggplot2)
set.seed(654321)
ex <- dplyr::data_frame(
  label = sample(stringr::sentences, 3),
  value = runif(3, 0, 10)
)

g <- ggplot(ex) +
  aes(value, label) +
  geom_point()

g

g + scale_y_discrete(
  label = format_pretty_string(truncate_at = 25)
)

g + scale_y_discrete(
  label = format_pretty_string(truncate_at = NULL, wrap_at = 20)
)

GerkeLab/grkmisc documentation built on Feb. 23, 2020, 6:50 a.m.