The R Project SVN R

Rev

Rev 81944 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed


R Under development (unstable) (2026-07-08 r90215) -- "Unsuffered Consequences"
Copyright (C) 2026 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ### Tests of structure() and deparsing.
> 
> ## deparsing has always treated some attributes specially (apparently
> ## to allow inter-operability with S).
> ## The mapping is
> ## as printed: "dim", "dimnames", "names", "tsp", "levels"
> ## deparsed: ".Dim", ".Dimnames", ".Names", ".Tsp", ".Label"
> ## structure() remaps to the printed form.
> 
> ## The remapping in deparse has been removed in R 4.2.0.
> ## The re-remapping (e.g. from ".Dim" to "dim") is deprecated for R 4.7.0
> 
> ## Now __warn__ about old names use:
> options(keep.parse.data = FALSE) -> op
> getVaW <- function(expr) { # obj = TRUE - version
+     W <- NULL
+     withCallingHandlers(val <- expr,
+                         warning = function(w) {
+                             W <<- w
+                             invokeRestart("muffleWarning") })
+     structure(val %||% quote(._NULL_()), warning = W) # NULL cannot have attr.
+ }
> noW <- function(x) {attr(x, "warning") <- NULL ; x }
> chk_n_prt <- function(w) {
+     w <- attr(w,"warning")
+     cat(conditionMessage(w), "\n")
+     inherits(w, "deprecatedWarning")
+ }
> 
> X <- matrix(1:4, 2, 2, dimnames=list(c("A", "B"), 1:2))
> names(attributes(X))
[1] "dim"      "dimnames"
> cat(deparse(X), "\n")
structure(1:4, dim = c(2L, 2L), dimnames = list(c("A", "B"),      c("1", "2"))) 
> w1 <- getVaW(structure(1:4, .Dim = c(2L, 2L),
+                        .Dimnames = list(c("A", "B"), c("1", "2"))))
> stopifnot(chk_n_prt(w1), identical(noW(w1), X))
Replacing special names '.Dim', '.Dimnames' is deprecated; use 'dim', 'dimnames' instead. 
> 
> z <- ts(1:10, frequency = 4, start = c(1959, 2))
> attributes(z) # had '.Tsp' component, now 'tsp'
$tsp
[1] 1959.25 1961.50    4.00

$class
[1] "ts"

> cat(deparse(z), "\n")
structure(1:10, tsp = c(1959.25, 1961.5, 4), class = "ts") 
> w2 <- getVaW(structure(1:10, .Tsp = c(1959.25, 1961.5, 4), class = "ts"))
> stopifnot(chk_n_prt(w2), identical(noW(w2), z))
Replacing special names '.Tsp' is deprecated; use 'tsp' instead. 
> 
> ## levels <-> .Label is most relevant to factors, but is always remapped; also .Dim :
> x <- array(1:3, 3)
> attr(x, "levels") <- letters[x]
> cat(deparse(x), "\n") # did show  ' .Label = ..'  but shows "levels = .." since 2021
structure(1:3, dim = 3L, levels = c("a", "b", "c")) 
> w3 <- getVaW(structure(1:3, .Label = c("a", "b", "c"), .Dim = 3L))
> stopifnot(chk_n_prt(w3), identical(noW(w3), x))
Replacing special names '.Label', '.Dim' is deprecated; use 'levels', 'dim' instead. 
> 
> 
> ## factors were long deparsed with double (rather than integer codes).
> ## As from R 2.5.0 parsing such a deparse will given an error, so
> ## structure() coerces the codes to an integer vector.
> ## Example from an earlier version of Puromycin.R
> state <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+                      2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
+                    levels = c("treated", "untreated"), class = "factor")
> typeof(state)       # "integer" -- probably will become "double"
[1] "integer"
> storage.mode(state) # ditto
[1] "integer"
> attributes(state)
$levels
[1] "treated"   "untreated"

$class
[1] "factor"

> cat(deparse(state))
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("treated",  "untreated"), class = "factor")> 
> state2 <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
+                       2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
+                     levels = c("treated", "untreated"), class = "factor")
> identical(state,state2)
[1] TRUE
> 
> options(op) # in case this is source()d
>