# HG changeset patch
# User Volker Braun <vbraun@stp.dias.ie>
# Date 1321906436 0
# Node ID 9d5ffb2a960288ed69b7078338fc8c0e38af9092
# Parent 9276337e5cd9643ceb185388014bd03858b4cb86
Trac #12057: Fix doctests related to R upgrade
diff --git a/sage/interfaces/r.py b/sage/interfaces/r.py
a
|
b
|
|
11 | 11 | The simplest data structure in R is the numeric vector which |
12 | 12 | consists of an ordered collection of numbers. To create a |
13 | 13 | vector named $x$ using the R interface in Sage, you pass the |
14 | | R interpreter object a list or tuple of numbers. |
| 14 | R interpreter object a list or tuple of numbers:: |
| 15 | |
15 | 16 | sage: x = r([10.4,5.6,3.1,6.4,21.7]); x |
16 | 17 | [1] 10.4 5.6 3.1 6.4 21.7 |
17 | 18 | |
18 | 19 | You can invert elements of a vector x in R by using the |
19 | | invert operator or by doing 1/x. |
| 20 | invert operator or by doing 1/x:: |
| 21 | |
20 | 22 | sage: ~x |
21 | 23 | [1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295 |
22 | 24 | sage: 1/x |
23 | 25 | [1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295 |
24 | 26 | |
25 | 27 | The following assignment creates a vector $y$ with 11 entries which |
26 | | consists of two copies of $x$ with a 0 in between. |
| 28 | consists of two copies of $x$ with a 0 in between:: |
| 29 | |
27 | 30 | sage: y = r([x,0,x]); y |
28 | 31 | [1] 10.4 5.6 3.1 6.4 21.7 0.0 10.4 5.6 3.1 6.4 21.7 |
29 | 32 | |
… |
… |
|
31 | 34 | |
32 | 35 | The following command generates a new vector $v$ of length 11 constructed |
33 | 36 | by adding together (element by element) $2x$ repeated 2.2 times, $y$ |
34 | | repeated just once, and 1 repeated 11 times. |
| 37 | repeated just once, and 1 repeated 11 times:: |
| 38 | |
35 | 39 | sage: v = 2*x+y+1; v |
36 | 40 | [1] 32.2 17.8 10.3 20.2 66.1 21.8 22.6 12.8 16.9 50.8 43.5 |
37 | 41 | |
38 | 42 | One can compute the sum of the elements of an R vector in the following |
39 | | two ways: |
| 43 | two ways:: |
| 44 | |
40 | 45 | sage: sum(x) |
41 | 46 | [1] 47.2 |
42 | 47 | sage: x.sum() |
43 | 48 | [1] 47.2 |
44 | 49 | |
45 | | One can calculate the sample variance of a list of numbers: |
| 50 | One can calculate the sample variance of a list of numbers:: |
| 51 | |
46 | 52 | sage: ((x-x.mean())^2/(x.length()-1)).sum() |
47 | 53 | [1] 53.853 |
48 | 54 | sage: x.var() |
49 | 55 | [1] 53.853 |
50 | 56 | |
51 | | |
52 | 57 | sage: x.sort() |
53 | 58 | [1] 3.1 5.6 6.4 10.4 21.7 |
54 | 59 | sage: x.min() |
… |
… |
|
58 | 63 | sage: x |
59 | 64 | [1] 10.4 5.6 3.1 6.4 21.7 |
60 | 65 | |
61 | | |
62 | | |
63 | 66 | sage: r(-17).sqrt() |
64 | 67 | [1] NaN |
65 | 68 | sage: r('-17+0i').sqrt() |
66 | 69 | [1] 0+4.123106i |
67 | 70 | |
68 | | Generating Regular Sequences |
| 71 | Generating an arithmetic sequence:: |
69 | 72 | |
70 | 73 | sage: r('1:10') |
71 | 74 | [1] 1 2 3 4 5 6 7 8 9 10 |
72 | 75 | |
73 | 76 | Because 'from' is a keyword in Python, it can't be used |
74 | 77 | as a keyword argument. Instead, 'from_' can be passed, and |
75 | | R will recognize it as the correct thing. |
| 78 | R will recognize it as the correct thing:: |
| 79 | |
76 | 80 | sage: r.seq(length=10, from_=-1, by=.2) |
77 | 81 | [1] -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 |
78 | 82 | |
… |
… |
|
84 | 88 | sage: x.rep(each=2) |
85 | 89 | [1] 10.4 10.4 5.6 5.6 3.1 3.1 6.4 6.4 21.7 21.7 |
86 | 90 | |
87 | | Missing Values |
| 91 | Missing Values:: |
| 92 | |
88 | 93 | sage: na = r('NA') |
89 | 94 | sage: z = r([1,2,3,na]) |
90 | 95 | sage: z |
… |
… |
|
114 | 119 | [1] FALSE |
115 | 120 | |
116 | 121 | |
117 | | Character Vectors |
| 122 | Character Vectors:: |
118 | 123 | |
119 | 124 | sage: labs = r.paste('c("X","Y")', '1:10', sep='""'); labs |
120 | 125 | [1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10" |
121 | 126 | |
122 | 127 | |
123 | | Index vectors; selecting and modifying subsets of a data set |
| 128 | Index vectors; selecting and modifying subsets of a data set:: |
124 | 129 | |
125 | 130 | sage: na = r('NA') |
126 | 131 | sage: x = r([10.4,5.6,3.1,6.4,21.7,na]); x |
… |
… |
|
128 | 133 | sage: x['!is.na(self)'] |
129 | 134 | [1] 10.4 5.6 3.1 6.4 21.7 |
130 | 135 | |
131 | | |
132 | 136 | sage: x = r([10.4,5.6,3.1,6.4,21.7,na]); x |
133 | 137 | [1] 10.4 5.6 3.1 6.4 21.7 NA |
134 | 138 | sage: (x+1)['(!is.na(self)) & self>0'] |
… |
… |
|
138 | 142 | sage: (x+1)['(!is.na(self)) & self>0'] |
139 | 143 | [1] 11.4 4.1 0.5 22.7 |
140 | 144 | |
141 | | Distributions |
| 145 | Distributions:: |
142 | 146 | |
143 | 147 | sage: r.options(width="60"); |
144 | 148 | $width |
… |
… |
|
163 | 167 | [57] 0.013582969 0.010420935 0.007915452 0.005952532 |
164 | 168 | [61] 0.004431848 |
165 | 169 | |
166 | | Convert R Data Structures to Python/Sage |
| 170 | Convert R Data Structures to Python/Sage:: |
167 | 171 | |
168 | 172 | sage: rr = r.dnorm(r.seq(-3,3,0.1)) |
169 | 173 | sage: sum(rr._sage_()) |
170 | | 9.9772125168981081 |
| 174 | 9.97721251689810... |
171 | 175 | |
172 | | Or you get a dictionary to be able to access all the information. |
| 176 | Or you get a dictionary to be able to access all the information:: |
173 | 177 | |
174 | 178 | sage: rs = r.summary(r.c(1,4,3,4,3,2,5,1)) |
175 | 179 | sage: rs |
… |
… |
|
181 | 185 | sage: d['_Names'] |
182 | 186 | ['Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.'] |
183 | 187 | sage: d['_r_class'] |
184 | | 'table' |
| 188 | ['summaryDefault', 'table'] |
185 | 189 | |
186 | 190 | It is also possible to access the plotting capabilities of R |
187 | 191 | through Sage. For more information see the documentation of |
… |
… |
|
447 | 451 | |
448 | 452 | |
449 | 453 | def _quit_string(self): |
450 | | """ |
| 454 | r""" |
451 | 455 | Return the string that when typed into R causes the R |
452 | 456 | interpreter to exit. |
453 | 457 | |
454 | | EXAMPLES: |
| 458 | EXAMPLES:: |
| 459 | |
455 | 460 | sage: r._quit_string() |
456 | 461 | 'quit(save="no")' |
457 | 462 | """ |
458 | 463 | return 'quit(save="no")' |
459 | 464 | |
460 | 465 | def _read_in_file_command(self, filename): |
461 | | """ |
462 | | Returns the R command (as a string) to read in a file named |
| 466 | r""" |
| 467 | Return the R command (as a string) to read in a file named |
463 | 468 | filename into the R interpreter. |
464 | 469 | |
465 | | EXAMPLES: |
| 470 | EXAMPLES:: |
| 471 | |
466 | 472 | sage: r._read_in_file_command('file.txt') |
467 | | 'source(file=file("file.txt",open="r"))' |
| 473 | 'file=file("file.txt",open="r")\nsource(file)' |
468 | 474 | """ |
469 | | return 'source(file=file("%s",open="r"))'%filename |
| 475 | return 'file=file("%s",open="r")\nsource(file)'%filename |
470 | 476 | |
471 | 477 | def read(self, filename): |
472 | | """ |
473 | | Reads filename into the R interpreter by calling R's source function on a |
| 478 | r""" |
| 479 | Read filename into the R interpreter by calling R's source function on a |
474 | 480 | read-only file connection. |
475 | 481 | |
476 | | EXAMPLES: |
| 482 | EXAMPLES:: |
| 483 | |
477 | 484 | sage: filename = tmp_filename() |
478 | 485 | sage: f = open(filename, 'w') |
479 | 486 | sage: f.write('a <- 2+2\n') |
… |
… |
|
526 | 533 | |
527 | 534 | def version(self): |
528 | 535 | """ |
529 | | Returns the version of R currently running. |
| 536 | Return the version of R currently running. |
530 | 537 | |
531 | 538 | OUTPUT: |
532 | | tuple of ints; string |
533 | 539 | |
534 | | EXAMPLES: |
| 540 | tuple of ints; string |
| 541 | |
| 542 | EXAMPLES:: |
| 543 | |
535 | 544 | sage: r.version() |
536 | | ((2, 10, 1), 'R version 2.10.1 (2009-12-14)') |
| 545 | ((2, 14, 0), 'R version 2.14.0 (2011-10-31)') |
537 | 546 | """ |
538 | 547 | major_re = re.compile('^major\s*(\d.*?)$', re.M) |
539 | 548 | minor_re = re.compile('^minor\s*(\d.*?)$', re.M) |
… |
… |
|
1650 | 1659 | sage: rs = r.summary(r.c(1,4,3,4,3,2,5,1)) |
1651 | 1660 | sage: d = rs._sage_() |
1652 | 1661 | sage: list(sorted(d.items())) |
1653 | | [('DATA', [1, 1.75, 3, 2.875, 4, 5]), |
1654 | | ('_Names', ['Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.']), |
1655 | | ('_r_class', 'table')] |
| 1662 | [('DATA', [1, 1.75, 3, 2.875, 4, 5]), |
| 1663 | ('_Names', ['Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.']), |
| 1664 | ('_r_class', ['summaryDefault', 'table'])] |
1656 | 1665 | """ |
1657 | 1666 | self._check_valid() |
1658 | 1667 | P = self.parent() |
… |
… |
|
1698 | 1707 | exp = rel_re_param.sub(self._subs_dots, exp) |
1699 | 1708 | |
1700 | 1709 | # Rename class since it is a Python keyword |
1701 | | exp = re.sub(' class = "', ' _r_class = "',exp) |
| 1710 | exp = re.sub(' class = ', ' _r_class = ',exp) |
1702 | 1711 | |
1703 | 1712 | # Change 'structure' to '_r_structure' |
1704 | 1713 | # TODO: check that we are outside of quotes "" |
… |
… |
|
1945 | 1954 | |
1946 | 1955 | def r_version(): |
1947 | 1956 | """ |
1948 | | EXAMPLES: |
| 1957 | Return the R version. |
| 1958 | |
| 1959 | EXAMPLES:: |
| 1960 | |
1949 | 1961 | sage: r.version() |
1950 | | ((2, 10, 1), 'R version 2.10.1 (2009-12-14)') |
| 1962 | ((2, 14, 0), 'R version 2.14.0 (2011-10-31)') |
1951 | 1963 | """ |
1952 | 1964 | return r.version() |
1953 | 1965 | |