Skip to content

Commit

Permalink
Merge pull request #37 from jimhester/master
Browse files Browse the repository at this point in the history
Add function to word wrap long character vectors
  • Loading branch information
hadley committed Jun 10, 2015
2 parents 55532d3 + 1c0ea8f commit 0418281
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export(trans_breaks)
export(trans_format)
export(trans_new)
export(trans_range)
export(wrap_format)
export(zero_range)
import(Rcpp)
import(methods)
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Version 0.2.5
------------------------------------------------------------------------------

* `wrap_format()` function to wrap character vectors to a desired width.
(@jimhester, #37).

* Dollar format is now more flexible and can add either prefixes or suffixes
for different currencies (#53).

Expand Down
15 changes: 15 additions & 0 deletions R/formatter.r
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ math_format <- function(expr = 10 ^ .x, format = force) {
}
globalVariables(".x")

#' Wrap text to a specified width, adding newlines for spaces if text exceeds
#' the width
#'
#' @param width value above which to wrap
#' @return Function with single parameter x, a character vector, that
#' returns a wrapped character vector
#' @export
#' @examples
#' wrap_10 <- wrap_format(10)
#' wrap_10('A long line that needs to be wrapped')
wrap_format <- function(width) {
function(x) {
unlist(lapply(strwrap(x, width = width, simplify = FALSE), paste0, collapse = "\n"))
}
}
#' Format labels after transformation.
#'
#' @param trans transformation to apply
Expand Down
25 changes: 25 additions & 0 deletions man/wrap_format.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/formatter.r
\name{wrap_format}
\alias{wrap_format}
\title{Wrap text to a specified width, adding newlines for spaces if text exceeds
the width}
\usage{
wrap_format(width)
}
\arguments{
\item{width}{value above which to wrap}
}
\value{
Function with single parameter x, a character vector, that
returns a wrapped character vector
}
\description{
Wrap text to a specified width, adding newlines for spaces if text exceeds
the width
}
\examples{
wrap_10 <- wrap_format(10)
wrap_10('A long line that needs to be wrapped')
}

11 changes: 11 additions & 0 deletions tests/testthat/test-formatter.r
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ test_that("dollar format", {
expect_equal(dollar(c(99.999)), c("$100"))
})

test_that("wrap correctly wraps long lines", {
expect_equal(wrap_format(10)("this is a long line"),
"this is a\nlong line")
expect_equal(wrap_format(10)(c("this is a long line", "this is another long line")),
c("this is a\nlong line", "this is\nanother\nlong line"))
expect_equal(wrap_format(10)("a_very_long_word_without_spaces"),
"a_very_long_word_without_spaces")
expect_equal(wrap_format(10)("short line"), "short\nline")
expect_equal(wrap_format(15)("short line"), "short line")
})

test_that("formatters don't add extra spaces", {
has_space <- function(x) any(grepl("\\s", x))
x <- 10 ^ c(-1, 0, 1, 3, 6, 9)
Expand Down

0 comments on commit 0418281

Please sign in to comment.