Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
65 changes: 45 additions & 20 deletions R/format.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
)
}


Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand All @@ -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 = "* ")
Expand All @@ -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))
}



Expand Down
5 changes: 4 additions & 1 deletion man/print_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions man/str_convenience.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading