Diagnosing 'object of type closure is not subsettable' in R
When R says object of type closure is not subsettable you are subsetting a function, not data. This guide shows how to diagnose name collisions and missing parentheses, and fix them.
09 May 2026, 02:26 UTC

You run data[1, ] and R stops with Error in data[1, ] : object of type 'closure' is not subsettable. The useful takeaway is immediate: you are subsetting a function, not a data object. In R, functions have type closure, so [, [[ or $ applied to a function triggers this message.
What the error means
Subsetting operators expect a vector, list, data frame or similar structure. A closure is R's internal type for a function object. When a name resolves to a function, name[ ] tries to subset the function itself, and R refuses. You can confirm the mechanism in a fresh R session: data[1, ] fails with this error because data resolves to the built-in data() function, while data <- mtcars followed by data[1, ] works.
Common causes and quick diagnostics
| Observed condition | Likely cause | Quick check |
|---|---|---|
typeof(x) returns "closure" | Name is bound to a function | Run typeof(x) in the console where the error occurs |
exists("x") is FALSE | Object was never created or was removed; the name falls back to a base function such as data() | Run exists("x") and inspect ls() |
| Code works interactively but fails in a script or chunk | Assignment was skipped or an earlier error stopped execution; separate environments | Check script order and the environment pane for that chunk or reactive context |
x[1] fails but x()[1] works | Missing parentheses on a function call | Verify the call is myFunc()[1], not myFunc[1] |
Ordered checks
Identify the offending name. The name before
[in the error message is the object. Run, in the same R session where the error occurs:typeof(<name>) class(<name>)Expected check:
typeofreturning"closure"confirms a function binding. If it returns"list"or"data.frame", the problem is different.Confirm the object exists in the current environment:
exists("<name>", inherits = FALSE) ls(pattern = "^<name>$")Risk: in R Markdown or Shiny, objects visible in the console may not exist in the chunk or reactive environment where the error occurs.
Trace the assignment. Search the script for where
<name>should be created, for example<name> <- read.csv(...). If an earlier line errored, later lines see the fallback binding instead.Check for missing parentheses. If you intended to call a function and then subset its result:
# wrong myFunc[1] # correct myFunc()[1]
Fixes tied to findings
Recreate or reload the missing object. If exists is FALSE and you expected data, re-run the assignment that creates it, for example data <- mtcars; after that, data[1, ] works.
Rename variables that shadow base functions. Names like data, c, t, df are legal but shadow built-ins. Pick a distinct name and update every reference. Risk: renaming can break downstream code that still uses the old name, so search the project for all references first.
Add the missing parentheses. Change myFunc[1] to myFunc()[1] when you need the function's return value.
Verify after any fix: run typeof(<name>) again and confirm it is no longer "closure", then re-run the failing subsetting expression.
When to escalate
Escalate when the object exists, is not a function, and the error persists. Immediately after the error, in the same session, run:
traceback()
options(error = recover)Inspect the call stack to see whether the subsetting happens inside a package or helper function rather than in your own code. That case is no longer a simple name collision and needs stack-level debugging.
Limitations
Shadowing is legal in R, and lexical scoping finds the nearest binding, so the error can surface far from the shadowing assignment. In R Markdown and Shiny, each chunk or reactive expression runs in its own environment. The behavior described is stable across R 3.x and 4.x, but the exact error wording can vary slightly by version and locale.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.