9

I'm trying to use chron's is.holiday() function, but I'm having trouble getting it to work. The documentation says to modify the .Holiday object with the holidays that you wish to use, but the changes I've made to .Holiday don't seem to be detected by is.holiday(). Could someone provide an example of the appropriate way to load holidays?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
matt_k
  • 4,139
  • 4
  • 27
  • 33

3 Answers3

9

This is non-trivial and probably deserves to be referred to the chron maintainer as a bug.

library(chron)
library(timeDate)
hlist <- c("CAVictoriaDay","CACanadaDay","CALabourDay")
(ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))
.Holidays <- ss

(Someone who actually works with dates in R more often than I do probably has a more elegant solution for the preceding stuff, without that double-sapply thing ...)

But this doesn't change the important thing, which is the version of Holidays in the chron namespace:

chron::.Holidays  ## no change

The clue is here: Override a function that is imported in a namespace

Namespace magic:

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

Now look, and it has worked:

chron::.Holidays

Test it out:

yrvec <- seq.Date(as.Date("2011-01-01"),
                    as.Date("2011-12-31"),by="day")
plot(is.holiday(yrvec),axes=FALSE)
axis.Date(side=1,yrvec)
Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks. That was significantly more complicated than I expected. – matt_k Sep 18 '11 at 02:11
  • The solution above is fantastic, and works, with the exception that there seems to an error if hlist includes "GBNewYearsEve" I've started a new question on that [link] (http://stackoverflow.com/questions/26777282/in-using-timedate-r-package-i-receive-an-error-when-specifying-gbnewyearseve) – DaveRGP Jan 21 '15 at 09:25
2

I was trying to do the same thing and found this older post. I didn't need to modify .Holidays:

library(timeDate);library(chron)
hlist <- c("USChristmasDay","USGoodFriday","USIndependenceDay","USLaborDay",
    "USNewYearsDay","USThanksgivingDay")        
myholidays  <- dates(as.character(holiday(2000:2013,hlist)),format="Y-M-D")

> is.holiday(as.Date("2013-11-28"),myholidays)

[1] TRUE

> chron::.Holidays
  New Years Day     Memorial Day Independence Day        Labor Day     Thanksgiving        Christmas 
    01/01/92         05/25/92         07/04/92         09/07/92         11/26/92         12/25/92 
ideamotor
  • 856
  • 1
  • 7
  • 23
2

After looking a lot about this topic, I found this solution particularly simple using the package RQuantLib, so maybe RQuantLib might be more simple in this regard.

install.packages("RQuantLib")
library(RQuantLib)
isBusinessDay(calendar="WeekendsOnly", dates=yourdatesofinterest)

You can modify this code with different calendars to add to the weekends different sets of holidays in different countries (below just an example, but they have many more).

isBusinessDay(calendar="UnitedStates", dates=yourdatesofinterest)
isBusinessDay(calendar="UnitedStates/Settlement", dates=yourdatesofinterest)
isBusinessDay(calendar="UnitedStates/NYSE", dates=yourdatesofinterest)
isBusinessDay(calendar="Sweden", dates=yourdatesofinterest)
isBusinessDay(calendar="Mexico", dates=yourdatesofinterest)

I hope it helps somebody

Ivan
  • 759
  • 10
  • 18