I think its bad practice to reuse (a very generic name) for different purposes.
What is .data ? A parameter sent into your module ? Or a name of a reactive defined by your module.
In the definition you are necessarily referring to the param sent in, was that a reactive ? If it was you would need to acess the value via brackets in the standard way. .data()
The error message clearly shows that R thinks you are trying to filter a reactive.. and not a data.frame. First step is to edit your names to disambiguate them, as i said previously
As @nirgrahamuk points out, the issue stems from name collision.
Interestingly, even if you passed .data() (the content of the reactive expression) to dplyr::filter(), you would face infinite recursion (the reactive expression reacts to itself) and Shiny would throw an error.
To avoid this, disentangle the name of the reactive expression from the name of the dataframe (which would remain .data in the following example), by using a different name (e.g. filtered_data) :
Thanks, I understand how to solve the error. I'm more trying to wrap my head around the why/philosophy behind the need to separate the names .data and filtered_data.
When I look at the simple function, I think of the process acting linearly, once through. Is the difference that a server is live? So that .data becomes a reactive, and cannot be filtered on? What exactly happens step by step in the server that causes the error?
why/philosophy behind the need to separate the names .data and filtered_data
The short answer is because they are different things:.data is a dataframe, while filtered_data is a reactive. A reactive is a function. This function is called each time something reactive inside its definition gets invalidated (e.g. a change in input$year).
However, from your original post, it seems you are expecting .data to always be a dataframe, even when you defined the name of your reactive as .data as well.
R doesn't know your intentions and just looks for an object called .data which in your case is the reactive itself. However, as you are just using the expression and not its result (by accessing it with ()) it throws an error. Even if you used .data() this would cause infinite recursion.
When I look at the simple function, I think of the process acting linearly, once through. Is the difference that a server is live? So that .data becomes a reactive, and cannot be filtered on? What exactly happens step by step in the server that causes the error?
Maybe we can illustrate this without even refering to Shiny and reactivity.
Imagine this simplification of your case, and please take a look at the comments in the code I made:
# Your dataframe (i.e. original .data)
.data <- 1
# Your reactive expression (which you also named .data)
.data <- function() {
# What do you think this `.data` object (right below this comment) will be when you run this function? A numeric or a function?
.data + 1
}
# What do you think will happen when you run this?
.data()
Hope this helps
Cheers!
This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.
That was very helpful, thanks agus! Thinking of reactive() as a function solved my confusion. Following your last example, I believe the server function in my original post is like the below