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
| Parameter | Type | Default | Description |
|---|---|---|---|
... | objects | — | Objects to be concatenated and printed |
file | character | "" | Filename or connection to write to; "" outputs to console |
sep | character | " " | String inserted between objects |
fill | logical | FALSE | If TRUE, wrap lines after fill characters |
labels | character | NULL | Character vector of line labels (when fill=TRUE) |
append | logical | FALSE | If 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")