Skip to content

Commit 5be35ae

Browse files
committed
refactor: minor clean-up
1 parent e2c158b commit 5be35ae

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

R/interest-rate.R

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ tr_yield_curve <- function(date = NULL) {
2525
if (is.null(dt)) {
2626
return()
2727
}
28-
dt <- clean_yield_curve(dt)
29-
dt
28+
clean_yield_curve(dt)
3029
}
3130

3231
parse_yield_curve <- function(x) {
@@ -82,8 +81,7 @@ tr_bill_rates <- function(date = NULL) {
8281
if (is.null(dt)) {
8382
return()
8483
}
85-
dt <- clean_bill_rates(dt)
86-
dt
84+
clean_bill_rates(dt)
8785
}
8886

8987
parse_bill_rates <- function(x) {
@@ -103,8 +101,8 @@ clean_bill_rates <- function(dt) {
103101
dt[, type := gsub("round_b1_", "", tolower(type), fixed = TRUE)]
104102
dt[, type := gsub("_2$", "", type)]
105103
dt[, maturity := strsplit(type, "_", fixed = TRUE)]
106-
dt[, type := vapply(maturity, `[[`, NA_character_, 1L)]
107-
dt[, maturity := vapply(maturity, `[[`, NA_character_, 2L)]
104+
dt[, type := vapply(maturity, `[[`, "", 1L)]
105+
dt[, maturity := vapply(maturity, `[[`, "", 2L)]
108106
dt[, maturity := gsub("wk", " weeks", maturity, fixed = TRUE)]
109107
dt[, c("date", "type", "maturity", "value")]
110108
}
@@ -134,8 +132,7 @@ tr_long_term_rate <- function(date = NULL) {
134132
if (is.null(dt)) {
135133
return()
136134
}
137-
dt <- clean_long_term_rate(dt)
138-
dt
135+
clean_long_term_rate(dt)
139136
}
140137

141138
parse_long_term_rate <- function(x) {
@@ -176,16 +173,15 @@ clean_long_term_rate <- function(dt) {
176173
#' \donttest{
177174
#' # get data for a single month
178175
#' tr_real_yield_curve("202201")
179-
#' # or for the entire year
180176
#' tr_real_yield_curve(2022)
177+
#' # or for the entire year
181178
#' }
182179
tr_real_yield_curve <- function(date = NULL) {
183180
dt <- treasury("daily_treasury_real_yield_curve", date, parse_real_yield_curve)
184181
if (is.null(dt)) {
185182
return()
186183
}
187-
dt <- clean_real_yield_curves(dt)
188-
dt
184+
clean_real_yield_curves(dt)
189185
}
190186

191187
parse_real_yield_curve <- function(x) {

R/yield-curve.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tr_curve_rate <- function(
4343
type <- match.arg(type)
4444
start_year <- switch(x, hqm = 1984L, tnc = 1978L, trc = 2003L, tbi = 2003L)
4545
x <- if (type == "monthly") x else paste0(x, "eom")
46-
years <- seq.int(start_year, 2027L, by = 5L)
46+
years <- seq(start_year, 2027L, by = 5L)
4747
if (!is.null(year)) {
4848
years <- years[findInterval(year, years)]
4949
}
@@ -58,7 +58,7 @@ tr_curve_rate <- function(
5858
if (year >= 2023L && !startsWith(x, "hqm")) "xlsx" else "xls"
5959
)
6060
},
61-
NA_character_
61+
""
6262
)
6363
if (x == "hqmeom") {
6464
urls <- sub("88\\.xls$", "88_0.xls", urls)
@@ -75,8 +75,8 @@ tr_curve_rate <- function(
7575
nms <- paste(months, years, sep = "-")
7676
nms <- c("maturity", "tmp", nms)
7777
nms
78-
}) |>
79-
setDT()
78+
})
79+
dt <- setDT(dt)
8080
dt[, 2L := NULL]
8181
dt[, names(.SD) := lapply(.SD, as.numeric), .SDcols = is.logical]
8282
dt <- melt(
@@ -165,8 +165,8 @@ download_data <- function(x, col_names, skip, names_to, values_to) {
165165
tf <- tempfile()
166166
on.exit(unlink(tf), add = TRUE)
167167
utils::download.file(url, destfile = tf, quiet = TRUE, mode = "wb")
168-
dt <- readxl::read_excel(tf, col_names = col_names, skip = skip) |>
169-
setDT()
168+
dt <- readxl::read_excel(tf, col_names = col_names, skip = skip)
169+
dt <- setDT(dt)
170170
dt[, 2L := NULL]
171171
dt <- melt(
172172
dt,

0 commit comments

Comments
 (0)