简体   繁体   中英

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

I'm testing this code that implement a function to return a calendar chart with a xts dataset:

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)
}

The usage:

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

The error:

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

Looking for a possible solution, discover that .data and .env are now reserved words in dplyr verbs ( link ), but I don't know how to fix this.

In my experience this is normally caused by base functions not being masked by dplyr or lubridate because you never use your library() calls.

Using R version 3.5 and the latest version of tidyverse this code runs fine if you bring in the library, and then reference your functions directly

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")

你的阴谋!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM