Skip to content

Commit

Permalink
lintr related changes
Browse files Browse the repository at this point in the history
no code change intended.
  • Loading branch information
JanMarvin committed Aug 9, 2024
1 parent 07c3f23 commit ba08427
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 246 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^.lintr$
34 changes: 34 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, testing]
pull_request:
branches: [main, testing]

name: lint

permissions: read-all

jobs:
lint:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::lintr, local::.
needs: lint

- name: Lint
run: lintr::lint_package()
shell: Rscript {0}
env:
LINTR_ERROR_ON_LINT: true
8 changes: 8 additions & 0 deletions .lintr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
linters: linters_with_defaults(
line_length_linter = NULL, # we have a few
cyclocomp_linter = NULL, # four cases
commented_code_linter = NULL, # a few cases
object_name_linter = NULL, # a few cases
quotes_linter = NULL, # one case
indentation_linter = NULL
)
10 changes: 5 additions & 5 deletions R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ convert_dt_m <- function(x) {
z <- x / 12 # divide by 12 to create years
mth <- x %% 12 + 1
yr <- 1960 + floor(z)

z <- ifelse(is.na(z), NA, paste0(yr, "-", mth, "-1"))
z <- as.Date(z, "%Y-%m-%d")
z
Expand All @@ -50,13 +50,13 @@ convert_dt_m <- function(x) {
# @param x element to be converted
# @author Jan Marvin Garbuszus \email{jan.garbuszus@@ruhr-uni-bochum.de}
# @author Sebastian Jeworutzki \email{sebastian.jeworutzki@@ruhr-uni-bochum.de}
convert_dt_q <- function(x) {
z <- x / 4
convert_dt_q <- function(x) {
z <- x / 4
yr <- 1960 + floor(z)

qrt <- x %% 4 + 1
qrt_month <- c(1, 4, 7, 10)

z <- ifelse(is.na(z), NA, paste0(yr, "-", qrt_month[qrt], "-1"))
z <- as.Date(z, "%Y-%m-%d")
z
Expand Down
61 changes: 31 additions & 30 deletions R/dbcal.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,26 @@
stbcal <- function(stbcalfile) {

# Otherwise localised dates will be used.
lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")

# Parse full file
stbcal <- file(stbcalfile, "rb")
x <- readLines(stbcal, file.info(stbcalfile)$size)
close(stbcal)

# Dateformat can be ymd, ydm, myd, mdy, dym or dmy
if(any(grepl("dateformat ymd", x)))
if (any(grepl("dateformat ymd", x)))
dateformat <- "%Y%b%d"
if(any(grepl("dateformat ydm", x)))
if (any(grepl("dateformat ydm", x)))
dateformat <- "%Y%d%b"
if(any(grepl("dateformat myd", x)))
if (any(grepl("dateformat myd", x)))
dateformat <- "%b%Y%d"
if(any(grepl("dateformat mdy", x)))
if (any(grepl("dateformat mdy", x)))
dateformat <- "%b%d%Y"
if(any(grepl("dateformat dym", x)))
if (any(grepl("dateformat dym", x)))
dateformat <- "%b%Y%d"
if(any(grepl("dateformat dmy", x)))
if (any(grepl("dateformat dmy", x)))
dateformat <- "%d%b%Y"

# Range of stbcal. Range is required, contains start and end.
Expand All @@ -75,39 +76,39 @@ stbcal <- function(stbcalfile) {
range <- strsplit(range, " ")
rangestart <- range[[1]][2]
rangestop <- range[[1]][3]
range <- seq(from= as.Date(rangestart, dateformat),
to= as.Date(rangestop, dateformat), "days")
range <- seq(from = as.Date(rangestart, dateformat),
to = as.Date(rangestop, dateformat), "days")

# Centerdate of stbcal. Date that matches 0.
centerpos <- grep("centerdate", x)
centerdate <- x[centerpos]
centerdate <- gsub("centerdate ","",centerdate)
centerdate <- gsub("centerdate ", "", centerdate)
centerdate <- as.Date(centerdate, dateformat)

# Omit Dayofweek
omitdayofweekpos <- grep ("omit dayofweek", x)
omitdayofweekpos <- grep("omit dayofweek", x)
omitdayofweek <- x[omitdayofweekpos]

# Mo, Tu, We, Th, Fr, Sa, Su
daysofweek <- weekdays(as.Date(range))

stbcal <- data.frame(range = range, daysofweek=daysofweek)
stbcal <- data.frame(range = range, daysofweek = daysofweek)

# Weekdays every week
if (any(grepl("Mo", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Monday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Monday"] <- NA
if (any(grepl("Tu", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Tuesday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Tuesday"] <- NA
if (any(grepl("We", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Wednesday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Wednesday"] <- NA
if (any(grepl("Th", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Thursday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Thursday"] <- NA
if (any(grepl("Fr", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Friday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Friday"] <- NA
if (any(grepl("Sa", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Saturday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Saturday"] <- NA
if (any(grepl("Su", omitdayofweek)))
stbcal$daysofweek[stbcal$daysofweek=="Sunday"] <- NA
stbcal$daysofweek[stbcal$daysofweek == "Sunday"] <- NA

# Special days to be omitted
if (any(grepl("omit date", x))) {
Expand All @@ -117,21 +118,21 @@ stbcal <- function(stbcalfile) {
omitdates <- gsub("omit date ", "", omitdates)
dates <- as.Date(omitdates, dateformat)

stbcal$daysofweek[which(stbcal$range%in%dates)] <- NA
stbcal$daysofweek[which(stbcal$range %in% dates)] <- NA

# Keep only wanted days stbcal$daysofweek behalten
stbcal <- stbcal[complete.cases(stbcal$daysofweek),]
stbcal <- stbcal[complete.cases(stbcal$daysofweek), ]
}

# In case centerdate is not rangestart:
stbcal$buisdays <- NA
stbcal$buisdays[stbcal$range==centerdate] <- 0
stbcal$buisdays[stbcal$range<centerdate] <- seq(
from=-length(stbcal$range[stbcal$range<centerdate]),
to=-1)
stbcal$buisdays[stbcal$range>centerdate] <- seq(
from=1,
to=length(stbcal$range[stbcal$range>centerdate]))
stbcal$buisdays[stbcal$range == centerdate] <- 0
stbcal$buisdays[stbcal$range < centerdate] <- seq(
from = -length(stbcal$range[stbcal$range < centerdate]),
to = -1)
stbcal$buisdays[stbcal$range > centerdate] <- seq(
from = 1,
to = length(stbcal$range[stbcal$range > centerdate]))

# Add purpose
if (any(grepl("purpose", x))) {
Expand Down Expand Up @@ -165,11 +166,11 @@ stbcal <- function(stbcalfile) {
#' dat$ldatescal2 <- as.caldays(dat$ldate, sp500)
#' all(dat$ldatescal2==dat$ldatescal)
#' @export
as.caldays <- function(buisdays, cal, format="%Y-%m-%d") {
as.caldays <- function(buisdays, cal, format = "%Y-%m-%d") {
rownames(cal) <- cal$buisdays
dates <- cal[as.character(buisdays), "range"]

if(!is.null(format))
if (!is.null(format))
as.Date(dates, format = format)
return(dates)
}
Loading

0 comments on commit ba08427

Please sign in to comment.