The error is actually true because in the df I do not have values for col1 and col2 either for the selection I am making from the app. Since, this aggregate is a part of shiny app and I would not like my app to run into error/crash rather I would like it to keep running.
How do I handle this error? So that my app does not crash?
One approach could be to add a check before you use aggregate(). First, filter df to only those cases where values exist for col1 and col2. If the result has at least one row, proceed. It could look something like below.