5

Similar to this: pass function arguments to both dplyr and ggplot

I have in the past used !!sym() without any rememberable headache.

However, now I get an error when I try this:

library(rlang)
library(tidyverse)


diamond_plot <- function (data, group, metric) {
    quo_group  <- sym(group)
    quo_metric <- sym(metric)

    data %>%
        group_by(!! quo_group) %>%
        summarise(price = mean(!! quo_metric)) %>%
        ggplot(aes(x = !! quo_group, y = !! quo_metric)) +
        geom_col()
}

diamond_plot(diamonds, "clarity", "price")

The error is

Error in !quo_group : invalid argument type

Did I miss something?

Tung
  • 26,371
  • 7
  • 91
  • 115
alice m w
  • 151
  • 6
  • The code works for me. Have you tried updating `rlang` and all `tidyverse` packages? – Tung May 23 '18 at 18:11
  • 1
    What version of `ggplot` are you using? It has only been very recently updated to support `!!` inside `aes()` calls. You currently need the development version. – MrFlick May 23 '18 at 18:28
  • Thanks - I have updated everything - and now it works. ggplot2_2.2.1.9000 is ggplot2 version – alice m w May 23 '18 at 18:38

0 Answers0