In using timeDate R package, I receive an error wh

2019-05-24 20:57发布

问题:

IN looking for a way to modify the .Holiday object in the chron package I discovered this solution How to define holidays for is.holiday() chron package in R

Which works very well in itself, except when I include "GBNewYearsEve" in hlist, I recieve an error:

 Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'GBNewYearsEve' of mode 'function' was not found

This error doesn't appear if GBNewYearsEve is removed from the list. What have I missed?

Example Working Code:

library(chron)
library(timeDate)
hlist <- c("GBMayDay", "GBBankHoliday", "GBSummerBankHoliday", "ChristmasEve", "ChristmasDay", "BoxingDay", "NewYearsDay")
(ss <- dates(sapply(sapply(hlist,holiday,year=(c(2011)),as.Date)))
.Holidays <- ss

chron::.Holidays ##nochange

unlockBinding(".Holidays", as.environment("package:chron"))
assignInNamespace(".Holidays", .Holidays, ns="chron", 
                  envir=as.environment("package:chron"))
assign(".Holidays", .Holidays, as.environment("package:chron"))
lockBinding(".Holidays", as.environment("package:chron"))

chron::.Holidays ##change

Example non-working code:

hlist <- c("GBMayDay", "GBBankHoliday", "GBSummerBankHoliday", "ChristmasEve", "ChristmasDay", "BoxingDay", "NewYearsDay", "GBNewYearsEve")
(ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))

回答1:

Not sure this is an answer that will suit you. I was curious with your problem and I've downloaded the timeDate package from CRAN. Although it seems to be documented in ?holiday, I don't think the code is ready for GBNewYearsEve.

If I run your code as it is I get:

> hlist <- c("GBMayDay", "GBBankHoliday", "GBSummerBankHoliday", "ChristmasEve", "ChristmasDay", "BoxingDay", "NewYearsDay", "GBNewYearsEve")
> 
> (ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  el objeto 'GBNewYearsEve' de modo 'function' no fue encontrado

(Sorry for the mixture of languages, basically the error message is saying that GBNewYearsEve was not found. I actually don't find it in the code of timeDate. However, if I add a definition like this:

GBNewYearsEve =
  function(year = getRmetricsOptions("currentYear")) {
    ans = year*10000 + 1231
    timeDate(as.character(ans)) } 

(Which is basically copied from DENewYearsEve, the only definition for New Years' Eve present in the package)

Then I get your code running:

> (ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))
           GBMayDay       GBBankHoliday GBSummerBankHoliday        ChristmasEve        ChristmasDay           BoxingDay 
           05/02/11            05/30/11            08/29/11            12/24/11            12/25/11            12/26/11 
        NewYearsDay       GBNewYearsEve 
           01/01/11            12/31/11 

However I'm not sure how good a solution is this. Note that in dateTime, some additional transformations are done so that e.g. when the holiday falls in a weekend it is moved to the following day. With the code above, you get just the New Years' Eve on the 31th of December.

For example, this is in holiday-LONDON.R:

        # New Year's Day: if it falls on Sat/Sun, then is
        # moved to following Monday
        posix1 <- as.POSIXlt(NewYearsDay(y))
        if (posix1$wday == 0 | posix1$wday == 6) {
            lon <- timeDate(.on.or.after(y, 1, 1, 1), zone = "London",
                            FinCenter = "Europe/London")
            holidays <- c(holidays, as.character(lon))
        } else {
            holidays <- c(holidays, as.character(posix1))
        }

I guess the package is handling only official holidays for each country, and adding those additional rules?



标签: r chron