Convert a Data Frame or R Object to a String Representation
Source:R/tableToString.R
tableToString.Rd
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.
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