Closure is not subsettable
Matches: object of type 'closure' is not subsettable
WHAT THIS ERROR MEANS
You tried to use [ ] or $ on a function. This usually means you forgot the parentheses to call the function, or a variable has the same name as a function.
WHY IT HAPPENS — 4 COMMON CAUSES
1. The variable was never assigned, and a function has that name
The classic case is df. If your assignment failed or never ran, R keeps looking up the search path and finds stats::df(), the F distribution density. df[1, ] then tries to index a function. data, c, t and rev cause the same thing.
2. The parentheses were left off a function call
getwd is the function; getwd() calls it. Writing my_data <- read.csv assigns the function itself rather than its result, and any later indexing of my_data hits this error.
3. A typo in the object name
my_dat[1] when the object is my_data. If nothing matches, R continues to the attached packages, and with a short name it will often find a function there rather than stopping.
4. The assignment silently failed earlier
If the line creating the object errored, execution may have continued — particularly when sourcing without stopping on error — leaving the name unbound and the lookup falling through to a package function.
HOW TO DIAGNOSE WHICH ONE IT IS
- Run class(df). Getting "function" confirms the diagnosis immediately.
- Run exists("df", where = globalenv(), inherits = FALSE) — FALSE means your object was never created and the name is resolving to a package.
- Type the bare name. If R prints a function body, that is what you have been indexing.
- Scroll up for a failed assignment: the real error is usually earlier and this is the consequence.
- Check for a missing () on the line that should have created the object.
HOW TO FIX IT
1. Add () to call the function before subsetting.
2. Check if your variable name shadows a function.
3. Make sure you assigned the result, not the function itself.
CODE EXAMPLES
data[1, ] # if data is still the function, not your data frame
my_data <- read.csv("file.csv")
my_data[1, ]MORE SCENARIOS THAT TRIGGER THIS
Assigning a function instead of calling it
my_data <- read.csv my_data[1, ] # Error: object of type 'closure' is not subsettable
my_data <- read.csv("sales.csv")
my_data[1, ]Without parentheses R assigns the function object itself. class(my_data) returning "function" rather than "data.frame" is the tell.
The name df colliding with stats::df
# the read failed, so df was never created
df <- read.csv("missing.csv") # errored
df$revenue
# Error: object of type 'closure' is not subsettablesales <- read.csv("sales.csv")
sales$revenueNaming data frames something descriptive avoids the collision entirely and makes the code easier to read. df, data and c are the names most likely to shadow something in base R.
PACKAGE-SPECIFIC NOTES
This error is a direct consequence of lexical scoping: when a name is not found in the current environment R keeps searching enclosing environments and then the attached packages, so a missing object frequently resolves to a base function rather than failing outright. Shiny developers meet it constantly because reactives must be called with parentheses — output$plot reading my_data instead of my_data() indexes the reactive function itself. The fix there is the trailing (), not a change to the data.
FUNCTIONS WORTH KNOWING
class()exists()is.function()conflicts()get()rm()FREQUENTLY ASKED QUESTIONS
Why does R find a function when my variable does not exist?
Name lookup walks outward through enclosing environments and then along the search path of attached packages. It stops at the first match. If nothing local matches df, stats::df is a legitimate binding and lookup succeeds with the wrong thing.
Which names should I avoid for data frames?
df, data, c, t, rev, max, min and sum all exist as functions in base R or stats. Descriptive names such as sales, monthly_returns or customer_orders avoid the collision and read better.
Why do I get this in Shiny when the same code works in the console?
Reactive expressions are functions and must be called. Inside renderPlot() you need my_data() rather than my_data; the latter refers to the reactive itself, which is a closure.
Still stuck?
Paste your code in RChat and the AI will fix this error in context.