How Printing Works in R
R has a habit no other mainstream language shares: at the console, you don't need a print statement at all. Type a variable's name, press enter, and R prints its value. That's auto-printing, and it's why R tutorials are full of bare expressions.
But auto-printing has a strict boundary: it only fires for expressions at the top level. Inside a for loop or a function body, a bare value is evaluated and silently thrown away:
The lone x prints (it's top-level), the first loop prints nothing at all, and the second prints 1, 2, 3 because it asks explicitly. "My loop runs but shows nothing" is one of the most-asked R questions, and this is the whole answer: inside loops and functions, printing is always your job. (The same applies when you run a script with Rscript - what worked line-by-line at the console can go quiet inside a loop.)
print(): The Formal One
print() shows a value the way R sees it - which makes it the right tool for inspecting data, and the wrong one for polished output:
Two things stand out. Strings keep their quotes - print() is showing you a faithful representation, and the quotes tell you it's a character value. And every line starts with a bracketed number like [1]: that's the index of the first element on that line, so when a long vector wraps onto new lines, each line's bracket tells you which element it resumes at. It's a reading aid for vectors, not noise.
If you want print()-style output without the quotes, both print(x, quote = FALSE) and noquote(x) do it - but usually what you actually want at that point is cat().
cat(): Output for Humans
cat() (concatenate-and-print) writes raw text: no quotes, no index prefixes, and its arguments joined with spaces. The price of that control is that nothing is added for you - including the newline:
Forgetting the "\n" is the universal cat() rite of passage - without it the next piece of output glues onto the same line. Make ending cat() calls with "\n" a reflex.
The quote difference between the two functions is really a difference in what they show. print() displays escape sequences as you typed them; cat() renders them:
print() shows the \t and \n literally inside quotes - useful when you're debugging exactly what a string contains. cat() turns them into a real tab and a real line break - useful when you're producing output. Pick by intent: print() to inspect, cat() to present.
Formatted Reports: sprintf() + cat()
cat()'s space-separated arguments only go so far. For aligned, formatted output - fixed decimals, padded counters - build the string with sprintf() and hand it to cat():
%-8s pads each name to 8 characters and %6.2f gives every score the same width and two decimals, so the columns line up. This pair is the base-R workhorse for progress messages and small text reports (the format codes are covered in strings).
message() and warning(): The Other Channel
cat() and print() write to standard output - the stream that holds your program's results. message() and warning() write to standard error, the stream for commentary about the run:
On screen both look similar, but the separation matters the moment output is redirected: pipe your script's stdout to a file and the 42 lands in the file while the status note stays on the terminal. That's the rule of thumb - results to stdout with cat(), commentary to stderr with message(). It's also why message() (not cat()) is the polite way for functions and packages to talk to their users: callers can silence it with suppressMessages() without losing real output.
warning() is stronger - "something's off, but I carried on" - and when a script runs under Rscript, warnings are collected and shown together at the end rather than inline (see debugging errors for reading them).
Reading User Input: readline() and readLines()
To ask the user a question in an interactive session (the R console or RStudio), use readline(). These examples aren't runnable in the embedded editor - the on-page runner has no input panel - so try them in a local console:
# Interactive sessions only:
name <- readline("What is your name? ")
cat("Hello,", name, "\n")
age <- as.numeric(readline("Your age? "))
cat("Next year you'll be", age + 1, "\n")
Two things to remember. readline() always returns a string - convert with as.numeric() before doing math, and check the result for NA in case the user typed something that isn't a number. And in a non-interactive script, readline() doesn't wait for input at all; it instantly returns "".
For a script run with Rscript that should read piped or typed input, read from standard input instead:
# In a script run as: echo "Ada" | Rscript greet.R
line <- readLines(con = "stdin", n = 1)
cat("Hello,", line, "\n")
readLines(con = "stdin") with no n reads every line until the input ends - the standard shape for filter-style scripts. Reading data files is a different job with better tools; start with read-csv when your input is a dataset rather than a keyboard.
What You Take Away
- Auto-printing only happens at the top level - inside loops and functions, call
print()orcat()yourself. print()inspects: quotes on strings,[1]index prefixes, escapes shown literally.cat()presents: raw text, arguments joined by spaces, and you supply the"\n".sprintf()+cat()is the base-R pattern for formatted, aligned output.- Results go to stdout (
cat), commentary to stderr (message,warning). readline()for interactive input (always a string!);readLines(con = "stdin")for Rscript.
Next up: operators - arithmetic, comparison, and the logical operators that drive every condition you'll write.
Frequently Asked Questions
What is the difference between print() and cat() in R?
print() shows a value the way R represents it - strings keep their quotes and every line gets a [1]-style index prefix. cat() writes raw text with no quotes, no indexes, and no automatic newline (you add "\n" yourself). Use print() to inspect objects, cat() to produce output for humans.
Why does my R loop not print anything?
Auto-printing only happens for expressions at the top level. Inside a for loop or a function body, a bare x evaluates and is silently discarded. Wrap it explicitly: print(x) or cat(x, "\n").
How do you read user input in R?
In an interactive session, readline("prompt: ") reads one line of typed input as a string (convert with as.numeric() if you need a number). In a script run with Rscript, readline() doesn't wait - read from standard input with readLines(con = "stdin", n = 1) instead.
How do you print without quotes in R?
Use cat("hello\n") - it never adds quotes. If you specifically want print() semantics without quotes, print("hello", quote = FALSE) or noquote("hello") also work.