'Group by different column names in a df using in a for loop

I'm new to R, recently I tried to use a for loop to put every different col names in a group by statement, then make a graph accordingly. However I got this error

Error: Must group by variables found in .data.

*Column i is not found.

below is the code, I kinda simplify the problem here, only kept the group by part, does anyone know how I can solve this?

for (i in colnames(mtcars)) {
  mtcars %>%
    group_by(i) %>%
    summarise((total=n())
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source