Skip to content

Commit c440e98

Browse files
author
maechler
committed
attr<-(., "tsp")`s C code uses "ts.eps" option instead of hardwired 1e-5; ts() using that
git-svn-id: https://svn.r-project.org/R/trunk@88310 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 02272ff commit c440e98

File tree

7 files changed

+34
-22
lines changed

7 files changed

+34
-22
lines changed

doc/NEWS.Rd

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
support variable font axes so far.
6464
}
6565
}
66-
66+
6767
\subsection{INSTALLATION}{
6868
\itemize{
6969
\item .
@@ -146,6 +146,11 @@
146146
\item \code{text()} now truncates \code{labels} to maximum length
147147
of \code{x} and \code{y} (if it is longer), fixing \PR{7084}.
148148
Thanks to \I{Heather Turner} and \I{Ella Kaye}.
149+
150+
\item \code{attr(., "tsp") <- val} now uses \code{getOption("ts.eps")}
151+
instead of hardwired \code{1e-5}; consequently, \code{ts(.., ts.eps=*)}
152+
now passes \code{ts.eps} to the \code{"tsp"} setting C code;
153+
both fixing a long-standing \sQuote{\I{FIXME}}.
149154
}
150155
}
151156
}
@@ -3551,7 +3556,7 @@
35513556
patch by \I{Thomas Soeiro} in \PR{18243}.
35523557
35533558
\item \code{as.vector()} gains a \code{data.frame} method which
3554-
returns a simple named list, also clearing a long standing
3559+
returns a simple named list, also clearing a long-standing
35553560
\sQuote{\I{FIXME}} to enable \code{as.vector(<data.frame>,
35563561
mode="list")}. This breaks code relying on
35573562
\code{as.vector(<data.frame>)} to return the unchanged data frame.

src/library/base/po/R.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ msgid "names() applied to a non-vector"
11951195
msgstr ""
11961196

11971197
#: src/main/attrib.c:432
1198-
msgid "invalid time series parameters specified"
1198+
msgid "invalid time series parameters specified (%d)"
11991199
msgstr ""
12001200

12011201
#: src/main/attrib.c:446

src/library/stats/R/ts.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/stats/R/ts.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2024 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -86,8 +86,10 @@ ts <- function(data = NA, start = 1, end = numeric(), frequency = 1,
8686
if(ndata < nobs) data[rep_len(1L:ndata, nobs), ]
8787
else if(ndata > nobs) data[1L:nobs, ]
8888
}
89-
## FIXME: The following "attr<-"() calls C tspgets() which uses a
90-
## fixed equivalent of ts.eps := 1e-5
89+
## attr(data, "tsp") <- .. below calls C tspgets() which uses getOption("ts.eps"):
90+
if(doEps <- !missing(ts.eps) && ts.eps != getOption("ts.eps")) {
91+
op <- options(ts.eps = ts.eps); on.exit(options(op))
92+
}
9193
attr(data, "tsp") <- c(start, end, frequency) #-- order is fixed
9294
if(!is.null(class) && class[[1]] != "none") attr(data, "class") <- class
9395
## if you alter the return structure, you also need to alter
@@ -400,7 +402,7 @@ print.ts <- function(x, calendar, ...)
400402
invisible(x)
401403
}
402404

403-
## To be used in a format.ts():
405+
## used in print.ts(), and to be used in a (future / other pkg) format.ts()
404406
.preformat.ts <- function(x, calendar, ...)
405407
{
406408
fr.x <- frequency(x)
@@ -456,7 +458,7 @@ print.ts <- function(x, calendar, ...)
456458
paste(month.abb[t2], p1)
457459
else
458460
paste(p1, if(fr.x == 4) c("Q1", "Q2", "Q3", "Q4")[t2]
459-
else format(t2))
461+
else format(t2))
460462
} else
461463
format(time(x))
462464
attr(x, "class") <- attr(x, "tsp") <- attr(x, "na.action") <- NULL

src/library/stats/man/ts.Rd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/stats/man/ts.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2024 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{ts}
@@ -49,7 +49,8 @@ is.mts(x)
4949
\code{frequency} or \code{deltat} should be provided.}
5050
\item{ts.eps}{time series comparison tolerance. Frequencies are
5151
considered equal if their absolute difference is less than
52-
\code{ts.eps}.}
52+
\code{ts.eps}. It is also used to check consistency of \code{end - start},
53+
\code{frequency}, and the length of the time-series.}
5354
\item{class}{class to be given to the result, or none if \code{NULL}
5455
or \code{"none"}. The default is \code{"ts"} for a single series, or
5556
\code{c("mts", "ts", "matrix", "array")} for multiple series.}

src/library/stats/man/tsp.Rd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
% File src/library/stats/man/tsp.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2014 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{tsp}
7-
\title{Tsp Attribute of Time-Series-like Objects}
7+
\title{`Tsp` Attribute of Time-Series-like Objects}
88
\alias{tsp}
99
\alias{tsp<-}
1010
\alias{hasTsp}
@@ -27,6 +27,7 @@ hasTsp(x)
2727
The \code{tsp} attribute gives the start time \emph{in time units},
2828
the end time and the frequency (the number of observations per unit of
2929
time, e.g.\sspace{}12 for a monthly series).
30+
The frequency may be fractional, see \code{\link{ts}}.
3031

3132
Assignments are checked for consistency.
3233

src/main/attrib.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,17 +427,14 @@ static void checkNames(SEXP x, SEXP s)
427427

428428
/* Time Series Parameters */
429429

430-
NORET static void badtsp(void)
430+
NORET static void badtsp(int k)
431431
{
432-
error(_("invalid time series parameters specified"));
432+
error(_("invalid time series parameters specified (%d)"), k);
433433
}
434434

435435
attribute_hidden
436436
SEXP tspgets(SEXP vec, SEXP val)
437437
{
438-
double start, end, frequency;
439-
int n;
440-
441438
if (vec == R_NilValue)
442439
error(_("attempt to set an attribute on NULL"));
443440

@@ -451,6 +448,7 @@ SEXP tspgets(SEXP vec, SEXP val)
451448
if (!isNumeric(val) || LENGTH(val) != 3)
452449
error(_("'tsp' attribute must be numeric of length three"));
453450

451+
double start, end, frequency;
454452
if (isReal(val)) {
455453
start = REAL(val)[0];
456454
end = REAL(val)[1];
@@ -464,13 +462,12 @@ SEXP tspgets(SEXP vec, SEXP val)
464462
frequency = (INTEGER(val)[2] == NA_INTEGER) ?
465463
NA_REAL : INTEGER(val)[2];
466464
}
467-
if (frequency <= 0) badtsp();
468-
n = nrows(vec);
465+
if (frequency <= 0) badtsp(0);
466+
int n = nrows(vec);
469467
if (n == 0) error(_("cannot assign 'tsp' to zero-length vector"));
470468

471-
/* FIXME: 1.e-5 should rather be == option('ts.eps') !! */
472-
if (fabs(end - start - (n - 1)/frequency) > 1.e-5)
473-
badtsp();
469+
if (fabs(end - start - (n - 1)/frequency) > asReal(GetOption1(install("ts.eps"))))
470+
badtsp(1);
474471

475472
PROTECT(vec);
476473
val = allocVector(REALSXP, 3);

tests/reg-tests-1e.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,12 @@ stopifnot(exprs = {
19961996
## The t.test() calls errored all in R <= 4.5.1
19971997

19981998

1999+
## long standing "FIXME" fixed:
2000+
stopifnot(is.ts(ts(1:711, frequency=2*pi, start = 1, end = 114)))
2001+
assertErrV( ts(1:711, frequency=2*pi, start = 1, end = 114, ts.eps = 1e-6) )
2002+
## did *not* error in R <= 4.5.1, as 'ts.eps' was *not* passed to C code
2003+
2004+
19992005

20002006
## keep at end
20012007
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)