How to Solve R Error: object of type ‘closure’ is not subsettable

by | Programming, R, Tips

If you try to index a function using square brackets [] or the dollar sign operator $, you will raise the error: object of type ‘closure’ is not subsettable.

This error typically occurs when using square brackets to subset a function mistaken for a subsettable object like a data.frame or a vector.

You can solve this error by not naming variables after base R-functions.


Example #1: Using Square Brackets to Call a Function

Let’s look at an example of a function that returns the square of a number:

square_function <- function(x) {
 x <- x ^ 2
 return(x)
}

Next, we will attempt to call the function and pass a number to it as an argument.

print(square_function[5])

Let’s run the code to see what happens:

Error in square_function[5] : object of type 'closure' is not subsettable

The error occurs because we used square brackets instead of parentheses to call the function. The R interpreter considers this expression to be a subsetting operation. We can verify that square_function is a type closure object using the typeof() function:

print(typeof(square_function))
[1] "closure"

Solution

We need to replace the square brackets with parentheses to solve this error. Let’s look at the revised code:

square_function <- function(x) {
 x <- x ^ 2
 return(x)
}

print(square_function(5))

Let’s run the code to get the result.

[1] 25

Example #2: Reactive Data Frames Using shiny

This error can occur when accessing reactive data frames using the shiny package. Reactive expressions cache their values and update the values when they become outdated. Let’s look at how to reproduce the error with reactive data frames. First, we will install and load the shiny package:

install.packages('shiny')
library('shiny')

Next, we will define a reactive data frame:

df <- reactive({
data.frame(x = c(1, 3, 5),
y = c(2, 4, 6))
})

Next, we will use the isolate function to retrieve the values under the column name x:

isolate({
print(df)
print(df$x)
})

Let’s run the code to see what happens:

Error in df$x : object of type 'closure' is not subsettable

The error occurs because df is not a data frame but is a function that returns a data frame. Therefore when we try to index df using the dollar sign ($) operator, R interprets this as trying to subset a function.

Solution: Use parentheses

To solve this error, we need to call the df function by putting parentheses at the end of the function name. Calling the function will return a data frame, which we can subset. Let’s look at the revised code:

isolate({
print(df())
print(df()$x)
})

Let’s run the code to see the result:

  x y
1 1 2
2 3 4
3 5 6
[1] 1 3 5

We successfully retrieved the values under column x.

Summary

Congratulations on reading to the end of this tutorial! The error object of type ‘closure’ is not subsettable occurs when you try to index a function like a list, data.frame, or vector. When calling a function, you must use parentheses, and it is good practice to give variables unique names that differ from built-in functions. When creating reactive functions that return subsettable objects, you have to call the reactive function first using parentheses before you can subset the underlying object.


For further reading on R related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨