object of type ‘closure’ is not subsettable

ItsMyCode |

If you are working on R, a standard error message that you have faced is R is the *object of type ‘closure’ is not subsettable. *

What does object of type ‘closure’ is not subsettable mean?

The error occurs when you try to use indexing on a function (is not a function but a reactive variable) with the name instead of value.

To make it further simple, if you have a variable representing a function and you’re mistakenly using square brackets to try and subset it thinking it represents a data.frame or vector.

Example :

mean[1]
## Error in mean[1] : object of type 'closure' is not subsettable
mean[[1]]
## Error in mean[[1]] : object of type 'closure' is not subsettable
mean$a
## Error in mean$a : object of type 'closure' is not subsettable

So whenever you try to subset a function, you will get this error( accessing an item within a function using $) which is totally wrong and meaningless in R.

Also, as a matter of fact, you should avoid naming the variables after base R-functions. (Calling variables data is the most common reason for this issue).

If you’re running into this problem in shiny, the most likely cause is that you’re trying to work with a reactive expression without calling it as a function using parentheses.

library(shiny)
reactive_df <- reactive({
    data.frame(col1 = c(1,2,3),
               col2 = c(4,5,6))
})

While we often work with reactive expressions in shiny as if they were data frames, they are actually functions that return data frames (or other objects).

isolate({
    print(reactive_df())
    print(reactive_df()$col1)
})
  col1 col2
1 1 4
2 2 5
3 3 6
[1] 1 2 3

But if we try to subset it without parentheses, then we’re actually trying to index a function, and we get an error:

isolate(
    reactive_df$col1
)
Error in reactive_df$col1 : object of type 'closure' is not subsettable

Another possible cause of object of type’ closure’ is not subsettable

There are several related errors if you are trying to subset operators or keywords. Let’s take a few examples

`+`[1]
## Error in `+`[1] : object of type 'builtin' is not subsettable
`if`[1]
## Error in `if`[1] : object of type 'special' is not subsettable

If you look at the above example + is an operator and if is a special type in R, you cannot subset operator and reserved keywords.

The solution to object of type’ closure’ is not subsettable

Here is a classic example from Stackoverflow, when you call the Profit function and pass the value as 10000 R will throw an error. The major issue is subsetting the function that means profitis a function, and you are calling profit[i].

nsims=1000
sim.demand=rep(NA,nsims)
  for(i in 1:nsims){
  sim.demand[i]=rnorm(12, 12000, sd=3496.752) 
}

profit <- function(n)
 for(i in 1:1000){
   if(sim.demand[i]<=n) 
     profit[i]=-100000-(80*n)+100*sim.demand[i]+30*(n-sim.demand[i]) else
     profit[i]=-100000-(80*n)+100*n
}

# Output 
Error in profit[i] = -1e+05 - (80 * n) + 100 * n : 
object of type 'closure' is not subsettable

To resolve the issue create a new variable( return_profit ) and return the variable at the end of the function as shown below.

nsims=1000
sim.demand=rep(NA,nsims)
  for(i in 1:nsims){
  sim.demand[i]=rnorm(12, 12000, sd=3496.752) 
}

profit <- function(n){
  return_profit<-rep(NA, 1000)
  for(i in 1:1000){
    if(sim.demand[i]<=n) {
      return_profit[i]=-100000-(80*n)+100*sim.demand[i]+30*(n-sim.demand[i])
    } 
    else{
      return_profit[i]=-100000-(80*n)+100*n
    }
  }
  return_profit
}

20