Your First R Program
Run R for the first time, learn what the prompt is doing, understand how output appears, and write a tiny program that does something useful.
Let us run R for the first time.
You do not have to install anything. The code block below is a fully functional R session, running in your browser. Click Run.
When you ran that, several things happened invisibly:
- R compiled your line of code.
- It looked up the name
printand found it refers to a built-in function. - It evaluated the string
"Hello, R!"(which is a piece of "character data"). - It called the function on the data.
- The function wrote
"Hello, R!"to the output stream. - WebR captured that and showed it to you.
That whole cycle happens every time you run code. Once you have internalized that this is what a "run" is, R becomes much less mysterious.
What is "the prompt"?
If you were running R locally in a terminal, you would see something like:
R version 4.5.0 (2025-04-11) -- "Spring Renaissance"
Copyright (C) 2025 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
> The > is the R prompt. It is R saying "I am ready for an
instruction." You type one, press Enter, and R either evaluates it
and shows the result, or prints an error.
In WebR, we replace the prompt with a friendly code editor — but the semantics are the same. Each time you click Run, R reads your code, evaluates it top to bottom, and prints the output.
Two kinds of "output"
R has a slightly unusual habit. There are two ways something can appear in the output area:
- Printing explicitly, with
print()orcat(). - Auto-printing, which happens when you type an expression and do not assign it.
Both are useful; they are easy to confuse at first.
Notice the third line, x <- 3 + 3. Nothing appeared in the
output, because the value of that expression was assigned to a
name (x) instead of being printed. This is the most common
"why didn't anything happen?" confusion for beginners. The answer
is always: it did happen — you just stored the result.
The two ways to print: print() versus cat()
R inherits these two siblings from S, and they behave slightly differently.
print() shows the value with its R-style formatting (vectors get
index hints like [1], strings appear in quotes, etc.). It is
designed for interactive inspection.
cat() concatenates its arguments into a plain string and writes
it. It is designed for making readable output.
Use print() when you want to inspect a value. Use cat() when
you want to write a sentence. As a rule of thumb: if you are
building a message with multiple pieces, use cat().
The "\n" is a newline character — without it, multiple
cat() calls would all run together on a single line.
Comments
Anything after a # on a line is a comment — R ignores it.
Comments are notes you leave for the next human to read your code
(often yourself, three months from now).
A good general rule: code should describe what you are doing through naming, and comments should explain why you are doing it. You will see this throughout the course.
Errors are friendly (most of the time)
R will tell you when it does not understand. Most of the time the message is helpful, if a bit terse.
That should produce an error like:
Error in prnt("hello") : could not find function "prnt"The message tells you:
- Where the problem is (
in prnt("hello")). - What the problem is (
could not find function "prnt").
Try fixing it:
Errors are not a sign you have failed; they are a sign R is helping you. You will read a lot of error messages over the years. The good news: they get easier with practice, and Googling the exact text of an R error almost always finds the answer.
What R is doing under the hood
When you click Run, the WebR runtime does roughly this:
Nothing more, nothing less. There is no "code review fairy" who fixes things. There is no AI inferring what you meant. R does exactly what you write, and reports exactly what happened.
This is simultaneously the wonderful and the scary thing about programming: the machine is rigorously, mercilessly literal.
A small program that does something real
Let's combine everything so far into a one-screen program. We will compute a heart-health summary for a small group of people.
That tiny program already does several things characteristic of
data analysis: defines named columns, applies a vectorized
calculation (bmi <- weights / (heights / 100)^2), and walks
through results to print human-readable output.
Notice that the BMI calculation itself is vectorized — no loop was needed to compute the four BMIs. The loop only appeared when we wanted to print four different sentences. This is the texture of real R code: vectorize the math, loop only when you really need per-element behavior.
Test your understanding
Which of these lines will produce visible output when run by itself?
x <- 5
5 + 3
# print("hello")
print(5)
What is the difference between print() and cat()?
They are identical.
print() formats values in R-style for inspection; cat() concatenates arguments into a plain string for building human-readable messages.
print() writes to a file; cat() writes to the screen.
cat() is the modern replacement for print().
Why does this code produce no output?
result <- 2 + 2
Because R does not handle integer arithmetic.
Because the result of the expression was assigned to the variable result instead of being printed.
Because of a parsing error.
Because R only prints character strings.
Mini challenge: write a tiny program
Write a short program that defines two vectors — cities (a
character vector) and temperatures_c (a numeric vector of
celsius temperatures of the same length) — and prints one line
per city in the format:
<city>: <temp> C / <temp_f> F…where <temp_f> is the Fahrenheit equivalent, rounded to 1
decimal. Use the formula F = C * 9/5 + 32.
Given the vectors cities and temperatures_c (already loaded), print one line per city in the format <city>: <temp> C / <temp_f> F. Make sure temp_f is rounded to 1 decimal place.
In the next page we'll get more comfortable with R as the world's most powerful pocket calculator, before moving on to variables.
Thinking in Data
Before writing a line of R, what does it mean to "think computationally" about data? An introduction to the mental shift from doing arithmetic by hand to instructing a computer to do it for you.
R as a Calculator
Get fluent with R's basic arithmetic and comparison operators. Before R is "the language of data science," it is the world's most powerful pocket calculator — and the calculator habits matter.