From e764b1aec411639c89468930fbc83d6185ba61f3 Mon Sep 17 00:00:00 2001 From: Frederik Vanrenterghem Date: Mon, 9 May 2016 08:00:49 +0800 Subject: [PATCH] Initial commit. Depends RCurl (not Imports). --- .Rbuildignore | 2 ++ .gitignore | 4 ++++ DESCRIPTION | 10 ++++++++++ NAMESPACE | 1 + R/getRemainingOpDays.R | 34 ++++++++++++++++++++++++++++++++++ operatingdays.Rproj | 20 ++++++++++++++++++++ 6 files changed, 71 insertions(+) create mode 100644 .Rbuildignore create mode 100644 .gitignore create mode 100644 DESCRIPTION create mode 100644 NAMESPACE create mode 100644 R/getRemainingOpDays.R create mode 100644 operatingdays.Rproj diff --git a/.Rbuildignore b/.Rbuildignore new file mode 100644 index 0000000..91114bf --- /dev/null +++ b/.Rbuildignore @@ -0,0 +1,2 @@ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata diff --git a/DESCRIPTION b/DESCRIPTION new file mode 100644 index 0000000..03aa672 --- /dev/null +++ b/DESCRIPTION @@ -0,0 +1,10 @@ +Package: operatingdays +Type: Package +Title: Return Number of Operating Days in Month +Version: 0.1.0 +Author: Frederik Vanrenterghem +Maintainer: Frederik Vanrenterghem +Description: Return the number of operating days in a given month. Currently returns remaining days in current month. +Depends: RCurl +License: GPL-3 +LazyData: TRUE diff --git a/NAMESPACE b/NAMESPACE new file mode 100644 index 0000000..d75f824 --- /dev/null +++ b/NAMESPACE @@ -0,0 +1 @@ +exportPattern("^[[:alpha:]]+") diff --git a/R/getRemainingOpDays.R b/R/getRemainingOpDays.R new file mode 100644 index 0000000..9325670 --- /dev/null +++ b/R/getRemainingOpDays.R @@ -0,0 +1,34 @@ +# getRemainingOpDays +# This is a function that returns the remaining operating days in a month +# It takes as arguments the country, state, date for which the request is made. +# It returns a number +# It currently only supports AU (Australia) + +getRemainingOpDays <- function(country,state,date) { + cutoff.time = "15:00" + + # open dataset with Australian public holidays for 2016-2017 + URL <- "http://data.gov.au/dataset/b1bc6077-dadd-4f61-9f8c-002ab2cdff10/resource/a24ecaf2-044a-4e66-989c-eacc81ded62f/download/australianpublicholidays-201617.csv" + x <- RCurl::getURL(URL) + public.holidays <- read.csv(textConnection(x)) + + # limit to public holidays observed in the state (including national holidays) + state.public.holidays <- public.holidays[public.holidays$Applicable.To %in% c(state,"NAT"),] + state.public.holidays$Date<-as.Date(state.public.holidays$Date,"%Y%m%d") + + today <- Sys.Date() + startdate <- as.Date(cut(date, "month")) + enddate <- seq(startdate, by = "1 months", length.out = 2)[2] + days.in.month <- as.data.frame(seq(startdate,enddate-1,by = "1 day")) + colnames(days.in.month)<- c("Date") + # Add number of the weekday (1 to 7) to each day + days.in.month$weekdays <- mapply(function(x) strftime(x,"%u"),days.in.month) + # Saturday and Sunday are days 6 and 7 and non-working days + days.in.month$WorkingDay <- mapply(function(x) ifelse(x %in% c(6,7),"0","1"),days.in.month$weekdays) + # Overwrite working day flag if a working day is a public holiday, leaving as-is otherwise + days.in.month$WorkingDay <- mapply(function(x,y) ifelse(x %in% state.public.holidays$Date & identical(y,"1"),"0",y), days.in.month[,1],days.in.month$WorkingDay) + + # if run before cutoff time, count today as remaining working day + remaining.operating.days.in.month <- ifelse(strftime(Sys.time(), "%H:%M") > cutoff.time,sum(days.in.month$WorkingDay==1 & days.in.month$Date>today),sum(days.in.month$WorkingDay==1 & days.in.month$Date>=today)) + return(remaining.operating.days.in.month) +} diff --git a/operatingdays.Rproj b/operatingdays.Rproj new file mode 100644 index 0000000..497f8bf --- /dev/null +++ b/operatingdays.Rproj @@ -0,0 +1,20 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source -- 2.30.2