cat( )

Print labelled results with units to the console. cat() is a base R function.

Required Library

# cat() is part of base R — no package installation needed

Syntax

cat(...)

cat(...) concatenates and prints its arguments to the console with no formatting. Unlike print(), it does not add quotes around character strings or display [1] index prefixes, making it ideal for producing clean, readable output.

cat("Label:", value, "unit\n")

cat("Label:", value, "unit\n") is the standard pattern for reporting a named result with its unit. The \n at the end moves the cursor to a new line, creating line breaks. Useful for printing multiple results in a report.

NoteCoding convention — always label printed results

Never print a bare variable name or call print() on a calculated value. Always use cat() with a descriptive label and the unit. A number without context is meaningless in a pharmaceutical report.

Examples

Combine text and values

Rounds the value to the appropriate number of significant figures at the point of printing, while keeping the stored variable at full precision.

Print multiple results from a content uniformity assay

"\n" creates a line break so each result is printed on its own line.

Custom separator between items

Argument Overview

Required arguments must be included when using a function while optional arguments can be included on demand.

— items to print character | numeric | logical Required

One or more values to print, separated by commas. Items are coerced to character before printing. Common usage combines a text label, a computed value (wrapped in signif() or round()), and a unit string:

cat("Mean fill mass:", signif(X_bar, 4), "mg\n")

Use \n (newline) as the last item so the next output starts on a fresh line.

Data Types: character | numeric | logical | integer

sep — separator between items character Optional

A character string inserted between consecutive items. Default is a single space " ". Set sep = "" to print items with no gap, or sep = ", " for comma-separated lists.

cat("Result:", value, "mg\n")          # default: space between each item
cat("A", "B", "C", sep = "-")          # prints: A-B-C

Data Types: character · Default: ” “

file — output destination character | connection Optional

Where to send the output. Default "" prints to the console. Pass a file path string (or an open connection) to write to a file instead.

cat("Result:", signif(AV_s1, 4), "\n", file = "results.txt")

Data Types: character (file path) | connection · Default: ““ (console)

append — append to file instead of overwriting logical Optional

Only relevant when file is a file path. If TRUE, new output is appended to the end of the file. If FALSE (default), the file is overwritten.

Data Types: logical · Default: FALSE