diff --git a/DESCRIPTION b/DESCRIPTION index bc7a25d24..e2b920e1e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: datawizard Title: Easy Data Wrangling -Version: 0.4.1.3 +Version: 0.4.1.4 Authors@R: c( person("Dominique", "Makowski", , "dom.makowski@gmail.com", role = "aut", comment = c(ORCID = "0000-0001-5375-9967", Twitter = "@Dom_Makowski")), diff --git a/NEWS.md b/NEWS.md index 9b631d94e..9b22a1033 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,11 @@ MAJOR CHANGES * Given his continued and significant contributions to the package, Etienne Bacher (@etiennebacher) is now included as an author. +CHANGES + +* Some of the text formatting helpers (like `text_concatenate()`) gain an + `enclose` argument, to wrap text elements with surrounding characters. + NEW FUNCTIONS * `row_to_colnames()` and `colnames_to_row()` to move a row to column names, diff --git a/R/format_text.R b/R/format_text.R index 07043143e..9fb56c522 100644 --- a/R/format_text.R +++ b/R/format_text.R @@ -10,6 +10,9 @@ #' @param sep Separator. #' @param last Last separator. #' @param n The number of characters to find. +#' @param enclose Character that will be used to wrap elements of `text`, so +#' these can be, e.g., enclosed with quotes or backticks. If `NULL` (default), +#' text elements will not be enclosed. #' @param ... Other arguments to be passed to or from other functions. #' #' @return A character string. @@ -23,6 +26,7 @@ #' #' # Smart concatenation #' text_concatenate(c("First", "Second", "Last")) +#' text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`") #' #' # Remove parts of string #' text_remove(c("one!", "two", "three!"), "!") @@ -34,8 +38,8 @@ #' # Paste with optional separator #' text_paste(c("A", "", "B"), c("42", "42", "42")) #' @export -format_text <- function(text, sep = ", ", last = " and ", width = NULL, ...) { - text_wrap(text_concatenate(text, sep = sep, last = last), width = width) +format_text <- function(text, sep = ", ", last = " and ", width = NULL, enclose = NULL, ...) { + text_wrap(text_concatenate(text, sep = sep, last = last, enclose = enclose), width = width) } @@ -58,22 +62,39 @@ text_lastchar <- function(text, n = 1) { #' @rdname format_text #' @export -text_concatenate <- function(text, sep = ", ", last = " and ") { +text_concatenate <- function(text, sep = ", ", last = " and ", enclose = NULL) { text <- text[text != ""] + if (length(text) && !is.null(enclose) && length(enclose) == 1 && nchar(enclose) > 0) { + text <- paste0(enclose, text, enclose) + } if (length(text) == 1) { - text + s <- text } else { s <- paste0(utils::head(text, -1), collapse = sep) s <- paste0(c(s, utils::tail(text, 1)), collapse = last) - s } + s } #' @rdname format_text #' @export -text_paste <- function(text, text2 = NULL, sep = ", ", ...) { +text_paste <- function(text, text2 = NULL, sep = ", ", enclose = NULL, ...) { if (!is.null(text2)) { + if (!is.null(enclose) && length(enclose) == 1 && nchar(enclose) > 0) { + text <- sapply(text, function(i) { + if (i != "") { + i <- paste0(enclose, i, enclose) + } + i + }) + text2 <- sapply(text2, function(i) { + if (i != "") { + i <- paste0(enclose, i, enclose) + } + i + }) + } paste0(text, ifelse(text == "" | text2 == "", "", sep), text2) } } diff --git a/man/format_text.Rd b/man/format_text.Rd index c97ab2314..0969d98c8 100644 --- a/man/format_text.Rd +++ b/man/format_text.Rd @@ -10,15 +10,22 @@ \alias{text_wrap} \title{Convenient text formatting functionalities} \usage{ -format_text(text, sep = ", ", last = " and ", width = NULL, ...) +format_text( + text, + sep = ", ", + last = " and ", + width = NULL, + enclose = NULL, + ... +) text_fullstop(text) text_lastchar(text, n = 1) -text_concatenate(text, sep = ", ", last = " and ") +text_concatenate(text, sep = ", ", last = " and ", enclose = NULL) -text_paste(text, text2 = NULL, sep = ", ", ...) +text_paste(text, text2 = NULL, sep = ", ", enclose = NULL, ...) text_remove(text, pattern = "", ...) @@ -35,6 +42,10 @@ text_wrap(text, width = NULL, ...) lines in the output. Can be "auto", in which case it will select 90\\% of the default width.} +\item{enclose}{Character that will be used to wrap elements of \code{text}, so +these can be, e.g., enclosed with quotes or backticks. If \code{NULL} (default), +text elements will not be enclosed.} + \item{...}{Other arguments to be passed to or from other functions.} \item{n}{The number of characters to find.} @@ -59,6 +70,7 @@ text_lastchar(c("ABC", "DEF"), n = 2) # Smart concatenation text_concatenate(c("First", "Second", "Last")) +text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`") # Remove parts of string text_remove(c("one!", "two", "three!"), "!") diff --git a/tests/testthat/test-format_text.R b/tests/testthat/test-format_text.R index 3bb05e110..abf1fe697 100644 --- a/tests/testthat/test-format_text.R +++ b/tests/testthat/test-format_text.R @@ -1,4 +1,14 @@ test_that("text formatting helpers work as expected", { + expect_equal( + format_text(c("A very long First", "Some similar long Second", "Shorter Third", "More or less long Fourth", "And finally the Last"), width = 20), + "A very long First,\nSome similar long\nSecond, Shorter\nThird, More or less\nlong Fourth and And\nfinally the Last\n" + ) + + expect_equal( + format_text(c("A very long First", "Some similar long Second", "Shorter Third", "More or less long Fourth", "And finally the Last"), last = " or ", enclose = "`", width = 20), + "`A very long\nFirst`, `Some\nsimilar long\nSecond`, `Shorter\nThird`, `More or\nless long Fourth`\nor `And finally the\nLast`\n" + ) + expect_equal( text_fullstop(c("something", "something else.")), c("something.", "something else.") @@ -15,6 +25,11 @@ test_that("text formatting helpers work as expected", { "First, Second and Last" ) + expect_equal( + text_concatenate(c("First", "Second", "Last"), last = " or ", enclose = "`"), + "`First`, `Second` or `Last`" + ) + expect_equal( text_remove(c("one!", "two", "three!"), "!"), c("one", "two", "three") @@ -27,4 +42,9 @@ test_that("text formatting helpers work as expected", { text_paste(c("A", "", "B"), c("42", "42", "42")), c("A, 42", "42", "B, 42") ) + + expect_equal( + text_paste(c("A", "", "B"), c("42", "42", "42"), enclose = "`"), + c("`A`, `42`", "`42`", "`B`, `42`") + ) })