Skip to content

Commit

Permalink
Warn if deprecated attrs passed to xts()
Browse files Browse the repository at this point in the history
Index attributes will no longer be attached to the xts object itself,
so warn the user if they pass deprecated arguments to the xts
constructor.

See #245.
  • Loading branch information
joshuaulrich committed Oct 22, 2018
1 parent fbd742a commit 1e64734
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
21 changes: 21 additions & 0 deletions R/xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ function(x=NULL,
index=structure(index,tzone=tzone,tclass=orderBy),
class=c('xts','zoo'),
...)

ctor.call <- match.call(expand.dots = TRUE)
if(hasArg(".indexFORMAT")) {
warning(sQuote(".indexFORMAT"), " is deprecated, use tformat instead.")
if(missing("tformat")) {
attr(attr(x, "index"), "tformat") <- eval.parent(ctor.call$.indexFORMAT)
}
}
if(hasArg(".indexCLASS")) {
warning(sQuote(".indexCLASS"), " is deprecated, use tclass instead.")
if(missing("tclass")) {
attr(attr(x, "index"), "tclass") <- eval.parent(ctor.call$.indexCLASS)
}
}
if(hasArg(".indexTZ")) {
warning(sQuote(".indexTZ"), " is deprecated, use tzone instead.")
if(missing("tzone")) {
attr(attr(x, "index"), "tzone") <- eval.parent(ctor.call$.indexTZ)
}
}

if(!is.null(attributes(x)$dimnames[[1]]))
# this is very slow if user adds rownames, but maybe that is deserved :)
dimnames(x) <- dimnames(x) # removes row.names
Expand Down
6 changes: 6 additions & 0 deletions inst/unitTests/runit.tclass.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ test.ctors_dont_add_tclass_indexCLASS_to_object <- function() {
checkIdentical(NULL, attr(y, ".indexCLASS"))
}

test.xts_ctor_warns_for_indexCLASS_arg <- function() {
op <- options(warn = 2)
on.exit(options(warn = op$warn))
checkException(x <- xts(1, as.Date("2018-05-02"), .indexCLASS = "Date"))
}

test.get_coredata_drops_xts_tclass_indexCLASS <- function() {
y <- coredata(x)
checkIdentical(NULL, attr(y, "tclass"))
Expand Down
6 changes: 6 additions & 0 deletions inst/unitTests/runit.tformat.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ test.ctors_dont_add_indexFORMAT_to_object <- function() {
checkIdentical(NULL, attr(y, ".indexFORMAT"))
}

test.xts_ctor_warns_for_indexFORMAT_arg <- function() {
op <- options(warn = 2)
on.exit(options(warn = op$warn))
checkException(x <- xts(1, as.Date("2018-05-02"), .indexFORMAT = "%Y"))
}

test.get_coredata_drops_xts_indexFORMAT <- function() {
y <- coredata(x)
checkIdentical(NULL, attr(y, ".indexFORMAT"))
Expand Down
6 changes: 6 additions & 0 deletions inst/unitTests/runit.tzone.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ test.ctors_dont_add_tzone_indexTZ_to_object <- function() {
checkIdentical(NULL, attr(y, ".indexTZ"))
}

test.xts_ctor_warns_for_indexTZ_arg <- function() {
op <- options(warn = 2)
on.exit(options(warn = op$warn))
checkException(x <- xts(1, as.Date("2018-05-02"), .indexTZ = "UTC"))
}

test.get_coredata_drops_xts_tzone_indexTZ <- function() {
y <- coredata(x)
checkIdentical(NULL, attr(y, "tzone"))
Expand Down

0 comments on commit 1e64734

Please sign in to comment.