【发布时间】:2021-03-09 19:47:33
【问题描述】:
以下自定义函数以前有效,请参阅here,但它似乎不再有效:
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
必须有一些更新,因为该解决方案导致了问题。我试图用{{ }} 避免enquo() 和!!。
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
谢谢
编辑这是与dplyr bug 报告的错误
【问题讨论】:
-
不确定发生了什么,但可能与
group_by()有关。如果您在mutate()中创建“月”变量,然后分组而不是直接使用group_by(),事情似乎有效:mutate(month = floor_date({{date_varaible}}, "month")) %>% group_by(month) %>% ... -
@aosmith bug github.com/tidyverse/lubridate/issues/959