Skip to content
AgileLogics
4 min read

How R Code Runs

Prerequisites: Your first R program

R is an interpreted language: the R interpreter reads each expression, evaluates it, and prints the result — a cycle called the REPL (read–evaluate–print loop). There's no separate compile step.

Real-world analogy: R is like a very fast assistant with a calculator. You ask one question at a time and get an answer back immediately — ideal for exploring data, where each answer suggests the next question.

Code runs top to bottom

A script runs one expression after another. An error stops it at that point:

cat("This line runs\n")
total <- 10 + "5"
cat("This line never runs\n")
This line runs
Error in 10 + "5" : non-numeric argument to binary operator

The error message shows the expression that failed (10 + "5") and the reason. Read R errors carefully — they're short, but usually point straight at the problem.

Errors vs warnings

R also has warnings: the code runs, but R thinks something may be wrong:

as.numeric("abc")
cat("Still running\n")
[1] NA
Still running
Warning message:
NAs introduced by coercion

as.numeric("abc") can't make a number, so it returns NA (R's missing value) and warns you. Don't ignore warnings — they often reveal data problems.

The workspace

Every variable you create lives in your workspace (the global environment), shown in RStudio's Environment pane. Restart R regularly (Session → Restart R) and re-run your script from the top, to check it doesn't depend on something you typed into the console earlier.