Search code examples
rrlang

curly curly Error: Base operators are not defined for quosures


The following custom function worked before, see here, but it doesn't seem to work anymore:

library(tidyverse)
library(rlang)
library(scales)
data1 <- data.frame(date1 = sample(seq(as.Date("2005-01-01"), as.Date('2018-01-01'), by="day"), 5),
                    num1 = 1:5)
data1
hist_date_f <- function(df, date_varaible) {
    df %>%
      group_by(month = floor_date({{date_varaible}}, "month")) %>%
      dplyr::summarize(freq = n()) %>%  
      ggplot(aes(x = month, y = freq)) + 
      geom_bar(stat = "identity") + 
      scale_x_date(labels = date_format("%m-%Y"), 
                   breaks = date_breaks ("1 month")) + 
      theme_bw()
}

hist_date_f(data1, date1)
# Error: Base operators are not defined for quosures.
# Do you need to unquote the quosure?

#   # Bad:
#   myquosure == rhs

#   # Good:
#   !!myquosure == rhs

There must be some update since that solution causing the issue. I'm trying to avoid enquo() and !! with {{ }}.

sessionInfo()
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     

# other attached packages:
#  [1] scales_1.1.1    rlang_0.4.10    forcats_0.5.0   stringr_1.4.0  
#  [5] dplyr_1.0.3     purrr_0.3.4     readr_1.4.0     tidyr_1.1.2    
#  [9] tibble_3.0.5    ggplot2_3.3.3   tidyverse_1.3.0 tidylog_1.0.2

thanks

EDIT this is a reported bug with dplyr bug


Solution

  • We could convert to symbol and evaluate (!!)

    hist_date_f <- function(df, date_varaible) {
       df %>%
        group_by(month = floor_date(!! ensym(date_varaible), "month")) %>%
        dplyr::summarize(freq = n()) %>%  
        ggplot(aes(x = month, y = freq)) + 
       geom_bar(stat = "identity") + 
       scale_x_date(labels = date_format("%m-%Y"), 
                   breaks = date_breaks ("1 month")) + 
      theme_bw()
     }
    

    -testing

    hist_date_f(data1, date1)
    

    enter image description here