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

The error “object of type ‘closure’ is not subsettable” occurs when you “try to subset a function in R”. The error occurs because functions cannot be accessed or subsetted like other objects, such as vectors or data frames.

Reproduce the error

data_func <- function(x) {
  x <- x * 2
  return(x)
}

# define data
data <- c(21, 31, 19, 46, 52)

data_func[1]

Output

Error in data_func[1] : object of type 'closure' is not subsettable

We receive an error because it’s impossible to subset an object of type “closure” in R.

How to fix it?

To fix the Error: object of type ‘closure’ is not subsettable in R, “avoid trying to access or subset the function object”. Instead, you can use the function directly by calling it with the appropriate arguments.

data_func <- function(x) {
  x <- x * 2
  return(x)
}

# define data
data <- c(21, 31, 19, 46, 52)

data_func(data[1])

In general, the “object of type ‘closure’ is not subsettable” error occurs when you try to treat a function like a vector or other type of object that can be accessed or subsetted.

To avoid this error, you should call functions directly by using their names and providing the necessary arguments.

That’s it.

Leave a Comment