cat()

cat(... , file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)
Returns: NULL (invisibly) · Updated March 13, 2026 · Base Functions
cat output base print

cat() is a base R function that outputs objects to the console or a file. Unlike print(), it does not add quotation marks around strings and offers fine-grained control over separators and output destinations.

Syntax

cat(... , file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)

Parameters

ParameterTypeDefaultDescription
...objectsObjects to be concatenated and printed
filecharacter""Filename or connection to write to; "" outputs to console
sepcharacter" "String inserted between objects
filllogicalFALSEIf TRUE, wrap lines after fill characters
labelscharacterNULLCharacter vector of line labels (when fill=TRUE)
appendlogicalFALSEIf TRUE, append to file instead of overwriting

Examples

Basic console output

cat("Hello", "world", "!")
# Hello world !

Custom separator

cat("a", "b", "c", sep = "-")
# a-b-c

cat("x", "y", "z", sep = "\n")
# x
# y
# z

Writing to a file

# Write to a file (overwrites by default)
cat("Hello, file!", file = "greeting.txt")

# Append to existing file
cat("\nGoodbye!", file = "greeting.txt", append = TRUE)

# Read back the content
readLines("greeting.txt")
# [1] "Hello, file!" "Goodbye!"

Building strings for processing

# Useful in scripts to build status messages
status <- "processing"
percent <- 75
cat("Status:", status, "(", percent, "% complete)\n", sep = "")
# Status: processing (75% complete)

Common Patterns

Progress indicators in loops:

for (i in 1:5) {
  cat("Processing item", i, "\r")
  Sys.sleep(0.5)
}
cat("Done!\n")
# Processing item 5
# Done!

Logging to file with timestamps:

log_msg <- function(msg) {
  timestamp <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
  cat(timestamp, " -", msg, "\n", file = "app.log", append = TRUE)
}

log_msg("Application started")
log_msg("Data loaded successfully")

See Also