/
eval-etc.Rout.save
173 lines (168 loc) · 4.2 KB
/
eval-etc.Rout.save
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
R version 2.5.0 Under development (unstable) (2006-11-11 r39849)
Copyright (C) 2006 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
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.
> #### eval / parse / deparse / substitute etc
>
> ##- From: Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk>
> ##- Subject: Re: source() / eval() bug ??? (PR#96)
> ##- Date: 20 Jan 1999 14:56:24 +0100
> e1 <- parse(text='c(F=(f <- .3), "Tail area" = 2 * if(f < 1) 30 else 90)')[[1]]
> e1
c(F = (f <- 0.3), "Tail area" = 2 * if (f < 1) 30 else 90)
> str(eval(e1))
Named num [1:2] 0.3 60
- attr(*, "names")= chr [1:2] "F" "Tail area"
> mode(e1)
[1] "call"
>
> ( e2 <- quote(c(a=1,b=2)) )
c(a = 1, b = 2)
> names(e2)[2] <- "a b c"
> e2
c("a b c" = 1, b = 2)
> parse(text=deparse(e2))
expression(c("a b c" = 1, b = 2))
>
> ##- From: Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk>
> ##- Date: 22 Jan 1999 11:47
>
> ( e3 <- quote(c(F=1,"tail area"=pf(1,1,1))) )
c(F = 1, "tail area" = pf(1, 1, 1))
> eval(e3)
F tail area
1.0 0.5
> names(e3)
[1] "" "F" "tail area"
>
> names(e3)[2] <- "Variance ratio"
> e3
c("Variance ratio" = 1, "tail area" = pf(1, 1, 1))
> eval(e3)
Variance ratio tail area
1.0 0.5
>
>
> ##- From: Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk>
> ##- Date: 2 Sep 1999
>
> ## The first failed in 0.65.0 :
> attach(list(x=1))
> evalq(dim(x) <- 1,as.environment(2))
> dput(get("x", env=as.environment(2)), control="all")
structure(1, .Dim = 1L)
>
> e <- local({x <- 1;environment()})
> evalq(dim(x) <- 1,e)
> dput(get("x",env=e), control="all")
structure(1, .Dim = 1L)
>
> ### Substitute, Eval, Parse, etc
>
> ## PR#3 : "..." matching
> ## Revised March 7 2001 -pd
> A <- function(x, y, ...) {
+ B <- function(a, b, ...) { match.call() }
+ B(x+y, ...)
+ }
> (aa <- A(1,2,3))
B(a = x + y, b = 3)
> all.equal(as.list(aa),
+ list(as.name("B"), a = expression(x+y)[[1]], b = 3))
[1] TRUE
> (a2 <- A(1,2, named = 3)) #A(1,2, named = 3)
B(a = x + y, named = 3)
> all.equal(as.list(a2),
+ list(as.name("B"), a = expression(x+y)[[1]], named = 3))
[1] TRUE
>
> CC <- function(...) match.call()
> DD <- function(...) CC(...)
> a3 <- DD(1,2,3)
> all.equal(as.list(a3),
+ list(as.name("CC"), 1, 2, 3))
[1] TRUE
>
> ## More dots issues: March 19 2001 -pd
> ## Didn't work up to and including 1.2.2
>
> f <- function(...) {
+ val <- match.call(expand.dots=FALSE)$...
+ x <- val[[1]]
+ eval.parent(substitute(missing(x)))
+ }
> g <- function(...) h(f(...))
> h <- function(...) list(...)
> k <- function(...) g(...)
> X <- k(a=)
> all.equal(X, list(TRUE))
[1] TRUE
>
> ## Bug PR#24
> f <- function(x,...) substitute(list(x,...))
> deparse(f(a, b)) == "list(a, b)" &&
+ deparse(f(b, a)) == "list(b, a)" &&
+ deparse(f(x, y)) == "list(x, y)" &&
+ deparse(f(y, x)) == "list(y, x)"
[1] TRUE
>
> tt <- function(x) { is.vector(x); deparse(substitute(x)) }
> a <- list(b=3); tt(a$b) == "a$b" # tends to break when ...
[1] TRUE
>
>
> ## Parser:
> 1 <
+ 2
[1] TRUE
> 2 <=
+ 3
[1] TRUE
> 4 >=
+ 3
[1] TRUE
> 3 >
+ 2
[1] TRUE
> 2 ==
+ 2
[1] TRUE
> ## bug till ...
> 1 !=
+ 3
[1] TRUE
>
> all(NULL == NULL)
[1] TRUE
>
> ## PR #656 (related)
> u <- runif(1); length(find(".Random.seed")) == 1
[1] TRUE
>
> MyVaR <<- "val";length(find("MyVaR")) == 1
[1] TRUE
> rm(MyVaR); length(find("MyVaR")) == 0
[1] TRUE
>
>
> ## Martin Maechler: rare bad bug in sys.function() {or match.arg()} (PR#1409)
> callme <- function(a = 1, mm = c("Abc", "Bde")) {
+ mm <- match.arg(mm); cat("mm = "); str(mm) ; invisible()
+ }
> ## The first two were as desired:
> callme()
mm = chr "Abc"
> callme(mm="B")
mm = chr "Bde"
> mycaller <- function(x = 1, callme = pi) { callme(x) }
> mycaller()## wrongly gave `mm = NULL' now = "Abc"
mm = chr "Abc"
>