diff --git a/DESCRIPTION b/DESCRIPTION index 78c718c..46e3546 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GiottoUtils Title: Giotto Suite Utilities -Version: 0.2.4 +Version: 0.2.5 Authors@R: c( person("Ruben", "Dries", email = "rubendries@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7650-7754")), diff --git a/NAMESPACE b/NAMESPACE index 4827c3e..b68c807 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -86,6 +86,7 @@ export(str_double_quote) export(str_locate2) export(str_parenth) export(str_quote) +export(str_reformat) export(str_vector) export(suite_install) export(suite_packages) diff --git a/NEWS.md b/NEWS.md index bc7c96e..2a872ac 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# GiottoUtils 0.2.5 (2025/05/21) + +## New +- `str_reformat()` for print post-processing with `strwrap()` + +## Enhancement +- Improve fallbacks for `print_list()` + + # GiottoUtils 0.2.4 (2025/05/06) ## Changes diff --git a/R/format.R b/R/format.R index 493e225..968b572 100644 --- a/R/format.R +++ b/R/format.R @@ -60,13 +60,11 @@ wrap_txt <- function(..., } cat(..., sep = sep) %>% - capture.output() %>% - strwrap( - prefix = .prefix, initial = .initial, # indent later lines, - # no indent first line + str_reformat( + prefix = .prefix, + initial = .initial, width = min(80, getOption("width"), strWidth) - ) %>% - paste(collapse = "\n") + ) } #' @rdname wrap_txt @@ -93,13 +91,12 @@ wrap_txtf <- function(..., } cat(sprintf(...), sep = sep) %>% - capture.output() %>% - strwrap( - prefix = .prefix, initial = .initial, # indent later lines, - # no indent first line + # indent later lines, no indend first line + str_reformat( + prefix = .prefix, + initial = .initial, width = min(80, getOption("width"), strWidth) - ) %>% - paste(collapse = "\n") + ) } @@ -318,14 +315,16 @@ gstop <- function( #' cat(str_parenth(x), "\n") #' cat(str_double_quote(x), "\n") #' cat(str_quote(x), "\n") -#' -#' vec <- c("item1", "item2", "item3") -#' cat(str_vector(vec), "\n") -#' cat(str_vector(vec, qchar = "double")) NULL -#' @rdname str_convenience +#' @describeIn str_convenience Format a vector of values into 'a', 'b', 'c' or +#' "a", "b", "c" depending on `qchar` via [toString()] #' @param qchar quote character to use. Either 'single' or "double" +#' @examples +#' # format a set of character values with str_vector() +#' vec <- c("item1", "item2", "item3") +#' cat(str_vector(vec)) # single quote (default) +#' cat(str_vector(vec, qchar = "double")) # double quote #' @export str_vector <- function(x, qchar = c("single", "double")) { qchar <- match.arg(qchar, choices = c("single", "double")) @@ -359,7 +358,18 @@ str_quote <- function(x) { paste0("\'", x, "\'") } - +#' @describeIn str_convenience capture and reformat a print with [strwrap()] +#' @param ... additional params to pass to [strwrap()] +#' @examples +#' # print post-processing with str_reformat() +#' txt <- "hello +#' world" +#' cat(str_reformat(cat(txt), indent = 3)) +#' @export +str_reformat <- function(x, ...) { + cap <- capture.output(x) + paste(strwrap(cap, ...), collapse = "\n") +} #' @name print_list #' @title Pretty print formatting for lists and vectors @@ -378,7 +388,10 @@ str_quote <- function(x) { #' test <- list( #' name1 = "1", #' longername2 = "test_char", -#' thirdname = factor("this will be converted with as.character()") +#' thirdname = factor("this will be converted with as.character()"), +#' df_test = data.frame(a = "a", b = "b"), +#' list_test = list(a = 1, b = 2, c = 3), +#' formula_test = ~a #' ) #' print_list(test) #' print_list(test, pre = "* ") @@ -391,11 +404,23 @@ print_list <- function(x, pre = "") { if (length(ns) != length(x)) { stop("all elements must be named") } - x <- lapply(x, as.character) + x <- lapply(x, function(i) { + if (is.character(i) || + is.logical(i) || + is.factor(i) || + is.numeric(i)) { + return(as.character(i)) + } + # fallback + .print_fallback(i) + }) cat(sprintf("%s%s : %s", pre, format(ns), x), sep = "\n") invisible(x) } +.print_fallback <- function(x) { + sprintf("<%s> length %d", class(x), length(x)) +} diff --git a/man/print_list.Rd b/man/print_list.Rd index 4fdad27..14977a5 100644 --- a/man/print_list.Rd +++ b/man/print_list.Rd @@ -29,7 +29,10 @@ print_list(testvec) test <- list( name1 = "1", longername2 = "test_char", - thirdname = factor("this will be converted with as.character()") + thirdname = factor("this will be converted with as.character()"), + df_test = data.frame(a = "a", b = "b"), + list_test = list(a = 1, b = 2, c = 3), + formula_test = ~a ) print_list(test) print_list(test, pre = "* ") diff --git a/man/str_convenience.Rd b/man/str_convenience.Rd index 69f637e..12d66b2 100644 --- a/man/str_convenience.Rd +++ b/man/str_convenience.Rd @@ -7,6 +7,7 @@ \alias{str_parenth} \alias{str_double_quote} \alias{str_quote} +\alias{str_reformat} \title{String convenience functions} \usage{ str_vector(x, qchar = c("single", "double")) @@ -18,11 +19,15 @@ str_parenth(x) str_double_quote(x) str_quote(x) + +str_reformat(x, ...) } \arguments{ \item{x}{string item(s) to format} \item{qchar}{quote character to use. Either 'single' or "double"} + +\item{...}{additional params to pass to \code{\link[=strwrap]{strwrap()}}} } \value{ character @@ -30,14 +35,26 @@ character \description{ String convenience functions } +\section{Functions}{ +\itemize{ +\item \code{str_vector()}: Format a vector of values into 'a', 'b', 'c' or +"a", "b", "c" depending on \code{qchar} via \code{\link[=toString]{toString()}} + +\item \code{str_reformat()}: capture and reformat a print with \code{\link[=strwrap]{strwrap()}} + +}} \examples{ x <- "test" cat(str_bracket(x), "\n") cat(str_parenth(x), "\n") cat(str_double_quote(x), "\n") cat(str_quote(x), "\n") - +# format a set of character values with str_vector() vec <- c("item1", "item2", "item3") -cat(str_vector(vec), "\n") -cat(str_vector(vec, qchar = "double")) +cat(str_vector(vec)) # single quote (default) +cat(str_vector(vec, qchar = "double")) # double quote +# print post-processing with str_reformat() +txt <- "hello + world" +cat(str_reformat(cat(txt), indent = 3)) }