mutate_impl(.data,dots)出错:评估错误:未使用的参数

时间:2018-04-08 20:11:55

标签: r tidyverse mutate

我正在测试此代码实现一个函数来返回带有xts数据集的日历图表:

xts_heatmap <- function(x){
  data.frame(Date=as.Date(index(x)), x[,1]) %>%
    setNames(c("Date","Value")) %>%
    dplyr::mutate(
      Year=lubridate::year(Date),
      Month=lubridate::month(Date),
      # I use factors here to get plot ordering in the right order
      # without worrying about locale
      MonthTag=factor(Month,levels=as.character(1:12),
                      labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE),
      # week start on Monday in my world
      Wday=lubridate::wday(Date,week_start=1),
      # the rev reverse here is just for the plotting order
      WdayTag=factor(Wday,levels=rev(1:7),labels=rev(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE),
      Week=as.numeric(format(Date,"%W"))
    ) %>%
    # ok here we group by year and month and then calculate the week of the month 
    # we are currently in
    dplyr::group_by(Year,Month) %>% 
    dplyr::mutate(Wmonth=1+Week-min(Week)) %>% 
    dplyr::ungroup() %>% 
    ggplot(aes(x=Wmonth, y=WdayTag, fill = Value)) + 
    geom_tile(colour = "white") + 
    facet_grid(Year~MonthTag) + 
    scale_fill_gradient(low="red", high="yellow") +
    labs(x="Week of Month", y=NULL)
}

用法:

quantmod::getSymbols("^VIX",src="yahoo")
xts_heatmap(Cl(VIX)) + labs(title="Heatmap of VIX")

错误:

Error in mutate_impl(.data, dots) : 
  Evaluation error: unused argument (week_start = 1).

寻找可能的解决方案,发现.data和.env现在是dplyr动词(link)中的保留字,但我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

根据我的经验,这通常是由于基本函数未被dplyrlubridate掩盖所致,因为您从未使用过library()调用。

使用R版本3.5 如果您引入库,然后直接引用函数,则最新版本的tidyverse可以很好地运行该代码

library(tidyverse) # brings in ggplot2 and dplyr together
library(lubridate)
library(quantmod)

xts_heatmap <- function(x){
  data.frame(Date=as.Date(index(x)), x[,1]) %>%
    setNames(c("Date","Value")) %>%
    mutate(
      Year  = year(Date),
      Month = month(Date),
      # I use factors here to get plot ordering in the right order
      # without worrying about locale
      MonthTag=factor(Month,levels=as.character(1:12),
                      labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE),
      # week start on Monday in my world
      Wday= wday(Date,week_start=1),
      # the rev reverse here is just for the plotting order
      WdayTag=factor(Wday,levels=rev(1:7),labels=rev(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE),
      Week=as.numeric(format(Date,"%W"))
    ) %>%
    # ok here we group by year and month and then calculate the week of the month 
    # we are currently in
    group_by(Year,Month) %>% 
    mutate(Wmonth=1+Week-min(Week)) %>% 
    ungroup() %>% 
      ggplot(aes(x=Wmonth, y=WdayTag, fill = Value)) + 
      geom_tile(colour = "white") + 
      facet_grid(Year~MonthTag) + 
      scale_fill_gradient(low="red", high="yellow") +
      labs(x="Week of Month", y=NULL)
}


getSymbols("^VIX",src="yahoo")
xts_heatmap(Cl(VIX)) + labs(title="Heatmap of VIX")

your plot!