object of type ‘closure’ is not subsettable
Tags: python, programming, codenewbie
Published: 2021-09-04 09:41:08

---
title: object of type ‘closure’ is not subsettable
published: true
date: 2021-09-02 09:48:20 UTC
tags: #python #programming #codenewbie
canonical_url: https://itsmycode.com/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 :**

```python
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.

```python
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).

```python
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:

```python
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

```python
`+`[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 `profit `is a function, and you are calling `profit[i]`.

```python
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.

```python

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
}

```

The post [object of type ‘closure’ is not subsettable](https://itsmycode.com/object-of-type-closure-is-not-subsettable/) appeared first on [ItsMyCode](https://itsmycode.com).