如何在r dygraphs中使用dyShading来遮蔽周末期?
我使用下面的代码,但是有一个简单的方法吗?
library(lubridate)
library(xts)
library(dygraphs)
dateP<-seq(as.POSIXct("2015-01-01 "), as.POSIXct("2015-02-01 "), by="days")
toto<-rep(6,length(dateP))
tutu<-xts(toto, order.by=dateP)
dateWD<-dateP[isWeekend(dateP, wday = 0:4)]
ok_periods<-0
i=1
j=1
while (i<(length(dateWD)-1)){
ok_periods[j] <- list(list(from = dateWD[i], to = dateWD[i+1]))
i=i+2
j=j+1
}
add_shades<-function(x,periods){
for(period in periods){
x<-dyShading(x, from=period$from,to=period$to)
}
x
}
dygraph(tutu) %>%
add_shades(ok_periods)