Skip to content

Commit b19812e

Browse files
committed
Use package fy
* validate_fys_permitted rather than idiosyncratic use of all_fy etc * Remove some tests that conflict with fy error messages or internal functions
1 parent 9b48e55 commit b19812e

18 files changed

+111
-502
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: grattan
22
Type: Package
33
Title: Australian Tax Policy Analysis
4-
Version: 1.7.1.4
5-
Date: 2019-10-29
4+
Version: 1.8.0.0
5+
Date: 2019-11-02
66
Authors@R: c(person("Hugh", "Parsonage", role = c("aut", "cre"), email = "[email protected]"),
77
person("Tim", "Cameron", role = "aut"),
88
person("Brendan", "Coates", role = "aut"),
@@ -27,6 +27,7 @@ Imports:
2727
rsdmx,
2828
fastmatch,
2929
forecast,
30+
fy (>= 0.2.0),
3031
assertthat (>= 0.1),
3132
magrittr (>= 1.5),
3233
Rcpp (>= 0.12.3),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import(data.table)
8686
importFrom(Rcpp,sourceCpp)
8787
importFrom(fastmatch,"%fin%")
8888
importFrom(fastmatch,fmatch)
89+
importFrom(fy,validate_fys_permitted)
8990
importFrom(hutils,"%ein%")
9091
importFrom(hutils,"%notchin%")
9192
importFrom(hutils,AND)

NEWS.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## 1.8.0.0
2+
3+
### Potentially breaking changes
4+
5+
* Pacakage `fy` is now used for operations involving financial years, notably validation of
6+
input. This has led to changes in error messages (below) as well as the weakening
7+
of some tests to omit checks of class attributes which were never intended to be
8+
relied on.
9+
10+
```r
11+
# Prev:
12+
fy2date("foo")
13+
#> Error: fy.yr contains non-FYs.
14+
15+
# Now
16+
#> Error: `x = "foo"` was not a valid financial year.
17+
```
18+
19+
20+
21+
122
## 1.7.1.4
223
* Fixed minor error that affected tax liability calculations for 2010-11 and 2011-12 financial years
324

R/CG_inflator.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ CG_population_inflator <- function(x = 1,
1313
to_fy,
1414
forecast.series = "mean",
1515
cg.series){
16-
stopifnot(all_fy(c(from_fy, to_fy)))
16+
from_fy <- validate_fys_permitted(from_fy)
17+
to_fy <- validate_fys_permitted(to_fy)
1718
stopifnot(forecast.series %in% c("mean", "lower", "upper", "custom"))
1819

1920
last_fy <- max(from_fy, to_fy)
@@ -43,25 +44,24 @@ CG_population_inflator <- function(x = 1,
4344

4445
CG_inflator <- function(x = 1, from_fy, to_fy, forecast.series = "mean"){
4546
prohibit_vector_recycling(x, from_fy, to_fy)
46-
stopifnot(is.numeric(x), all_fy(from_fy), all_fy(to_fy))
47+
stopifnot(is.numeric(x))
48+
cg_fys <- union(cg_inflators_1213[["fy_year"]],
49+
cg_inflators_1617[["fy_year"]])
50+
from_fy <- validate_fys_permitted(from_fy, permitted_fys = cg_fys)
51+
to_fy <- validate_fys_permitted(to_fy, permitted_fys = cg_fys)
52+
53+
4754

4855
nse_forecast_series <- forecast.series
4956
cg_inflators_tbl <-
5057
cg_inflators_1516[forecast.series == nse_forecast_series]
51-
52-
53-
# Else NAs.
54-
stopifnot(all(to_fy %in% cg_inflators_1516[["fy_year"]]),
55-
all(from_fy %in% cg_inflators_1516[["fy_year"]]))
5658

5759
# CRAN Note avoidance
5860
ordering <- NULL
5961
input <-
6062
data.table(x = x, from_fy = from_fy, to_fy = to_fy) %>%
6163
.[, ordering := 1:.N]
6264

63-
64-
6565
raw_out <-
6666
input %>%
6767
merge(cg_inflators_tbl, by.y = "fy_year", by.x = "from_fy", all.x = TRUE) %>%

R/append_custom_series.R

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,27 @@ append_custom_series <- function(orig,
5151
# Is the following
5252
if (first_fy_in_custom_series == next_fy(last_full_fy_in_orig)) {
5353
last_obsValue_in_actual_series <- last(.subset2(orig, "obsValue"))
54-
55-
obsValue <- r <- NULL
56-
custom.series[, obsValue := last_obsValue_in_actual_series * cumprod(1 + r)]
57-
58-
out <-
59-
rbindlist(list(orig,
60-
custom.series),
61-
use.names = TRUE,
62-
fill = TRUE) %>%
63-
# Ensure the date falls appropriately
64-
unique(by = "fy_year", fromLast = TRUE)
6554
} else {
6655
series_before_custom <- orig[fy_year < first_fy_in_custom_series]
67-
6856
last_obsValue_in_actual_series <- last(series_before_custom[["obsValue"]])
69-
custom.series[, obsValue := last_obsValue_in_actual_series * cumprod(1 + r)]
70-
71-
out <-
72-
rbindlist(list(orig,
73-
custom.series),
74-
use.names = TRUE,
75-
fill = TRUE) %>%
76-
# Ensure the date falls appropriately
77-
unique(by = "fy_year", fromLast = TRUE)
57+
7858
}
7959

80-
out
60+
obsValue <- r <- NULL
61+
custom.series[, obsValue := last_obsValue_in_actual_series * cumprod(1 + r)]
62+
63+
# TODO: make fy inherit character
64+
if (inherits(.subset2(orig, "fy_year"), "fy") &&
65+
!inherits(.subset2(custom.series, "fy_year"), "fy")) {
66+
orig <- copy(orig)[, fy_year := as.character(fy_year)]
67+
}
8168

69+
rbindlist(list(orig,
70+
custom.series),
71+
use.names = TRUE,
72+
fill = TRUE) %>%
73+
# Ensure the date falls appropriately
74+
unique(by = "fy_year", fromLast = TRUE)
8275
}
8376

8477

R/carer_payment.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ carer_payment <- function(Date = NULL,
7575
}
7676
}
7777

78+
class(fy.year) <- c("fy", "character") # for cbind.data.frame method
7879
input <- data.table(do.call(cbind.data.frame, mget(ls())))
7980

8081
#Rates, income test, and asset test same as age pension

R/cpi_inflator.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ cpi_inflator <- function(from_nominal_price = 1,
135135
}
136136

137137

138+
138139
permitted_fys <- .subset2(cpi.indices, "fy_year")
139140
earliest_from_fy <- permitted_fys[[1L]]
140141
cpi_table_nom <-
@@ -182,6 +183,10 @@ cpi_inflator <- function(from_nominal_price = 1,
182183
max_fy2yr(to_fy),
183184
by = 1L)),
184185
obsValue = cpi_index_forecast))
186+
187+
# TODO: fy should inherit 'character'
188+
cpi.indices.new[, fy_year := as.character(fy_year)]
189+
185190
cpi.indices <-
186191
rbindlist(list(cpi.indices, cpi.indices.new),
187192
use.names = TRUE,

R/fy.year.R

Lines changed: 17 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#' Convenience functions for dealing with financial years
22
#'
3+
#' @description From grattan v1.7.1.4, these are reexports from the \code{\link[fy]{fy-package}}.
4+
#'
35
#' @name is.fy
46
#' @aliases fy.year yr2fy fy2yr fy2date date2fy
57
#' @param yr_ending An integer representing a year.
@@ -23,6 +25,7 @@
2325
#'
2426
#' \code{date2fy} converts a date to the corresponding financial year.
2527
#'
28+
#' @importFrom fy validate_fys_permitted
2629
#'
2730
#' @examples
2831
#' is.fy("2012-13")
@@ -33,195 +36,29 @@
3336
#' @export is.fy fy.year yr2fy fy2yr fy2date date2fy
3437
NULL
3538

39+
is.fy <- fy::is_fy
3640

37-
38-
is.fy <- function(fy.yr){
39-
out <- logical(length(fy.yr))
40-
potential_fys <- grepl("^([12][0-9]{3})[-\\s]?[0-9]{2}$", fy.yr, perl = TRUE)
41-
out[potential_fys] <-
42-
{as.integer(sub("^([12][0-9]{3})[-\\s]?[0-9]{2}$", "\\1", fy.yr[potential_fys], perl = TRUE)) + 1L} %% 100L == as.integer(sub("^[12][0-9]{3}[-\\s]?([0-9]{2})$", "\\1", fy.yr[potential_fys], perl = TRUE))
43-
out
44-
}
45-
46-
is_fy2 <- function(x) {
47-
a <- c("1897-98", "1898-99", "1899-00", "1900-01", "1901-02", "1902-03",
48-
"1903-04", "1904-05", "1905-06", "1906-07", "1907-08", "1908-09",
49-
"1909-10", "1910-11", "1911-12", "1912-13", "1913-14", "1914-15",
50-
"1915-16", "1916-17", "1917-18", "1918-19", "1919-20", "1920-21",
51-
"1921-22", "1922-23", "1923-24", "1924-25", "1925-26", "1926-27",
52-
"1927-28", "1928-29", "1929-30", "1930-31", "1931-32", "1932-33",
53-
"1933-34", "1934-35", "1935-36", "1936-37", "1937-38", "1938-39",
54-
"1939-40", "1940-41", "1941-42", "1942-43", "1943-44", "1944-45",
55-
"1945-46", "1946-47", "1947-48", "1948-49", "1949-50", "1950-51",
56-
"1951-52", "1952-53", "1953-54", "1954-55", "1955-56", "1956-57",
57-
"1957-58", "1958-59", "1959-60", "1960-61", "1961-62", "1962-63",
58-
"1963-64", "1964-65", "1965-66", "1966-67", "1967-68", "1968-69",
59-
"1969-70", "1970-71", "1971-72", "1972-73", "1973-74", "1974-75",
60-
"1975-76", "1976-77", "1977-78", "1978-79", "1979-80", "1980-81",
61-
"1981-82", "1982-83", "1983-84", "1984-85", "1985-86", "1986-87",
62-
"1987-88", "1988-89", "1989-90", "1990-91", "1991-92", "1992-93",
63-
"1993-94", "1994-95", "1995-96", "1996-97", "1997-98", "1998-99",
64-
"1999-00", "2000-01", "2001-02", "2002-03", "2003-04", "2004-05",
65-
"2005-06", "2006-07", "2007-08", "2008-09", "2009-10", "2010-11",
66-
"2011-12", "2012-13", "2013-14", "2014-15", "2015-16", "2016-17",
67-
"2017-18", "2018-19", "2019-20", "2020-21", "2021-22", "2022-23",
68-
"2023-24", "2024-25", "2025-26", "2026-27", "2027-28", "2028-29",
69-
"2029-30", "2030-31", "2031-32", "2032-33", "2033-34", "2034-35",
70-
"2035-36", "2036-37", "2037-38", "2038-39", "2039-40", "2040-41",
71-
"2041-42", "2042-43", "2043-44", "2044-45", "2045-46", "2046-47",
72-
"2047-48", "2048-49", "2049-50", "2050-51", "2051-52", "2052-53")
73-
x %fin% a
74-
}
75-
76-
all_fy <- function(x, permitted = NULL) {
77-
is.character(x) && length(x) && {
78-
79-
80-
a <- if (is.null(permitted)) {
81-
c("1897-98", "1898-99", "1899-00", "1900-01", "1901-02", "1902-03",
82-
"1903-04", "1904-05", "1905-06", "1906-07", "1907-08", "1908-09",
83-
"1909-10", "1910-11", "1911-12", "1912-13", "1913-14", "1914-15",
84-
"1915-16", "1916-17", "1917-18", "1918-19", "1919-20", "1920-21",
85-
"1921-22", "1922-23", "1923-24", "1924-25", "1925-26", "1926-27",
86-
"1927-28", "1928-29", "1929-30", "1930-31", "1931-32", "1932-33",
87-
"1933-34", "1934-35", "1935-36", "1936-37", "1937-38", "1938-39",
88-
"1939-40", "1940-41", "1941-42", "1942-43", "1943-44", "1944-45",
89-
"1945-46", "1946-47", "1947-48", "1948-49", "1949-50", "1950-51",
90-
"1951-52", "1952-53", "1953-54", "1954-55", "1955-56", "1956-57",
91-
"1957-58", "1958-59", "1959-60", "1960-61", "1961-62", "1962-63",
92-
"1963-64", "1964-65", "1965-66", "1966-67", "1967-68", "1968-69",
93-
"1969-70", "1970-71", "1971-72", "1972-73", "1973-74", "1974-75",
94-
"1975-76", "1976-77", "1977-78", "1978-79", "1979-80", "1980-81",
95-
"1981-82", "1982-83", "1983-84", "1984-85", "1985-86", "1986-87",
96-
"1987-88", "1988-89", "1989-90", "1990-91", "1991-92", "1992-93",
97-
"1993-94", "1994-95", "1995-96", "1996-97", "1997-98", "1998-99",
98-
"1999-00", "2000-01", "2001-02", "2002-03", "2003-04", "2004-05",
99-
"2005-06", "2006-07", "2007-08", "2008-09", "2009-10", "2010-11",
100-
"2011-12", "2012-13", "2013-14", "2014-15", "2015-16", "2016-17",
101-
"2017-18", "2018-19", "2019-20", "2020-21", "2021-22", "2022-23",
102-
"2023-24", "2024-25", "2025-26", "2026-27", "2027-28", "2028-29",
103-
"2029-30", "2030-31", "2031-32", "2032-33", "2033-34", "2034-35",
104-
"2035-36", "2036-37", "2037-38", "2038-39", "2039-40", "2040-41",
105-
"2041-42", "2042-43", "2043-44", "2044-45", "2045-46", "2046-47",
106-
"2047-48", "2048-49", "2049-50", "2050-51", "2051-52", "2052-53")
107-
} else {
108-
permitted
109-
}
110-
if (anyNA(fmatch(x, a))) {
111-
a <- NULL
112-
FALSE
113-
} else {
114-
a <- NULL
115-
TRUE
116-
}
117-
}
118-
}
119-
120-
range_fy2yr <- function(x) {
121-
if (length(x) == 1L) {
122-
y <- fmatch(x, fys1901) + 1900L
123-
return(rep(y, times = 2L))
124-
}
125-
if (!is.null(g_min_yr <- attr(x, "grattan_min_yr")) &&
126-
!is.null(g_max_yr <- attr(x, "grattan_max_yr"))) {
127-
return(c(g_min_yr, g_max_yr))
128-
}
129-
y <- fmatch(x, fys1901) + 1900L
130-
miny <- min(y, na.rm = TRUE)
131-
maxy <- max(y, na.rm = TRUE)
132-
setattr(x, "grattan_min_yr", miny)
133-
setattr(x, "grattan_max_yr", maxy)
134-
c(miny, maxy)
135-
}
136-
137-
min_fy2yr <- function(x) {
138-
range_fy2yr(x)[1L]
139-
}
140-
141-
max_fy2yr <- function(x) {
142-
range_fy2yr(x)[2L]
143-
}
144-
145-
fy.year <- function(yr_ending){
41+
fy.year <- function(yr_ending) {
14642
paste0(as.integer(yr_ending) - 1, "-", substr(yr_ending, 3, 4))
14743
}
14844

149-
yr2fy <- function(yr_ending, assume1901_2100 = .getOption("grattan.assume1901_2100", TRUE)) {
150-
if (assume1901_2100 ||
151-
AND(min(yr_ending) > 1900L,
152-
max(yr_ending) < 2100L)) {
153-
fys1901[yr_ending - 1900L]
154-
} else {
155-
.yr2fy(yr_ending)
156-
}
157-
}
158-
159-
.yr2fy <- function(yr_ending) {
160-
if (length(yr_ending) > 10e3L) {
161-
# Apparently quicker for > 1000
162-
accel_repetitive_input(yr_ending, .yr2fy)
163-
} else {
164-
sprintf("%d-%02d", as.integer(yr_ending) - 1L, as.integer(yr_ending) %% 100L)
165-
}
166-
}
167-
168-
fy2yr <- function(fy.yr){
169-
if (!all(is.fy(fy.yr))){
170-
stop("fy.yr contains non-FYs")
171-
} else {
172-
1L + as.integer(gsub("^.*([12][0-9]{3}).?[0-9]{2}.*$", "\\1", fy.yr))
173-
}
174-
}
175-
176-
177-
178-
fy2date <- function(x){
179-
if (!all(is.fy(x))){
180-
stop("fy.yr contains non-FYs")
181-
} else {
182-
date <- paste0(as.numeric(gsub("^([1-9][0-9]{3}).*", "\\1", x)) + 1, "-06-30")
183-
as.Date(date)
184-
}
185-
}
186-
45+
yr2fy <- fy::yr2fy
46+
fy2yr <- fy::fy2yr
47+
fy2date <- fy::fy2date
48+
date2fy <- fy::date2fy
49+
qtr2fy <- fy::qtr2fy
18750

188-
date2fy <- function(date) {
189-
if_else(month(date) < 7L,
190-
yr2fy(year(date)),
191-
yr2fy(year(date) + 1L))
192-
}
51+
max_fy2yr <- function(x) fy2yr(max(x))
52+
min_fy2yr <- function(x) fy2yr(min(x))
19353

194-
qtr2fy <- function(yq) {
195-
if (inherits(yq, "yearqtr")) {
196-
yqn <- as.numeric(yq)
197-
o <-
198-
yr2fy(if_else(yqn %% 1 >= 0.5,
199-
yqn + 1,
200-
yqn))
201-
o
202-
} else if (is.character(yq)) {
203-
# Rely on the first element to determine the
204-
# format
205-
first_yq <- yq[1L]
206-
if (is.na(first_yq)) {
207-
yq_is_na <- is.na(yq)
208-
first_yq <- first(yq[which.min(yq_is_na)])
209-
}
210-
211-
y <- q <- NULL
212-
cm <- CJ(y = 1901:2099, q = 1:4)
213-
cm[, "YQ" := sprintf("%d%sQ%d", y, substr(first_yq, 5L, 5L), q)]
214-
cm[, "fy_year" := yr2fy(y + q %in% 3:4)]
215-
cmyq <- .subset2(cm, "YQ")
216-
o <- .subset2(cm, "fy_year")[fmatch(yq, cmyq)]
54+
all_fy <- function(x, permitted = NULL) {
55+
if (is.null(permitted)) {
56+
all(fy::is_fy(x), na.rm = TRUE)
21757
} else {
218-
stop("Unknown class for `yq`.")
58+
!anyNA(fmatch(x, permitted))
21959
}
220-
o
22160
}
22261

223-
224-
225-
62+
is_fy2 <- fy::is_fy
22663

22764

0 commit comments

Comments
 (0)