Home > Software design >  R Shiny Error in group_by: Must group by variables found in `.data`
R Shiny Error in group_by: Must group by variables found in `.data`

Time:06-22

I am trying to create a shiny app that:

  1. Let's the user upload a CSV file
  2. Select independent (year or month column) and dependent variables (tmean/TMin/TMax columns)
  3. Based on the selected variables, the app will return a regression summary.
  4. Based on the selected variables, the app will also return a geom_line plot with the regression/slope line.

However I get this error:

Warning: Error in group by: Must group by variables found in `.data`.
x Column `2010_2021` is not found.

How can I fix this?

Data:

The file can be downloaded from enter image description here

CodePudding user response:

Several points:

  • you need .data only in the cases where you want to access data via a variable that contains a string, if the name of the symbol (variable) is the variable name in the data.frame, you don't need .data
  • typo, you've forgot the after ggplot
  • to avoid errors due to uninitialised inputs, use a req in your plot rendering
output$RegPlots = renderPlotly({
  req(mydf(), input$independent, input$dependent)
  
  mydf() %>%   group_by(year) %>% 
    summarise(avgTemp = mean(.data[[input$independent]])) %>% 
    ungroup %>% 
    ggplot(aes(x = .data[[input$dependent]], y = avgTmean))  
    geom_point(color = "deepskyblue4")  
    geom_smooth(method = lm, linetype = 2, color = "red4", se = F)  
    theme(text = element_text(size = 16))  
    xlab("Year")  
    ylab("Temperature (C)")  
    ggtitle("1980-2021 Regression Plot")
})
  • Related