Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is “object of type ‘closure’ is not subsettable” error in Shiny?

Tags:

r

shiny

I have a shiny app and when I run it I get an error saying that an object of type ‘closure’ is not subsettable. What is that and how can I fix it?

Note: I wrote this question as this comes up a lot, and the possible dupes are either not shiny related or so specific that it is not obvious that the answers are broadly applicable.

like image 912
John Paul Avatar asked Nov 16 '16 03:11

John Paul


People also ask

How do you fix an object of type closure is not Subsettable?

The solution to object of type' closure' is not subsettable The major issue is subsetting the function that means profit is a function, and you are calling profit[i] .

What does it mean when object of type closure is not Subsettable?

A common error in R is object of type 'closure' is not subsettable . This message means that you have a variable which represents a function, and you're mistakenly using square brackets to try and subset it, thinking that it represents a data. frame or vector or something e.g.

What is a closure type in R?

Closures are used for creating functions within a function in the R environment. These are useful when the functions are changing/ repeating in the code with the same dataset.


1 Answers

See also this question which covers this error in a non-Shiny context.

How to fix this:

This is a very common error in shiny apps. This most typically appears when you create an object such as a list, data.frame or vector using the reactive() function – that is, your object reacts to some kind of input. If you do this, when you refer to your object afterwards, you must include parentheses.

For example, let’s say you make a reactive data.frame like so:

MyDF<-reactive({ code that makes a data.frame with a column called “X” })

If you then wish to refer to the data.frame and you call it MyDF or MyDF$X you will get the error. Instead it should be MyDF() or MyDF()$X You need to use this naming convention with any object you create using reactive().

Why this happens:

When you make a reactive object, such as a data.frame, using reactive() it is tempting to think of it as just like any other non-reactive data.frame and write your code accordingly. However, what you have created is not really a data.frame. Rather, what you have made is instructions, in the form of a function, which tell shiny how to make the data.frame when it is needed. When you wish to actually use this function to get the data.frame you have to use the parenthesis, just like you would any other function in R. If you forget to use the parenthesis, R thinks you are trying to use part of a function and gives you the error. Try typing:

plot$x

at the command line and you will get the same error.

You may not see this error right when your app starts. Reactive objects have what is called “lazy” evaluation. They are not evaluated until they are needed for some output. So if your data.frame is only used to make a plot, the data.frame will not exist until the user sees the plot for the first time. If when the app starts up the user is required to click a button or change tabs to see the plot, the code for the data.frame will not be evaluated until that happens. Once that happens, then and only then will shiny use the current values of the inputs to run the function that constructs the data.frame needed to make the plot. If you have forgotten to use the parentheses, this is when shiny will give you the error. Note that if the inputs change, but the user is not looking at the plot, the function that makes the data.frame will not be re-run until the user looks at the plot again.

like image 144
John Paul Avatar answered Sep 28 '22 02:09

John Paul