Unexpected symbol
Matches: unexpected symbol in
WHAT THIS ERROR MEANS
R found two symbols next to each other without an operator or separator between them. Common cause: missing comma, pipe, or operator.
WHY IT HAPPENS — 4 COMMON CAUSES
1. A missing comma between arguments
The commonest cause by far. c(1 2, 3) and data.frame(x = 1 y = 2) both put two tokens side by side with no separator. In a long multi-line call the omission is easy to miss.
2. A missing operator
x <- 5 y is not valid, nor is df |> filter(a > 1) select(b) — the second is missing its pipe. Chained pipelines are especially prone to this when lines are reordered.
3. A space in a name
my data <- 5 parses as the symbol my followed by the symbol data. Names must use dots or underscores, or be wrapped in backticks as `my data` when they come from imported data.
4. An unclosed quote or bracket on an earlier line
This is why the reported line often looks fine. An unterminated string on line 10 makes everything after it part of that string until the next quote, and the parser only gives up at some later token. Fix the earliest suspicious line, not the reported one.
HOW TO DIAGNOSE WHICH ONE IT IS
- Look at the line before the one reported. Parse errors surface where the parser gives up, which is usually after the actual mistake.
- Check bracket and quote balance — RStudio and VS Code both highlight the matching partner when the cursor is on one.
- A console prompt stuck at + means an incomplete expression from an earlier line. Press Escape to reset before trying again.
- Comment out blocks and re-source to bisect the file down to the offending region.
- Reformat with styler::style_file() — badly broken syntax usually becomes obvious once indentation is normalised.
HOW TO FIX IT
1. Look for missing commas between function arguments.
2. Check for missing operators (+, -, *, |>, %>%).
3. Variable names cannot contain spaces.
CODE EXAMPLES
my data <- c(1, 2, 3)
my_data <- c(1, 2, 3)
MORE SCENARIOS THAT TRIGGER THIS
A pipeline missing one pipe
df |> filter(revenue > 100) select(region, revenue) # Error: unexpected symbol
df |> filter(revenue > 100) |> select(region, revenue)
Reordering pipeline steps is the usual way this happens. Keeping the pipe at the end of each line makes a missing one visible at a glance.
Column names with spaces
df$Total Revenue # Error: unexpected symbol in "df$Total Revenue"
df$`Total Revenue` # better, normalise the names once on import: df <- janitor::clean_names(df) df$total_revenue
Backticks work but every later reference needs them. Cleaning the names once at import is less error-prone than quoting them throughout the analysis.
PACKAGE-SPECIFIC NOTES
This is a parse error, so it fires before any code runs — nothing in the file executes, including lines above the mistake. That distinguishes it from runtime errors and explains why no partial results appear. R has two other parse errors worth recognising: "unexpected end of input" means a bracket or quote is still open when the file ends, and "unexpected string constant" usually means a missing comma directly before a quoted argument. All three are fixed by looking earlier in the file rather than at the reported position.
FUNCTIONS WORTH KNOWING
parse()styler::style_file()janitor::clean_names()make.names()FREQUENTLY ASKED QUESTIONS
Why does R report a line that looks perfectly fine?
The parser reports where it could no longer make sense of the input, not where the mistake was made. An unclosed quote or bracket earlier keeps the expression open until some later token becomes impossible, so the real fix is usually above the reported line.
Why is my console stuck showing a + prompt?
R is waiting for you to finish an incomplete expression — an open bracket, quote or trailing operator. Press Escape to abandon it and fix the original line.
Can I use spaces in column names at all?
Yes, if you wrap them in backticks everywhere they appear. It is legal but tedious, and one forgotten pair produces this error. Normalising names once with janitor::clean_names() is the more maintainable choice.
Still stuck?
Paste your code in RChat and the AI will fix this error in context.