dplyr 0.7 - Specify grouping variable as string

2019-08-02 23:55发布

问题:

I have some variable names specified as string (e.g. input from Shiny app) and I would like to use them in my dplyr and ggplot2 code as if they were variables.

I got it to work by trial and error, but I feel like there must be a better way. What is a better way to perform these operations?

library(rlang)
library(ggplot2)
library(dplyr)

someString <- "g1"

df <- tibble(
  g1 = c(1, 1, 2, 2, 2),
  g2 = c(1, 2, 1, 2, 1),
  a = sample(5), 
  b = sample(5)
)

my_summarise <- function(df, group_var) {
  print(group_var)

  df %>%
    group_by(!!group_var) %>%
    summarise(a = mean(a))
}

my_plot <- function(df, group_var) {
  print(group_var)

  ggplot(data = df %>% 
           group_by(!!group_var) %>%
           summarise(a = mean(a)), 
         aes_string(x = quo_name(group_var), y = "a")) +
    geom_bar(stat = "identity")
}

my_summarise(df, quo(UQ(sym(someString))))

my_plot(df, quo(UQ(sym(someString))))

回答1:

Either of these options are probably simpler:

my_summarise <- function(df, group_var) {
  print(group_var)

  df %>%
    #Either works
    group_by_at(.vars = group_var) %>%
    #group_by(!!sym(group_var)) %>%
    summarise(a = mean(a))
}

my_summarise(df,someString)

my_plot <- function(df, group_var) {
  print(group_var)

  ggplot(data = df %>% 
           group_by_at(.vars = group_var) %>%
           #group_by(!!sym(group_var)) %>%
           summarise(a = mean(a)), 
         aes_string(x = group_var, y = "a")) +
    geom_bar(stat = "identity")
}

my_plot(df, someString)

...where you could use either group_by or group_by_at.



回答2:

What about calling with my_summarise(df, as.name(someString))?