There are many formatting or pretty printing functions available, some of them quite elaborate. Mine uses only functions in base. It seems that many programmers don't realize the power of strwrap and the sep parameter for cat.

An indented, wrapped list styled after a definition.

fl(x, label = "", indent_string = NA)

flp(x, label = "", indent_string = NA)

Arguments

x
the R object to display. toString must be able to evaluate it.
label
a string to describe the list. The remaining lines are indented by the number of characters from catcatenating indent_string and `label`.
indent_string
the string to prefix the first line. Defaults to tab.width option number of spaces. If you wanted your list indented 4 spaces, pass a string of 4 spaces to this parameter.

Value

a formatted string

Functions

  • flp: formatted string variant

R Trivia

Turns out the tab.width option is not set by default in base R as I discovered when I tried to build the package. Now explicitly test to see if the option is set.

Examples

fl(search(), label = "search(): ")
#> search(): .GlobalEnv, package:dgutils, devtools_shims, package:pkgdown, #> tools:rstudio, package:stats, package:graphics, package:grDevices, #> package:utils, package:datasets, package:ggplot2, #> package:devtools, package:methods, Autoloads, package:base
s <- flp(search(), label = "search(): ")