The R Project SVN R

Rev

Rev 81455 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 81455 Rev 90216
Line 5... Line 5...
5
## The mapping is
5
## The mapping is
6
## as printed: "dim", "dimnames", "names", "tsp", "levels"
6
## as printed: "dim", "dimnames", "names", "tsp", "levels"
7
## deparsed: ".Dim", ".Dimnames", ".Names", ".Tsp", ".Label"
7
## deparsed: ".Dim", ".Dimnames", ".Names", ".Tsp", ".Label"
8
## structure() remaps to the printed form.
8
## structure() remaps to the printed form.
9
 
9
 
10
## The remapping in deparse will be removed in R 4.2.0.
10
## The remapping in deparse has been removed in R 4.2.0.
-
 
11
## The re-remapping (e.g. from ".Dim" to "dim") is deprecated for R 4.7.0
-
 
12
 
-
 
13
## Now __warn__ about old names use:
-
 
14
options(keep.parse.data = FALSE) -> op
-
 
15
getVaW <- function(expr) { # obj = TRUE - version
-
 
16
    W <- NULL
-
 
17
    withCallingHandlers(val <- expr,
-
 
18
                        warning = function(w) {
-
 
19
                            W <<- w
-
 
20
                            invokeRestart("muffleWarning") })
-
 
21
    structure(val %||% quote(._NULL_()), warning = W) # NULL cannot have attr.
-
 
22
}
-
 
23
noW <- function(x) {attr(x, "warning") <- NULL ; x }
-
 
24
chk_n_prt <- function(w) {
-
 
25
    w <- attr(w,"warning")
-
 
26
    cat(conditionMessage(w), "\n")
-
 
27
    inherits(w, "deprecatedWarning")
-
 
28
}
11
 
29
 
12
X <- matrix(1:4, 2, 2, dimnames=list(c("A", "B"), 1:2))
30
X <- matrix(1:4, 2, 2, dimnames=list(c("A", "B"), 1:2))
13
names(attributes(X))
31
names(attributes(X))
14
cat(deparse(X), "\n")
32
cat(deparse(X), "\n")
15
Y <- structure(1:4, .Dim = c(2L, 2L),
33
w1 <- getVaW(structure(1:4, .Dim = c(2L, 2L),
16
               .Dimnames = list(c("A", "B"), c("1", "2")))
34
                       .Dimnames = list(c("A", "B"), c("1", "2"))))
17
identical(X, Y)
35
stopifnot(chk_n_prt(w1), identical(noW(w1), X))
18
 
36
 
19
z <- ts(1:10, frequency = 4, start = c(1959, 2))
37
z <- ts(1:10, frequency = 4, start = c(1959, 2))
20
attributes(z)
38
attributes(z) # had '.Tsp' component, now 'tsp'
21
cat(deparse(z), "\n")
39
cat(deparse(z), "\n")
22
z2 <- structure(1:10, .Tsp = c(1959.25, 1961.5, 4), class = "ts")
40
w2 <- getVaW(structure(1:10, .Tsp = c(1959.25, 1961.5, 4), class = "ts"))
23
identical(z, z2)
41
stopifnot(chk_n_prt(w2), identical(noW(w2), z))
24
 
42
 
25
## levels <-> .Label is most relevant to factors, but is always remapped.
43
## levels <-> .Label is most relevant to factors, but is always remapped; also .Dim :
26
x <- 1:3
44
x <- array(1:3, 3)
27
attr(x, "levels") <- letters[x]
45
attr(x, "levels") <- letters[x]
28
cat(deparse(x), "\n")
46
cat(deparse(x), "\n") # did show  ' .Label = ..'  but shows "levels = .." since 2021
29
y <- structure(1:3, .Label = c("a", "b", "c"))
47
w3 <- getVaW(structure(1:3, .Label = c("a", "b", "c"), .Dim = 3L))
30
identical(x, y)
48
stopifnot(chk_n_prt(w3), identical(noW(w3), x))
31
 
49
 
32
 
50
 
33
## Factors were long deparsed with double (rather than integer codes).
51
## factors were long deparsed with double (rather than integer codes).
34
## As from R 2.5.0 parsing such a deparse will given an error, so
52
## As from R 2.5.0 parsing such a deparse will given an error, so
35
## structure() coerces the codes to an integer vector.
53
## structure() coerces the codes to an integer vector.
36
## Example from an earlier version of Puromycin.R
54
## Example from an earlier version of Puromycin.R
37
state <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55
state <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38
                     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
56
                     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
39
                   .Label = c("treated", "untreated"), class = "factor")
57
                   levels = c("treated", "untreated"), class = "factor")
40
typeof(state)
58
typeof(state)       # "integer" -- probably will become "double"
41
storage.mode(state)
59
storage.mode(state) # ditto
42
attributes(state)
60
attributes(state)
43
cat(deparse(state))
61
cat(deparse(state))
44
 
62
 
45
state2 <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
63
state2 <- structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
46
                      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
64
                      2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
47
                    levels = c("treated", "untreated"), class = "factor")
65
                    levels = c("treated", "untreated"), class = "factor")
48
identical(state,state2)
66
identical(state,state2)
-
 
67
 
-
 
68
options(op) # in case this is source()d