Skip to contents

Captures the printed output of a data.frame or an R object (coerced to a data.frame) as a single string with preserved formatting. Useful for error messages, logging, and string-based output.

Usage

tableToString(obj)

Arguments

obj

An R object that can be coerced to a data.frame

Value

A character string containing the formatted table output with newlines

Examples

# Basic usage with a data.frame
df <- data.frame(
  numbers = 1:3,
  letters = c("a", "b", "c")
)
str_output <- tableToString(df)
cat(str_output)
#>   numbers letters
#> 1       1       a
#> 2       2       b
#> 3       3       c

# Using in error messages
df <- data.frame(value = c(10, 20, 30))
if (any(df$value > 25)) {
  msg <- sprintf(
    "Values exceed threshold:\n%s",
    tableToString(df)
  )
  message(msg)
}
#> Values exceed threshold:
#>   value
#> 1    10
#> 2    20
#> 3    30