-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathdemos.Rout.save
1514 lines (1207 loc) · 44.3 KB
/
demos.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
R version 3.3.1 RC (2016-06-14 r70774) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
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.
> #### Run all demos that do not depend on tcl and other specials.
> .ptime <- proc.time()
> set.seed(123)
> options(keep.source=TRUE, useFancyQuotes=FALSE, warn = 1)
>
> ## Drop these for strict testing {and add them to demos2.R)
> ## lm.glm is in ../src/library/utils/man/demo.Rd }:
> dont <- list(graphics = c("Hershey", "Japanese", "plotmath"),
+ stats = c("lm.glm", "nlm")
+ )
> ## don't take tcltk here
> for(pkg in c("base", "graphics", "stats")) {
+
+ demos <- list.files(file.path(system.file(package = pkg), "demo"),
+ pattern = "\\.R$")
+ demos <- demos[is.na(match(demos, paste(dont[[pkg]], "R",sep=".")))]
+
+ if(length(demos)) {
+ if(need <- pkg != "base" &&
+ !any((fpkg <- paste("package", pkg, sep=":")) == search()))
+ library(pkg, character.only = TRUE)
+
+ for(nam in sub("\\.R$", "", demos))
+ demo(nam, character.only = TRUE)
+
+ if(need) detach(pos = which(fpkg == search()))
+ }
+ }
demo(error.catching)
---- ~~~~~~~~~~~~~~
> ##================================================================##
> ### In longer simulations, aka computer experiments, ###
> ### you may want to ###
> ### 1) catch all errors and warnings (and continue) ###
> ### 2) store the error or warning messages ###
> ### ###
> ### Here's a solution (see R-help mailing list, Dec 9, 2010): ###
> ##================================================================##
>
> ##' Catch *and* save both errors and warnings, and in the case of
> ##' a warning, also keep the computed result.
> ##'
> ##' @title tryCatch both warnings (with value) and errors
> ##' @param expr an \R expression to evaluate
> ##' @return a list with 'value' and 'warning', where
> ##' 'value' may be an error caught.
> ##' @author Martin Maechler;
> ##' Copyright (C) 2010-2012 The R Core Team
> tryCatch.W.E <- function(expr)
+ {
+ W <- NULL
+ w.handler <- function(w){ # warning handler
+ W <<- w
+ invokeRestart("muffleWarning")
+ }
+ list(value = withCallingHandlers(tryCatch(expr, error = function(e) e),
+ warning = w.handler),
+ warning = W)
+ }
> str( tryCatch.W.E( log( 2 ) ) )
List of 2
$ value : num 0.693
$ warning: NULL
> str( tryCatch.W.E( log( -1) ) )
List of 2
$ value : num NaN
$ warning:List of 2
..$ message: chr "NaNs produced"
..$ call : language log(-1)
..- attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
> str( tryCatch.W.E( log("a") ) )
List of 2
$ value :List of 2
..$ message: chr "non-numeric argument to mathematical function"
..$ call : language log("a")
..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"
$ warning: NULL
demo(is.things)
---- ~~~~~~~~~
> # Copyright (C) 1997-2013 The R Core Team
>
> ## being a 'builtin' function is not the same as being in base
> ls.base <- ls("package:base", all=TRUE)
> base.is.f <- sapply(ls.base, function(x) is.function(get(x)))
> cat("\nNumber of base objects:\t\t", length(ls.base),
+ "\nNumber of functions in base:\t", sum(base.is.f),
+ "\n\t starting with 'is.' :\t ",
+ sum(grepl("^is\\.", ls.base[base.is.f])), "\n", sep = "")
Number of base objects: 1318
Number of functions in base: 1276
starting with 'is.' : 50
> ## 0.14 : 31
> ## 0.50 : 33
> ## 0.60 : 34
> ## 0.63 : 37
> ## 1.0.0 : 38
> ## 1.3.0 : 41
> ## 1.6.0 : 45
> ## 2.0.0 : 45
>
> ## Do we have a method (probably)?
> is.method <- function(fname) {
+ isFun <- function(name) (exists(name, mode="function") &&
+ is.na(match(name, c("is", "as"))))
+ np <- length(sp <- strsplit(fname, split = "\\.")[[1]])
+ if(np <= 1 )
+ FALSE
+ else
+ (isFun(paste(sp[1:(np-1)], collapse = '.')) ||
+ (np >= 3 &&
+ isFun(paste(sp[1:(np-2)], collapse = '.'))))
+ }
> is.ALL <- function(obj, func.names = ls(pos=length(search())),
+ not.using = c("is.single", "is.real", "is.loaded",
+ "is.empty.model", "is.R", "is.element", "is.unsorted"),
+ true.only = FALSE, debug = FALSE)
+ {
+ ## Purpose: show many 'attributes' of R object __obj__
+ ## -------------------------------------------------------------------------
+ ## Arguments: obj: any R object
+ ## -------------------------------------------------------------------------
+ ## Author: Martin Maechler, Date: 6 Dec 1996
+
+ is.fn <- func.names[substring(func.names,1,3) == "is."]
+ is.fn <- is.fn[substring(is.fn,1,7) != "is.na<-"]
+ use.fn <- is.fn[ is.na(match(is.fn, not.using))
+ & ! sapply(is.fn, is.method) ]
+
+ r <- if(true.only) character(0)
+ else structure(vector("list", length= length(use.fn)), names= use.fn)
+ for(f in use.fn) {
+ if(any(f == c("is.na", "is.finite"))) {
+ if(!is.list(obj) && !is.vector(obj) && !is.array(obj)) {
+ if(!true.only) r[[f]] <- NA
+ next
+ }
+ }
+ if(any(f == c("is.nan", "is.finite", "is.infinite"))) {
+ if(!is.atomic(obj)) {
+ if(!true.only) r[[f]] <- NA
+ next
+ }
+ }
+ if(debug) cat(f,"")
+ fn <- get(f)
+ rr <- if(is.primitive(fn) || length(formals(fn))>0) fn(obj) else fn()
+ if(!is.logical(rr)) cat("f=",f," --- rr is NOT logical = ",rr,"\n")
+ ##if(1!=length(rr)) cat("f=",f," --- rr NOT of length 1; = ",rr,"\n")
+ if(true.only && length(rr)==1 && !is.na(rr) && rr) r <- c(r, f)
+ else if(!true.only) r[[f]] <- rr
+ }
+ if(debug)cat("\n")
+ if(is.list(r)) structure(r, class = "isList") else r
+ }
> print.isList <- function(x, ..., verbose = getOption("verbose"))
+ {
+ ## Purpose: print METHOD for `isList' objects
+ ## ------------------------------------------------
+ ## Author: Martin Maechler, Date: 12 Mar 1997
+ if(is.list(x)) {
+ if(verbose) cat("print.isList(): list case (length=",length(x),")\n")
+ nm <- format(names(x))
+ rr <- lapply(x, stats::symnum, na = "NA")
+ for(i in seq_along(x)) cat(nm[i],":",rr[[i]],"\n", ...)
+ } else NextMethod("print", ...)
+ }
> is.ALL(NULL)
is.array : .
is.atomic : |
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : NA
is.function : .
is.infinite :
is.integer : .
is.language : .
is.list : .
is.logical : .
is.matrix : .
is.na : NA
is.name : .
is.nan :
is.null : |
is.numeric : .
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : |
is.primitive : .
is.qr : .
is.raw : .
is.recursive : .
is.symbol : .
is.table : .
is.vector : .
> ##fails: is.ALL(NULL, not.using = c("is.single", "is.loaded"))
> is.ALL(NULL, true.only = TRUE)
[1] "is.atomic" "is.null" "is.pairlist"
> all.equal(NULL, pairlist())
[1] TRUE
> ## list() != NULL == pairlist() :
> is.ALL(list(), true.only = TRUE)
[1] "is.list" "is.recursive" "is.vector"
> (pl <- is.ALL(pairlist(1, list(3,"A")), true.only = TRUE))
[1] "is.list" "is.pairlist" "is.recursive"
> (ll <- is.ALL( list(1,pairlist(3,"A")), true.only = TRUE))
[1] "is.list" "is.recursive" "is.vector"
> all.equal(pl[pl != "is.pairlist"],
+ ll[ll != "is.vector"])## TRUE
[1] TRUE
> is.ALL(1:5)
is.array : .
is.atomic : |
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : | | | | |
is.function : .
is.infinite : . . . . .
is.integer : |
is.language : .
is.list : .
is.logical : .
is.matrix : .
is.na : . . . . .
is.name : .
is.nan : . . . . .
is.null : .
is.numeric : |
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : .
is.symbol : .
is.table : .
is.vector : |
> is.ALL(array(1:24, 2:4))
is.array : |
is.atomic : |
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : | | | | | | | | | | | | | | | | | | | | | | | |
is.function : .
is.infinite : . . . . . . . . . . . . . . . . . . . . . . . .
is.integer : |
is.language : .
is.list : .
is.logical : .
is.matrix : .
is.na : . . . . . . . . . . . . . . . . . . . . . . . .
is.name : .
is.nan : . . . . . . . . . . . . . . . . . . . . . . . .
is.null : .
is.numeric : |
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : .
is.symbol : .
is.table : .
is.vector : .
> is.ALL(1 + 3)
is.array : .
is.atomic : |
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : |
is.environment : .
is.expression : .
is.factor : .
is.finite : |
is.function : .
is.infinite : .
is.integer : .
is.language : .
is.list : .
is.logical : .
is.matrix : .
is.na : .
is.name : .
is.nan : .
is.null : .
is.numeric : |
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : .
is.symbol : .
is.table : .
is.vector : |
> e13 <- expression(1 + 3)
> is.ALL(e13)
Warning in fn(obj) :
is.na() applied to non-(list or vector) of type 'expression'
is.array : .
is.atomic : .
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : |
is.factor : .
is.finite : NA
is.function : .
is.infinite : NA
is.integer : .
is.language : |
is.list : .
is.logical : .
is.matrix : .
is.na : .
is.name : .
is.nan : NA
is.null : .
is.numeric : .
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : |
is.symbol : .
is.table : .
is.vector : |
> is.ALL(substitute(expression(a + 3), list(a=1)), true.only = TRUE)
[1] "is.call" "is.language" "is.recursive"
> is.ALL(y ~ x) #--> NA for is.na & is.finite
is.array : .
is.atomic : .
is.call : |
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : NA
is.function : .
is.infinite : NA
is.integer : .
is.language : |
is.list : .
is.logical : .
is.matrix : .
is.na : NA
is.name : .
is.nan : NA
is.null : .
is.numeric : .
is.numeric_version : .
is.object : |
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : |
is.symbol : .
is.table : .
is.vector : .
> is0 <- is.ALL(numeric(0))
> is0.ok <- 1 == (lis0 <- sapply(is0, length))
> is0[!is0.ok]
$is.finite
logical(0)
$is.infinite
logical(0)
$is.na
logical(0)
$is.nan
logical(0)
> is0 <- unlist(is0)
> is0
is.array is.atomic is.call is.character
FALSE TRUE FALSE FALSE
is.complex is.data.frame is.double is.environment
FALSE FALSE TRUE FALSE
is.expression is.factor is.function is.integer
FALSE FALSE FALSE FALSE
is.language is.list is.logical is.matrix
FALSE FALSE FALSE FALSE
is.name is.null is.numeric is.numeric_version
FALSE FALSE TRUE FALSE
is.object is.ordered is.package_version is.pairlist
FALSE FALSE FALSE FALSE
is.primitive is.qr is.raw is.recursive
FALSE FALSE FALSE FALSE
is.symbol is.table is.vector
FALSE FALSE TRUE
> ispi <- unlist(is.ALL(pi))
> all(ispi[is0.ok] == is0)
[1] TRUE
> is.ALL(numeric(0), true=TRUE)
[1] "is.atomic" "is.double" "is.numeric" "is.vector"
> is.ALL(array(1,1:3), true=TRUE)
[1] "is.array" "is.atomic" "is.double" "is.numeric"
> is.ALL(cbind(1:3), true=TRUE)
[1] "is.array" "is.atomic" "is.integer" "is.matrix" "is.numeric"
> is.ALL(structure(1:7, names = paste("a",1:7,sep="")))
is.array : .
is.atomic : |
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : | | | | | | |
is.function : .
is.infinite : . . . . . . .
is.integer : |
is.language : .
is.list : .
is.logical : .
is.matrix : .
is.na : . . . . . . .
is.name : .
is.nan : . . . . . . .
is.null : .
is.numeric : |
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : .
is.symbol : .
is.table : .
is.vector : |
> is.ALL(structure(1:7, names = paste("a",1:7,sep="")), true.only = TRUE)
[1] "is.atomic" "is.integer" "is.numeric" "is.vector"
> x <- 1:20 ; y <- 5 + 6*x + rnorm(20)
> lm.xy <- lm(y ~ x)
> is.ALL(lm.xy)
is.array : .
is.atomic : .
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : NA
is.function : .
is.infinite : NA
is.integer : .
is.language : .
is.list : |
is.logical : .
is.matrix : .
is.na : . . . . . . . . . . . .
is.name : .
is.nan : NA
is.null : .
is.numeric : .
is.numeric_version : .
is.object : |
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : |
is.symbol : .
is.table : .
is.vector : .
> is.ALL(structure(1:7, names = paste("a",1:7,sep="")))
is.array : .
is.atomic : |
is.call : .
is.character : .
is.complex : .
is.data.frame : .
is.double : .
is.environment : .
is.expression : .
is.factor : .
is.finite : | | | | | | |
is.function : .
is.infinite : . . . . . . .
is.integer : |
is.language : .
is.list : .
is.logical : .
is.matrix : .
is.na : . . . . . . .
is.name : .
is.nan : . . . . . . .
is.null : .
is.numeric : |
is.numeric_version : .
is.object : .
is.ordered : .
is.package_version : .
is.pairlist : .
is.primitive : .
is.qr : .
is.raw : .
is.recursive : .
is.symbol : .
is.table : .
is.vector : |
> is.ALL(structure(1:7, names = paste("a",1:7,sep="")), true.only = TRUE)
[1] "is.atomic" "is.integer" "is.numeric" "is.vector"
demo(recursion)
---- ~~~~~~~~~
> # Copyright (C) 1997-2005 The R Core Team
>
> ## Adaptive integration: Venables and Ripley pp. 105-110
> ## This is the basic integrator.
>
> area <- function(f, a, b, ..., fa = f(a, ...), fb = f(b, ...), limit
+ = 10, eps = 1.e-5)
+ {
+ h <- b - a
+ d <- (a + b)/2
+ fd <- f(d, ...)
+ a1 <- ((fa + fb) * h)/2
+ a2 <- ((fa + 4 * fd + fb) * h)/6
+ if(abs(a1 - a2) < eps)
+ return(a2)
+ if(limit == 0) {
+ warning(paste("iteration limit reached near x = ",
+ d))
+ return(a2)
+ }
+ area(f, a, d, ..., fa = fa, fb = fd, limit = limit - 1,
+ eps = eps) + area(f, d, b, ..., fa = fd, fb =
+ fb, limit = limit - 1, eps = eps)
+ }
> ## The function to be integrated
>
> fbeta <- function(x, alpha, beta)
+ {
+ x^(alpha - 1) * (1 - x)^(beta - 1)
+ }
> ## Compute the approximate integral, the exact integral and the error
>
> b0 <- area(fbeta, 0, 1, alpha=3.5, beta=1.5)
> b1 <- exp(lgamma(3.5) + lgamma(1.5) - lgamma(5))
> c(b0, b1, b0-b1)
[1] 1.227170e-01 1.227185e-01 -1.443996e-06
> ## Modify the function so that it records where it was evaluated
>
> fbeta.tmp <- function (x, alpha, beta)
+ {
+ val <<- c(val, x)
+ x^(alpha - 1) * (1 - x)^(beta - 1)
+ }
> ## Recompute and plot the evaluation points.
>
> val <- NULL
> b0 <- area(fbeta.tmp, 0, 1, alpha=3.5, beta=1.5)
> plot(val, fbeta(val, 3.5, 1.5), pch=0)
> ## Better programming style -- renaming the function will have no effect.
> ## The use of "Recall" as in V+R is VERY black magic. You can get the
> ## same effect transparently by supplying a wrapper function.
> ## This is the approved Abelson+Sussman method.
>
> area <- function(f, a, b, ..., limit=10, eps=1e-5) {
+ area2 <- function(f, a, b, ..., fa = f(a, ...), fb = f(b, ...),
+ limit = limit, eps = eps) {
+ h <- b - a
+ d <- (a + b)/2
+ fd <- f(d, ...)
+ a1 <- ((fa + fb) * h)/2
+ a2 <- ((fa + 4 * fd + fb) * h)/6
+ if(abs(a1 - a2) < eps)
+ return(a2)
+ if(limit == 0) {
+ warning(paste("iteration limit reached near x =", d))
+ return(a2)
+ }
+ area2(f, a, d, ..., fa = fa, fb = fd, limit = limit - 1,
+ eps = eps) + area2(f, d, b, ..., fa = fd, fb =
+ fb, limit = limit - 1, eps = eps)
+ }
+ area2(f, a, b, ..., limit=limit, eps=eps)
+ }
demo(scoping)
---- ~~~~~~~
> ## Here is a little example which shows a fundamental difference between
> ## R and S. It is a little example from Abelson and Sussman which models
> ## the way in which bank accounts work. It shows how R functions can
> ## encapsulate state information.
> ##
> ## When invoked, "open.account" defines and returns three functions
> ## in a list. Because the variable "total" exists in the environment
> ## where these functions are defined they have access to its value.
> ## This is even true when "open.account" has returned. The only way
> ## to access the value of "total" is through the accessor functions
> ## withdraw, deposit and balance. Separate accounts maintain their
> ## own balances.
> ##
> ## This is a very nifty way of creating "closures" and a little thought
> ## will show you that there are many ways of using this in statistics.
>
> # Copyright (C) 1997-8 The R Core Team
>
> open.account <- function(total) {
+
+ list(
+ deposit = function(amount) {
+ if(amount <= 0)
+ stop("Deposits must be positive!\n")
+ total <<- total + amount
+ cat(amount,"deposited. Your balance is", total, "\n\n")
+ },
+ withdraw = function(amount) {
+ if(amount > total)
+ stop("You don't have that much money!\n")
+ total <<- total - amount
+ cat(amount,"withdrawn. Your balance is", total, "\n\n")
+ },
+ balance = function() {
+ cat("Your balance is", total, "\n\n")
+ }
+ )
+ }
> ross <- open.account(100)
> robert <- open.account(200)
> ross$withdraw(30)
30 withdrawn. Your balance is 70
> ross$balance()
Your balance is 70
> robert$balance()
Your balance is 200
> ross$deposit(50)
50 deposited. Your balance is 120
> ross$balance()
Your balance is 120
> try(ross$withdraw(500)) # no way..
Error in ross$withdraw(500) : You don't have that much money!
demo(graphics)
---- ~~~~~~~~
> # Copyright (C) 1997-2009 The R Core Team
>
> require(datasets)
> require(grDevices); require(graphics)
> ## Here is some code which illustrates some of the differences between
> ## R and S graphics capabilities. Note that colors are generally specified
> ## by a character string name (taken from the X11 rgb.txt file) and that line
> ## textures are given similarly. The parameter "bg" sets the background
> ## parameter for the plot and there is also an "fg" parameter which sets
> ## the foreground color.
>
>
> x <- stats::rnorm(50)
> opar <- par(bg = "white")
> plot(x, ann = FALSE, type = "n")
> abline(h = 0, col = gray(.90))
> lines(x, col = "green4", lty = "dotted")
> points(x, bg = "limegreen", pch = 21)
> title(main = "Simple Use of Color In a Plot",
+ xlab = "Just a Whisper of a Label",
+ col.main = "blue", col.lab = gray(.8),
+ cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)
> ## A little color wheel. This code just plots equally spaced hues in
> ## a pie chart. If you have a cheap SVGA monitor (like me) you will
> ## probably find that numerically equispaced does not mean visually
> ## equispaced. On my display at home, these colors tend to cluster at
> ## the RGB primaries. On the other hand on the SGI Indy at work the
> ## effect is near perfect.
>
> par(bg = "gray")
> pie(rep(1,24), col = rainbow(24), radius = 0.9)
> title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)
> title(xlab = "(Use this as a test of monitor linearity)",
+ cex.lab = 0.8, font.lab = 3)
> ## We have already confessed to having these. This is just showing off X11
> ## color names (and the example (from the postscript manual) is pretty "cute".
>
> pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
> names(pie.sales) <- c("Blueberry", "Cherry",
+ "Apple", "Boston Cream", "Other", "Vanilla Cream")
> pie(pie.sales,
+ col = c("purple","violetred1","green3","cornsilk","cyan","white"))
> title(main = "January Pie Sales", cex.main = 1.8, font.main = 1)
> title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3)
> ## Boxplots: I couldn't resist the capability for filling the "box".
> ## The use of color seems like a useful addition, it focuses attention
> ## on the central bulk of the data.
>
> par(bg="cornsilk")
> n <- 10
> g <- gl(n, 100, n*100)
> x <- rnorm(n*100) + sqrt(as.numeric(g))
> boxplot(split(x,g), col="lavender", notch=TRUE)
> title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1)
> ## An example showing how to fill between curves.
>
> par(bg="white")
> n <- 100
> x <- c(0,cumsum(rnorm(n)))
> y <- c(0,cumsum(rnorm(n)))
> xx <- c(0:n, n:0)
> yy <- c(x, rev(y))
> plot(xx, yy, type="n", xlab="Time", ylab="Distance")
> polygon(xx, yy, col="gray")
> title("Distance Between Brownian Motions")
> ## Colored plot margins, axis labels and titles. You do need to be
> ## careful with these kinds of effects. It's easy to go completely
> ## over the top and you can end up with your lunch all over the keyboard.
> ## On the other hand, my market research clients love it.
>
> x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.05, 2.02)
> par(bg="lightgray")
> plot(x, type="n", axes=FALSE, ann=FALSE)
> usr <- par("usr")
> rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")
> lines(x, col="blue")
> points(x, pch=21, bg="lightcyan", cex=1.25)
> axis(2, col.axis="blue", las=1)
> axis(1, at=1:12, lab=month.abb, col.axis="blue")
> box()
> title(main= "The Level of Interest in R", font.main=4, col.main="red")
> title(xlab= "1996", col.lab="red")
> ## A filled histogram, showing how to change the font used for the
> ## main title without changing the other annotation.
>
> par(bg="cornsilk")
> x <- rnorm(1000)
> hist(x, xlim=range(-4, 4, x), col="lavender", main="")
> title(main="1000 Normal Random Variates", font.main=3)
> ## A scatterplot matrix
> ## The good old Iris data (yet again)
>
> pairs(iris[1:4], main="Edgar Anderson's Iris Data", font.main=4, pch=19)
> pairs(iris[1:4], main="Edgar Anderson's Iris Data", pch=21,
+ bg = c("red", "green3", "blue")[unclass(iris$Species)])
> ## Contour plotting
> ## This produces a topographic map of one of Auckland's many volcanic "peaks".
>
> x <- 10*1:nrow(volcano)
> y <- 10*1:ncol(volcano)
> lev <- pretty(range(volcano), 10)
> par(bg = "lightcyan")
> pin <- par("pin")
> xdelta <- diff(range(x))
> ydelta <- diff(range(y))
> xscale <- pin[1]/xdelta
> yscale <- pin[2]/ydelta
> scale <- min(xscale, yscale)
> xadd <- 0.5*(pin[1]/scale - xdelta)
> yadd <- 0.5*(pin[2]/scale - ydelta)
> plot(numeric(0), numeric(0),
+ xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
+ type = "n", ann = FALSE)
> usr <- par("usr")
> rect(usr[1], usr[3], usr[2], usr[4], col="green3")
> contour(x, y, volcano, levels = lev, col="yellow", lty="solid", add=TRUE)
> box()
> title("A Topographic Map of Maunga Whau", font= 4)
> title(xlab = "Meters North", ylab = "Meters West", font= 3)
> mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE,
+ at = mean(par("usr")[1:2]), cex=0.7, font=3)
> ## Conditioning plots
>
> par(bg="cornsilk")
> coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3")
> par(opar)
demo(image)
---- ~~~~~
> # Copyright (C) 1997-2009 The R Core Team
>
> require(datasets)
> require(grDevices); require(graphics)
> x <- 10*(1:nrow(volcano)); x.at <- seq(100, 800, by=100)
> y <- 10*(1:ncol(volcano)); y.at <- seq(100, 600, by=100)
> # Using Terrain Colors
>