Select Page

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!