Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/interfaces/r.py
8814 views
1
r"""
2
Interface to R
3
4
The following examples try to follow "An Introduction to R" which can
5
be found at http://cran.r-project.org/doc/manuals/R-intro.html .
6
7
EXAMPLES:
8
9
Simple manipulations; numbers and vectors
10
11
The simplest data structure in R is the numeric vector which
12
consists of an ordered collection of numbers. To create a
13
vector named $x$ using the R interface in Sage, you pass the
14
R interpreter object a list or tuple of numbers::
15
16
sage: x = r([10.4,5.6,3.1,6.4,21.7]); x
17
[1] 10.4 5.6 3.1 6.4 21.7
18
19
You can invert elements of a vector x in R by using the
20
invert operator or by doing 1/x::
21
22
sage: ~x
23
[1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
24
sage: 1/x
25
[1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
26
27
The following assignment creates a vector $y$ with 11 entries which
28
consists of two copies of $x$ with a 0 in between::
29
30
sage: y = r([x,0,x]); y
31
[1] 10.4 5.6 3.1 6.4 21.7 0.0 10.4 5.6 3.1 6.4 21.7
32
33
Vector Arithmetic
34
35
The following command generates a new vector $v$ of length 11 constructed
36
by adding together (element by element) $2x$ repeated 2.2 times, $y$
37
repeated just once, and 1 repeated 11 times::
38
39
sage: v = 2*x+y+1; v
40
[1] 32.2 17.8 10.3 20.2 66.1 21.8 22.6 12.8 16.9 50.8 43.5
41
42
One can compute the sum of the elements of an R vector in the following
43
two ways::
44
45
sage: sum(x)
46
[1] 47.2
47
sage: x.sum()
48
[1] 47.2
49
50
One can calculate the sample variance of a list of numbers::
51
52
sage: ((x-x.mean())^2/(x.length()-1)).sum()
53
[1] 53.853
54
sage: x.var()
55
[1] 53.853
56
57
sage: x.sort()
58
[1] 3.1 5.6 6.4 10.4 21.7
59
sage: x.min()
60
[1] 3.1
61
sage: x.max()
62
[1] 21.7
63
sage: x
64
[1] 10.4 5.6 3.1 6.4 21.7
65
66
sage: r(-17).sqrt()
67
[1] NaN
68
sage: r('-17+0i').sqrt()
69
[1] 0+4.123106i
70
71
Generating an arithmetic sequence::
72
73
sage: r('1:10')
74
[1] 1 2 3 4 5 6 7 8 9 10
75
76
Because ``from`` is a keyword in Python, it can't be used
77
as a keyword argument. Instead, ``from_`` can be passed, and
78
R will recognize it as the correct thing::
79
80
sage: r.seq(length=10, from_=-1, by=.2)
81
[1] -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8
82
83
sage: x = r([10.4,5.6,3.1,6.4,21.7]);
84
sage: x.rep(2)
85
[1] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
86
sage: x.rep(times=2)
87
[1] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7
88
sage: x.rep(each=2)
89
[1] 10.4 10.4 5.6 5.6 3.1 3.1 6.4 6.4 21.7 21.7
90
91
Missing Values::
92
93
sage: na = r('NA')
94
sage: z = r([1,2,3,na])
95
sage: z
96
[1] 1 2 3 NA
97
sage: ind = r.is_na(z)
98
sage: ind
99
[1] FALSE FALSE FALSE TRUE
100
sage: zero = r(0)
101
sage: zero / zero
102
[1] NaN
103
sage: inf = r('Inf')
104
sage: inf-inf
105
[1] NaN
106
sage: r.is_na(inf)
107
[1] FALSE
108
sage: r.is_na(inf-inf)
109
[1] TRUE
110
sage: r.is_na(zero/zero)
111
[1] TRUE
112
sage: r.is_na(na)
113
[1] TRUE
114
sage: r.is_nan(inf-inf)
115
[1] TRUE
116
sage: r.is_nan(zero/zero)
117
[1] TRUE
118
sage: r.is_nan(na)
119
[1] FALSE
120
121
122
Character Vectors::
123
124
sage: labs = r.paste('c("X","Y")', '1:10', sep='""'); labs
125
[1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"
126
127
128
Index vectors; selecting and modifying subsets of a data set::
129
130
sage: na = r('NA')
131
sage: x = r([10.4,5.6,3.1,6.4,21.7,na]); x
132
[1] 10.4 5.6 3.1 6.4 21.7 NA
133
sage: x['!is.na(self)']
134
[1] 10.4 5.6 3.1 6.4 21.7
135
136
sage: x = r([10.4,5.6,3.1,6.4,21.7,na]); x
137
[1] 10.4 5.6 3.1 6.4 21.7 NA
138
sage: (x+1)['(!is.na(self)) & self>0']
139
[1] 11.4 6.6 4.1 7.4 22.7
140
sage: x = r([10.4,-2,3.1,-0.5,21.7,na]); x
141
[1] 10.4 -2.0 3.1 -0.5 21.7 NA
142
sage: (x+1)['(!is.na(self)) & self>0']
143
[1] 11.4 4.1 0.5 22.7
144
145
Distributions::
146
147
sage: r.options(width="60");
148
$width
149
[1] 100
150
151
sage: rr = r.dnorm(r.seq(-3,3,0.1))
152
sage: rr
153
[1] 0.004431848 0.005952532 0.007915452 0.010420935
154
[5] 0.013582969 0.017528300 0.022394530 0.028327038
155
[9] 0.035474593 0.043983596 0.053990967 0.065615815
156
[13] 0.078950158 0.094049077 0.110920835 0.129517596
157
[17] 0.149727466 0.171368592 0.194186055 0.217852177
158
[21] 0.241970725 0.266085250 0.289691553 0.312253933
159
[25] 0.333224603 0.352065327 0.368270140 0.381387815
160
[29] 0.391042694 0.396952547 0.398942280 0.396952547
161
[33] 0.391042694 0.381387815 0.368270140 0.352065327
162
[37] 0.333224603 0.312253933 0.289691553 0.266085250
163
[41] 0.241970725 0.217852177 0.194186055 0.171368592
164
[45] 0.149727466 0.129517596 0.110920835 0.094049077
165
[49] 0.078950158 0.065615815 0.053990967 0.043983596
166
[53] 0.035474593 0.028327038 0.022394530 0.017528300
167
[57] 0.013582969 0.010420935 0.007915452 0.005952532
168
[61] 0.004431848
169
170
Convert R Data Structures to Python/Sage::
171
172
sage: rr = r.dnorm(r.seq(-3,3,0.1))
173
sage: sum(rr._sage_())
174
9.9772125168981...
175
176
Or you get a dictionary to be able to access all the information::
177
178
sage: rs = r.summary(r.c(1,4,3,4,3,2,5,1))
179
sage: rs
180
Min. 1st Qu. Median Mean 3rd Qu. Max.
181
1.000 1.750 3.000 2.875 4.000 5.000
182
sage: d = rs._sage_()
183
sage: d['DATA']
184
[1, 1.75, 3, 2.875, 4, 5]
185
sage: d['_Names']
186
['Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.']
187
sage: d['_r_class']
188
['summaryDefault', 'table']
189
190
It is also possible to access the plotting capabilities of R
191
through Sage. For more information see the documentation of
192
r.plot() or r.png().
193
194
AUTHORS:
195
196
- Mike Hansen (2007-11-01)
197
- William Stein (2008-04-19)
198
- Harald Schilly (2008-03-20)
199
- Mike Hansen (2008-04-19)
200
"""
201
202
##########################################################################
203
#
204
# Copyright (C) 2007 William Stein <[email protected]>
205
# 2007 Mike Hansen <[email protected]>
206
# 2008 Harald Schilly <[email protected]>
207
#
208
# Distributed under the terms of the GNU General Public License (GPL)
209
#
210
# http://www.gnu.org/licenses/
211
#
212
##########################################################################
213
214
from expect import Expect, ExpectElement, ExpectFunction, FunctionElement
215
from sage.misc.misc import DOT_SAGE
216
import re
217
import sage.rings.integer
218
219
COMMANDS_CACHE = '%s/r_commandlist.sobj'%DOT_SAGE
220
PROMPT = '__SAGE__R__PROMPT__> '
221
prompt_re = re.compile("^>", re.M)
222
223
#there is a mirror network, but lets take #1 for now
224
RRepositoryURL = "http://cran.r-project.org/"
225
RFilteredPackages = ['.GlobalEnv']
226
227
# crosscheck with https://svn.r-project.org/R/trunk/src/main/names.c
228
# but package:base should cover this. i think.
229
RBaseCommands = ['c', "NULL", "NA", "True", "False", "Inf", "NaN"]
230
231
class R(Expect):
232
def __init__(self,
233
maxread=100000, script_subdirectory=None,
234
server_tmpdir = None,
235
logfile=None,
236
server=None,
237
init_list_length=1024):
238
"""
239
An interface to the R interpreter.
240
241
R is a comprehensive collection of methods for statistics,
242
modelling, bioinformatics, data analysis and much more.
243
For more details, see http://www.r-project.org/about.html
244
245
Resources:
246
247
* http://r-project.org/ provides more information about R.
248
* http://rseek.org/ R's own search engine.
249
250
EXAMPLES::
251
252
sage: r.summary(r.c(1,2,3,111,2,3,2,3,2,5,4))
253
Min. 1st Qu. Median Mean 3rd Qu. Max.
254
1.00 2.00 3.00 12.55 3.50 111.00
255
256
TESTS::
257
258
sage: r == loads(dumps(r))
259
True
260
"""
261
Expect.__init__(self,
262
263
# The capitalized version of this is used for printing.
264
name = 'r',
265
266
# This is regexp of the input prompt. If you can change
267
# it to be very obfuscated that would be better. Even
268
# better is to use sequence numbers.
269
# options(prompt=\"<prompt> \")
270
prompt = '> ', #default, later comes the change
271
272
# This is the command that starts up your program
273
command = "R --vanilla --quiet",
274
275
maxread = maxread,
276
277
server=server,
278
server_tmpdir=server_tmpdir,
279
280
script_subdirectory = script_subdirectory,
281
282
# If this is true, then whenever the user presses Control-C to
283
# interrupt a calculation, the whole interface is restarted.
284
restart_on_ctrlc = False,
285
286
# If true, print out a message when starting
287
# up the command when you first send a command
288
# to this interface.
289
verbose_start = False,
290
291
logfile=logfile,
292
293
# If an input is longer than this number of characters, then
294
# try to switch to outputting to a file.
295
eval_using_file_cutoff=1024)
296
297
self.__seq = 0
298
self.__var_store_len = 0
299
self.__init_list_length = init_list_length
300
self._prompt_wait = [self._prompt]
301
302
def _start(self):
303
"""
304
Start up the R interpreter and sets the initial prompt and options.
305
306
This is called the first time the R interface is actually used.
307
308
EXAMPLES::
309
310
sage: r = R()
311
sage: r._start()
312
"""
313
Expect._start(self)
314
315
# width is line width, what's a good value? maximum is 10000!
316
# pager needed to replace help view from less to printout
317
# option device= is for plotting, is set to x11, NULL would be better?
318
self._change_prompt(PROMPT)
319
self.eval('options(prompt=\"%s\",continue=\"%s\", width=100,pager="cat",device="png")'%(PROMPT, PROMPT))
320
self.expect().expect(PROMPT)
321
self.eval('options(repos="%s")'%RRepositoryURL)
322
self.eval('options(CRAN="%s")'%RRepositoryURL)
323
324
# don't abort on errors, just raise them!
325
# necessary for non-interactive execution
326
self.eval('options(error = expression(NULL))')
327
328
def png(self, *args, **kwds):
329
"""
330
Creates an R PNG device.
331
332
This should primarily be used to save an R graphic to a custom file. Note
333
that when using this in the notebook, one must plot in the same cell that
334
one creates the device. See r.plot() documentation for more information
335
about plotting via R in Sage.
336
337
These examples won't work on the many platforms where R still gets
338
built without graphics support.
339
340
EXAMPLES::
341
342
sage: filename = tmp_filename() + '.png'
343
sage: r.png(filename='"%s"'%filename) # optional -- rgraphics
344
NULL
345
sage: x = r([1,2,3])
346
sage: y = r([4,5,6])
347
sage: r.plot(x,y) # This saves to filename, but is not viewable from command line; optional -- rgraphics
348
null device
349
1
350
sage: import os; os.unlink(filename) # We remove the file for doctesting; optional -- rgraphics
351
352
We want to make sure that we actually can view R graphics, which happens
353
differently on different platforms::
354
355
sage: s = r.eval('capabilities("png")') # Should be on Linux and Solaris
356
sage: t = r.eval('capabilities("aqua")') # Should be on all supported Mac versions
357
sage: "TRUE" in s+t # optional -- rgraphics
358
True
359
"""
360
#Check to see if R has PNG support
361
s = self.eval('capabilities("png")')
362
t = r.eval('capabilities("aqua")')
363
if "TRUE" not in s+t:
364
raise RuntimeError, "R was not compiled with PNG support"
365
366
from sage.server.support import EMBEDDED_MODE
367
if EMBEDDED_MODE:
368
self.setwd('"%s"'%os.path.abspath('.'))
369
return RFunction(self, 'png')(*args, **kwds)
370
371
def convert_r_list(self, l):
372
r"""
373
Converts an R list to a Python list.
374
375
EXAMPLES::
376
377
sage: s = 'c(".GlobalEnv", "package:stats", "package:graphics", "package:grDevices", \n"package:utils", "package:datasets", "package:methods", "Autoloads", \n"package:base")'
378
sage: r.convert_r_list(s)
379
['.GlobalEnv',
380
'package:stats',
381
'package:graphics',
382
'package:grDevices',
383
'package:utils',
384
'package:datasets',
385
'package:methods',
386
'Autoloads',
387
'package:base']
388
"""
389
pl = []
390
l = "".join(l.split("\n"))
391
l = l[2:len(l)-1]
392
for l in l.split(","):
393
pl.append(l.strip().strip('"'))
394
return pl
395
396
def install_packages(self, package_name):
397
"""
398
Install an R package into Sage's R installation.
399
400
EXAMPLES::
401
402
sage: r.install_packages('aaMI') # not tested
403
...
404
R is free software and comes with ABSOLUTELY NO WARRANTY.
405
You are welcome to redistribute it under certain conditions.
406
Type 'license()' or 'licence()' for distribution details.
407
...
408
Please restart Sage in order to use 'aaMI'.
409
"""
410
cmd = """options(repos="%s"); install.packages("%s")"""%(RRepositoryURL, package_name)
411
os.system("time echo '%s' | R --vanilla"%cmd)
412
print "Please restart Sage in order to use '%s'."%package_name
413
414
def __repr__(self):
415
"""
416
Return string representation of this R interface.
417
418
EXAMPLES::
419
420
sage: r.__repr__()
421
'R Interpreter'
422
"""
423
return 'R Interpreter'
424
425
def __reduce__(self):
426
"""
427
Used in serializing an R interface.
428
429
EXAMPLES::
430
431
sage: rlr, t = r.__reduce__()
432
sage: rlr(*t)
433
R Interpreter
434
"""
435
return reduce_load_R, tuple([])
436
437
def __getattr__(self, attrname):
438
"""
439
Called when you get an attribute of the R interface. This
440
manufactures an R function, which is a Python function that
441
can then be called with various inputs.
442
443
EXAMPLES::
444
445
sage: c = r.c; c
446
c
447
sage: type(c)
448
<class 'sage.interfaces.r.RFunction'>
449
"""
450
if attrname[:1] == "_":
451
raise AttributeError
452
return RFunction(self, attrname)
453
454
455
def _quit_string(self):
456
r"""
457
Return the string that when typed into R causes the R
458
interpreter to exit.
459
460
EXAMPLES::
461
462
sage: r._quit_string()
463
'quit(save="no")'
464
"""
465
return 'quit(save="no")'
466
467
def _read_in_file_command(self, filename):
468
r"""
469
Return the R command (as a string) to read in a file named
470
filename into the R interpreter.
471
472
EXAMPLES::
473
474
sage: r._read_in_file_command('file.txt')
475
'file=file("file.txt",open="r")\nsource(file)'
476
"""
477
return 'file=file("%s",open="r")\nsource(file)'%filename
478
479
def read(self, filename):
480
r"""
481
Read filename into the R interpreter by calling R's source function on a
482
read-only file connection.
483
484
EXAMPLES::
485
486
sage: filename = tmp_filename()
487
sage: f = open(filename, 'w')
488
sage: f.write('a <- 2+2\n')
489
sage: f.close()
490
sage: r.read(filename)
491
sage: r.get('a')
492
'[1] 4'
493
"""
494
self.eval( self._read_in_file_command(filename) )
495
496
def _install_hints(self):
497
"""
498
EXAMPLES::
499
500
sage: print r._install_hints()
501
R is currently installed with Sage.
502
"""
503
return "R is currently installed with Sage.\n"
504
505
def _source(self, s):
506
"""
507
Returns the source code of an R function as a string.
508
509
INPUT:
510
511
- s -- the name of the function as a string
512
513
EXAMPLES::
514
515
sage: print r._source("print.anova")
516
function (x, digits = max(getOption("digits") - 2L, 3L), signif.stars = getOption("show.signif.stars"),
517
...
518
"""
519
if s[-2:] == "()":
520
s = s[-2:]
521
return self.eval('%s'%s)
522
523
def source(self, s):
524
"""
525
Display the R source (if possible) about the function named s.
526
527
INPUT:
528
529
- s -- a string representing the function whose source code you want to see
530
531
OUTPUT: string -- source code
532
533
EXAMPLES::
534
535
sage: print r.source("print.anova")
536
function (x, digits = max(getOption("digits") - 2L, 3L), signif.stars = getOption("show.signif.stars"),
537
...
538
"""
539
return self._source(s)
540
541
def version(self):
542
"""
543
Return the version of R currently running.
544
545
OUTPUT: tuple of ints; string
546
547
EXAMPLES::
548
549
sage: r.version() # not tested
550
((3, 0, 1), 'R version 3.0.1 (2013-05-16)')
551
sage: rint, rstr = r.version()
552
sage: rint[0] >= 3
553
True
554
sage: rstr.startswith('R version')
555
True
556
"""
557
major_re = re.compile('^major\s*(\d.*?)$', re.M)
558
minor_re = re.compile('^minor\s*(\d.*?)$', re.M)
559
version_string_re = re.compile('^version.string\s*(R.*?)$', re.M)
560
561
s = self.eval('version')
562
563
major = int( major_re.findall(s)[0].strip() )
564
minor = tuple(int(i) for i in minor_re.findall(s)[0].strip().split(".") )
565
version_string = version_string_re.findall(s)[0].strip()
566
567
return ( (major,) + minor, version_string )
568
569
def library(self, library_name):
570
"""
571
Load the library library_name into the R interpreter.
572
573
This function raises an ImportError if the given library
574
is not known.
575
576
INPUT:
577
578
- library_name -- string
579
580
EXAMPLES::
581
582
sage: r.library('grid')
583
sage: 'grid' in r.eval('(.packages())')
584
True
585
sage: r.library('foobar')
586
Traceback (most recent call last):
587
...
588
ImportError: ...
589
"""
590
ret = self.eval('require("%s")'%library_name)
591
# try hard to parse the message string in a locale-independent way
592
if ' library(' in ret: # locale-independent key-word
593
raise ImportError, "%s"%ret
594
else:
595
try:
596
# We need to rebuild keywords!
597
del self.__trait_names
598
except AttributeError:
599
pass
600
self.trait_names(verbose=False, use_disk_cache=False)
601
602
require = library #overwrites require
603
604
def available_packages(self):
605
"""
606
Returns a list of all available R package names.
607
608
This list is not necessarily sorted.
609
610
OUTPUT: list of strings
611
612
.. note::
613
614
This requires an internet connection. The CRAN server is
615
that is checked is defined at the top of sage/interfaces/r.py.
616
617
EXAMPLES::
618
619
sage: ap = r.available_packages() # optional - internet
620
sage: len(ap) > 20 #optional
621
True
622
"""
623
p = self.new('available.packages("%s/src/contrib")'%RRepositoryURL)
624
s = str(p).splitlines()[1:]
625
v = [x.split()[0].strip("'") for x in s]
626
return v
627
#The following was more structural, but breaks on my machine. (stein)
628
#p = p._sage_()
629
#s = p['_Dim'][0]
630
#l = [[p['DATA'][i],p['DATA'][s+1+i]] for i in xrange(0,s)]
631
#return l
632
633
def _object_class(self):
634
"""
635
Return the underlying class of elements of the R interface object.
636
637
OUTPUT: a Python class
638
639
EXAMPLES::
640
641
sage: r._object_class()
642
<class 'sage.interfaces.r.RElement'>
643
"""
644
return RElement
645
646
def _true_symbol(self):
647
"""
648
Return the symbol that represents True in R.
649
650
OUTPUT: string
651
652
EXAMPLES::
653
654
sage: r._true_symbol()
655
'[1] TRUE'
656
657
This is used behinds the scenes to implement comparison::
658
659
sage: r('1') == r('1')
660
[1] TRUE
661
sage: r('1') == r('2')
662
[1] FALSE
663
"""
664
# return the string rep of truth, i.e., what the system outputs
665
# when you type 1==1.
666
return "[1] TRUE"
667
668
def _false_symbol(self):
669
"""
670
Return the symbol that represents True in R.
671
672
OUTPUT: string
673
674
EXAMPLES::
675
676
sage: r._false_symbol()
677
'[1] FALSE'
678
"""
679
# return the string rep of false, i.e., what the system outputs
680
# when you type 1==2.
681
return "[1] FALSE"
682
683
def _equality_symbol(self):
684
"""
685
EXAMPLES::
686
687
sage: r._equality_symbol()
688
'=='
689
"""
690
# return the symbol for checking equality, e.g., == or eq.
691
return "=="
692
693
def help(self, command):
694
"""
695
Returns help string for a given command.
696
697
INPUT:
698
- command -- a string
699
700
OUTPUT: HelpExpression -- a subclass of string whose __repr__ method is __str__, so it prints nicely
701
702
EXAMPLES::
703
704
sage: r.help('print.anova')
705
anova package:stats R Documentation
706
...
707
Chambers, J. M. and Hastie, T. J. (1992) _Statistical Models in
708
S_, Wadsworth & Brooks/Cole.
709
...
710
711
.. note::
712
713
This is similar to typing r.command?.
714
"""
715
s = self.eval('help("%s")'%command).strip() # ?cmd is only an unsafe shortcut
716
import sage.plot.plot
717
if sage.plot.plot.EMBEDDED_MODE:
718
s = s.replace('_\x08','')
719
return HelpExpression(s)
720
721
def _assign_symbol(self):
722
"""
723
Return the symbol used in R for assignment, which is ' <- '.
724
725
OUTPUT: string
726
727
EXAMPLES::
728
729
sage: r._assign_symbol()
730
' <- '
731
"""
732
return " <- "
733
734
def _left_list_delim(self):
735
"""
736
Return the left delimiter for lists in R, which is 'c('
737
738
OUTPUT: string
739
740
EXAMPLES::
741
742
sage: r._left_list_delim()
743
'c('
744
"""
745
return "c("
746
747
def _right_list_delim(self):
748
"""
749
Return the right delimiter for lists in R, which is 'c('
750
751
OUTPUT: string
752
753
EXAMPLES::
754
755
sage: r._right_list_delim()
756
')'
757
"""
758
return ")"
759
760
def console(self):
761
"""
762
Runs the R console as a separate new R process.
763
764
EXAMPLES::
765
766
sage: r.console() # not tested
767
R version 2.6.1 (2007-11-26)
768
Copyright (C) 2007 The R Foundation for Statistical Computing
769
ISBN 3-900051-07-0
770
...
771
"""
772
r_console()
773
774
def function_call(self, function, args=None, kwds=None):
775
"""
776
Return the result of calling an R function, with given args and keyword args.
777
778
OUTPUT: RElement -- an object in R
779
780
EXAMPLES::
781
782
sage: r.function_call('length', args=[ [1,2,3] ])
783
[1] 3
784
"""
785
args, kwds = self._convert_args_kwds(args, kwds)
786
self._check_valid_function_name(function)
787
return self.new("%s(%s)"%(function, ",".join([s.name() for s in args] +
788
[self._sage_to_r_name(key)+'='+kwds[key].name() for key in kwds ] )))
789
790
def call(self, function_name, *args, **kwds):
791
r"""
792
This is an alias for :meth:`function_call`.
793
794
EXAMPLES::
795
796
sage: r.call('length', [1,2,3])
797
[1] 3
798
"""
799
return self.function_call(function_name, args=args, kwds=kwds)
800
801
def _an_element_impl(self):
802
"""
803
Returns an element belonging to the R interpreter. This is used
804
behind the scenes when doing things like comparisons, etc.
805
806
OUTPUT: RElement -- an R element.
807
808
EXAMPLES::
809
810
sage: r._an_element_impl()
811
[1] 0
812
sage: type(_)
813
<class 'sage.interfaces.r.RElement'>
814
"""
815
return self(0)
816
817
def set(self, var, value):
818
"""
819
Set the variable var in R to what the string value evaluates to in R.
820
821
INPUT:
822
823
- var -- a string
824
- value -- a string
825
826
EXAMPLES::
827
828
sage: r.set('a', '2 + 3')
829
sage: r.get('a')
830
'[1] 5'
831
"""
832
cmd = '%s <- %s'%(var,value)
833
out = self.eval(cmd)
834
if out.find("error") != -1:
835
raise TypeError, "Error executing code in R\nCODE:\n\t%s\nR ERROR:\n\t%s"%(cmd, out)
836
837
def get(self, var):
838
"""
839
Returns the string representation of the variable var.
840
841
INPUT:
842
843
- var -- a string
844
845
OUTPUT: string
846
847
EXAMPLES::
848
849
sage: r.set('a', 2)
850
sage: r.get('a')
851
'[1] 2'
852
"""
853
s = self.eval('%s'%var)
854
#return self._remove_indices_re.sub("", s).strip()
855
return s
856
857
def na(self):
858
"""
859
Returns the NA in R.
860
861
OUTPUT: RElement -- an element of R
862
863
EXAMPLES::
864
865
sage: r.na()
866
[1] NA
867
"""
868
return self('NA')
869
870
def completions(self, s):
871
"""
872
Return all commands names that complete the command starting with the
873
string s. This is like typing s[Ctrl-T] in the R interpreter.
874
875
INPUT:
876
877
- s -- string
878
879
OUTPUT: list -- a list of strings
880
881
EXAMPLES::
882
883
sage: dummy = r.trait_names(use_disk_cache=False) #clean doctest
884
sage: r.completions('tes')
885
['testInheritedMethods', 'testPlatformEquivalence', 'testVirtual']
886
"""
887
return [name for name in self.trait_names() if name[:len(s)] == s]
888
889
def _commands(self):
890
"""
891
Return list of all commands defined in R.
892
893
OUTPUT: list -- a sorted list of strings
894
895
EXAMPLES::
896
897
sage: l = r._commands()
898
sage: 'AIC' in l
899
True
900
sage: len(l) > 200
901
True
902
"""
903
v = RBaseCommands
904
905
ll = self.eval('dput(search())') # loaded libs
906
ll = self.convert_r_list(ll)
907
908
for lib in ll:
909
if lib in RFilteredPackages:
910
continue
911
912
if lib.find("package:") != 0:
913
continue #only packages
914
915
raw = self.eval('dput(objects("%s"))'%lib)
916
raw = self.convert_r_list(raw)
917
raw = [x.replace(".","_") for x in raw]
918
919
#TODO are there others? many of them are shortcuts or
920
#should be done on another level, like selections in lists
921
#instead of calling obj.[[( fun-args) or other crazy stuff like that
922
923
#TODO further filtering, check if strings are now
924
#really functions, in R: exists(s, mode = "function"))
925
# (apply to vector with sapply(vec,func))
926
927
#filter only python compatible identifiers
928
valid = re.compile('[^a-zA-Z0-9_]+')
929
raw = [x for x in raw if valid.search(x) is None]
930
v += raw
931
932
v.sort()
933
return v
934
935
def trait_names(self, verbose=True, use_disk_cache=True):
936
"""
937
Return list of all R functions.
938
939
INPUT:
940
941
- verbose -- bool (default: True); if True, display debugging information
942
- use_disk_cache -- bool (default: True); if True, use the disk cache of
943
trait names to save time.
944
945
OUTPUT: list -- list of string
946
947
EXAMPLES::
948
949
sage: t = r.trait_names(verbose=False)
950
sage: len(t) > 200
951
True
952
"""
953
try:
954
return self.__trait_names
955
except AttributeError:
956
import sage.misc.persist
957
if use_disk_cache:
958
try:
959
self.__trait_names = sage.misc.persist.load(COMMANDS_CACHE)
960
return self.__trait_names
961
except IOError:
962
pass
963
if verbose and use_disk_cache:
964
print "\nBuilding R command completion list (this takes"
965
print "a few seconds only the first time you do it)."
966
print "To force rebuild later, delete %s."%COMMANDS_CACHE
967
v = self._commands()
968
self.__trait_names = v
969
if len(v) > 200 and use_disk_cache:
970
sage.misc.persist.save(v, COMMANDS_CACHE)
971
return v
972
973
def plot(self, *args, **kwds):
974
"""
975
The R plot function. Type r.help('plot') for much more extensive
976
documentation about this function. See also below for a brief
977
introduction to more plotting with R.
978
979
If one simply wants to view an R graphic, using this function is
980
is sufficient (because it calls dev.off() to turn off the device).
981
982
However, if one wants to save the graphic to a specific file, it
983
should be used as in the example below to write the output.
984
985
EXAMPLES:
986
987
This example saves a plot to the standard R output, usually
988
a filename like ``Rplot001.png`` - from the command line, in
989
the current directory, and in the cell directory in the notebook::
990
991
sage: d=r.setwd('"%s"'%SAGE_TMP) # for doctesting only; ignore if you are trying this;
992
sage: r.plot("1:10") # optional -- rgraphics
993
null device
994
1
995
996
To save to a specific file name, one should use :meth:`png` to set
997
the output device to that file. If this is done in the notebook, it
998
must be done in the same cell as the plot itself::
999
1000
sage: filename = tmp_filename() + '.png'
1001
sage: r.png(filename='"%s"'%filename) # Note the double quotes in single quotes!; optional -- rgraphics
1002
NULL
1003
sage: x = r([1,2,3])
1004
sage: y = r([4,5,6])
1005
sage: r.plot(x,y) # optional -- rgraphics
1006
null device
1007
1
1008
sage: import os; os.unlink(filename) # For doctesting, we remove the file; optional -- rgraphics
1009
1010
Please note that for more extensive use of R's plotting
1011
capabilities (such as the lattices package), it is advisable
1012
to either use an interactive plotting device or to use the
1013
notebook. The following examples are not tested, because they
1014
differ depending on operating system::
1015
1016
sage: r.X11() # not tested - opens interactive device on systems with X11 support
1017
sage: r.quartz() # not tested - opens interactive device on OSX
1018
sage: r.hist("rnorm(100)") # not tested - makes a plot
1019
sage: r.library("lattice") # not tested - loads R lattice plotting package
1020
sage: r.histogram(x = "~ wt | cyl", data="mtcars") # not tested - makes a lattice plot
1021
sage: r.dev_off() # not tested, turns off the interactive viewer
1022
1023
In the notebook, one can use r.png() to open the device, but
1024
would need to use the following since R lattice graphics do
1025
not automatically print away from the command line::
1026
1027
sage: filename = tmp_filename() + '.png' # Not needed in notebook, used for doctesting
1028
sage: r.png(filename='"%s"'%filename) # filename not needed in notebook, used for doctesting; optional -- rgraphics
1029
NULL
1030
sage: r.library("lattice")
1031
sage: r("print(histogram(~wt | cyl, data=mtcars))") # plot should appear; optional -- rgraphics
1032
sage: import os; os.unlink(filename) # We remove the file for doctesting, not needed in notebook; optional -- rgraphics
1033
"""
1034
# We have to define this to override the plot function defined in the
1035
# superclass.
1036
from sage.server.support import EMBEDDED_MODE
1037
if EMBEDDED_MODE:
1038
self.setwd('"%s"'%os.path.abspath('.'))
1039
RFunction(self, 'plot')(*args, **kwds)
1040
return RFunction(self, 'dev.off')()
1041
1042
def _strip_prompt(self, code):
1043
"""
1044
Remove the standard R prompt from the beginning of lines in code.
1045
1046
INPUT:
1047
1048
- code -- a string
1049
1050
OUTPUT: a string
1051
1052
EXAMPLES::
1053
1054
sage: s = '> a <- 2\n> b <- 3'
1055
sage: r._strip_prompt(s)
1056
' a <- 2\n b <- 3'
1057
"""
1058
return prompt_re.sub("", code)
1059
1060
def eval(self, code, globals=None, locals=None, synchronize=True, *args, **kwds):
1061
"""
1062
Evaluates a command inside the R interpreter and returns the output
1063
as a string.
1064
1065
EXAMPLES::
1066
1067
sage: r.eval('1+1')
1068
'[1] 2'
1069
"""
1070
# TODO split code at ";" outside of quotes and send them as individual
1071
# lines without ";".
1072
return Expect.eval(self, code, synchronize=synchronize, *args, **kwds)
1073
1074
def _r_to_sage_name(self, s):
1075
"""
1076
Returns a Sage/Python identifier from an R one. This involves
1077
replacing periods with underscores, <- with __, and prepending
1078
_ in front of Python keywords.
1079
1080
INPUT:
1081
1082
- s -- a string
1083
1084
OUTPUT: a string
1085
1086
EXAMPLES::
1087
1088
sage: f = r._r_to_sage_name
1089
sage: f('t.test')
1090
't_test'
1091
sage: f('attr<-')
1092
'attr__'
1093
sage: f('parent.env<-')
1094
'parent_env__'
1095
sage: f('class')
1096
'class_'
1097
"""
1098
from keyword import iskeyword
1099
s = s.replace('.', '_')
1100
s = s.replace('<-', '__')
1101
if iskeyword(s):
1102
s += '_'
1103
return s
1104
1105
def _sage_to_r_name(self, s):
1106
r"""
1107
The reverse of :meth:`_r_to_sage_name`. See the docs for that method.
1108
1109
EXAMPLES::
1110
1111
sage: f = r._sage_to_r_name
1112
sage: f('t_test')
1113
't.test'
1114
sage: f('attr__')
1115
'attr<-'
1116
sage: f('parent_env__')
1117
'parent.env<-'
1118
sage: r._r_to_sage_name(f('parent_env__'))
1119
'parent_env__'
1120
sage: f('class_')
1121
'class'
1122
"""
1123
if len(s) > 1 and s[-2:] == "__":
1124
s = s[:-2] + '<-'
1125
if len(s) > 0 and s[-1] == '_':
1126
s = s[:-1]
1127
s = s.replace('_', '.')
1128
return s
1129
1130
def __getitem__(self, s):
1131
"""
1132
Returns the RFunction with name s.
1133
1134
INPUT:
1135
1136
- s -- a string
1137
OUTPUT: RFunction -- the R function that in R has name s
1138
1139
EXAMPLES::
1140
1141
sage: r['as.data.frame']
1142
as.data.frame
1143
sage: r['print']
1144
print
1145
"""
1146
return RFunction(self, s, r_name=True)
1147
1148
def chdir(self, dir):
1149
"""
1150
Changes the working directory to ``dir``
1151
1152
INPUT:
1153
1154
- ``dir`` -- the directory to change to.
1155
1156
EXAMPLES::
1157
1158
sage: import tempfile
1159
sage: tmpdir = tempfile.mkdtemp()
1160
sage: r.chdir(tmpdir)
1161
1162
Check that ``tmpdir`` and ``r.getwd()`` refer to the same
1163
directory. We need to use ``realpath()`` in case ``$TMPDIR``
1164
(by default ``/tmp``) is a symbolic link (see :trac:`10264`).
1165
1166
::
1167
1168
sage: os.path.realpath(tmpdir) == sageobj(r.getwd()) # known bug (:trac:`9970`)
1169
True
1170
"""
1171
self.execute('setwd(%r)' % dir)
1172
1173
1174
# patterns for _sage_()
1175
rel_re_param = re.compile('\s([\w\.]+)\s=')
1176
rel_re_xrange = re.compile('([\d]+):([\d]+)')
1177
rel_re_integer = re.compile('([^\d])([\d]+)L')
1178
rel_re_terms = re.compile('terms\s*=\s*(.*?),')
1179
rel_re_call = re.compile('call\s*=\s*(.*?)\),')
1180
1181
class RElement(ExpectElement):
1182
def __reduce__(self):
1183
"""
1184
EXAMPLES::
1185
1186
sage: a = r([1,2,3])
1187
sage: dumps(a)
1188
Traceback (most recent call last):
1189
...
1190
NotImplementedError: pickling of R elements is not yet supported
1191
"""
1192
raise NotImplementedError, "pickling of R elements is not yet supported"
1193
1194
def trait_names(self):
1195
"""
1196
Return a list of all methods of this object.
1197
1198
.. note::
1199
1200
Currently returns all R commands.
1201
1202
EXAMPLES::
1203
1204
sage: a = r([1,2,3])
1205
sage: t = a.trait_names()
1206
sage: len(t) > 200
1207
True
1208
"""
1209
# TODO: rewrite it, just take methods(class=class(self))
1210
return self.parent().trait_names()
1211
1212
def tilde(self, x):
1213
"""
1214
The tilde regression operator in R.
1215
1216
EXAMPLES::
1217
1218
sage: x = r([1,2,3,4,5])
1219
sage: y = r([3,5,7,9,11])
1220
sage: a = r.lm( y.tilde(x) ) # lm( y ~ x )
1221
sage: d = a._sage_()
1222
sage: d['DATA']['coefficients']['DATA'][1]
1223
2
1224
"""
1225
parent = self.parent()
1226
rx = parent(x)
1227
return parent.new("%s ~ %s"%(self.name(), rx.name()))
1228
1229
stat_model = tilde
1230
1231
def __len__(self):
1232
"""
1233
Return the length of this object.
1234
1235
OUTPUT: integer
1236
1237
EXAMPLES::
1238
1239
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1240
sage: len(x)
1241
5
1242
"""
1243
return int(self.parent().eval('dput(length(%s))'%self.name())[:-1] )
1244
1245
def __getattr__(self, attrname):
1246
"""
1247
Return attribute of this object, which is an R function with this object
1248
as the first input.
1249
1250
INPUT:
1251
1252
- attrname -- string
1253
1254
OUTPUT: RFunctionElement
1255
1256
EXAMPLES::
1257
1258
sage: x = r([1,2,3])
1259
sage: length = x.length
1260
sage: type(length)
1261
<class 'sage.interfaces.r.RFunctionElement'>
1262
sage: length()
1263
[1] 3
1264
"""
1265
self._check_valid()
1266
if attrname[:1] == "_":
1267
raise AttributeError
1268
return RFunctionElement(self, attrname)
1269
1270
def __getitem__(self, n):
1271
"""
1272
Return element(s) of self.
1273
1274
INPUT:
1275
1276
- n -- an integer, a tuple, a string that makes sense to R, or an RElement
1277
1278
OUTPUT: RElement
1279
1280
EXAMPLES::
1281
1282
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1283
sage: x[0]
1284
numeric(0)
1285
sage: x[1]
1286
[1] 10.4
1287
sage: x[-1]
1288
[1] 5.6 3.1 6.4 21.7
1289
sage: x[-2]
1290
[1] 10.4 3.1 6.4 21.7
1291
sage: x[-3]
1292
[1] 10.4 5.6 6.4 21.7
1293
sage: x['c(2,3)']
1294
[1] 5.6 3.1
1295
sage: key = r.c(2,3)
1296
sage: x[key]
1297
[1] 5.6 3.1
1298
sage: m = r.array('1:3',r.c(2,4,2))
1299
sage: m
1300
, , 1
1301
[,1] [,2] [,3] [,4]
1302
[1,] 1 3 2 1
1303
[2,] 2 1 3 2
1304
, , 2
1305
[,1] [,2] [,3] [,4]
1306
[1,] 3 2 1 3
1307
[2,] 1 3 2 1
1308
sage: m[1,2,2]
1309
[1] 2
1310
sage: m[1,r.c(1,2),1]
1311
[1] 1 3
1312
"""
1313
P = self._check_valid()
1314
if isinstance(n, basestring):
1315
n = n.replace('self', self._name)
1316
return P.new('%s[%s]'%(self._name, n))
1317
elif (hasattr(n,'parent') and n.parent() is P): # the key is RElement itself
1318
return P.new('%s[%s]'%(self._name, n.name()))
1319
elif not isinstance(n,tuple):
1320
return P.new('%s[%s]'%(self._name, n))
1321
else:
1322
L = []
1323
for i in xrange(len(n)):
1324
if (hasattr(n[i],'parent') and n[i].parent() is P):
1325
L.append(n[i].name())
1326
else:
1327
L.append(str(n[i]))
1328
return P.new('%s[%s]'%(self._name, ','.join(L)))
1329
1330
def __nonzero__(self):
1331
"""
1332
Implements bool(self).
1333
1334
.. note::
1335
1336
bool(self) will only return True if self == 0 contains a FALSE in its representation.
1337
1338
EXAMPLES::
1339
1340
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1341
sage: bool(x)
1342
True
1343
sage: y = r([0,0,0,0])
1344
sage: bool(y)
1345
False
1346
sage: bool(r(0))
1347
False
1348
sage: bool(r(1))
1349
True
1350
"""
1351
return "FALSE" in repr(self==0)
1352
1353
def _comparison(self, other, symbol):
1354
"""
1355
Used to implement comparison of two objects.
1356
1357
INPUT:
1358
1359
- other -- RElement
1360
- symbol -- string
1361
1362
OUTPUT: RElement -- output is an R element; not a bool!
1363
1364
TESTS::
1365
1366
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1367
sage: x._comparison(10.4, "==")
1368
[1] TRUE FALSE FALSE FALSE FALSE
1369
"""
1370
P = self.parent()
1371
other = P(other)
1372
return P('%s %s %s'%(self.name(), symbol, other.name()))
1373
1374
def __eq__(self, other):
1375
"""
1376
Equality testing term by term.
1377
1378
INPUT:
1379
1380
- other -- RElement
1381
1382
OUTPUT: RElement -- an R element; not a bool!
1383
1384
EXAMPLES:
1385
1386
Notice that comparison is term by term and returns an R element. ::
1387
1388
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1389
sage: x == 10.4
1390
[1] TRUE FALSE FALSE FALSE FALSE
1391
"""
1392
return self._comparison(other, "==")
1393
1394
def __lt__(self, other):
1395
"""
1396
Less than testing term by term.
1397
1398
INPUT:
1399
1400
- other -- RElement
1401
1402
OUTPUT: RElement -- an R element; not a bool!
1403
1404
EXAMPLES:
1405
1406
Notice that comparison is term by term and returns an R element. ::
1407
1408
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1409
sage: x < 7
1410
[1] FALSE TRUE TRUE TRUE FALSE
1411
"""
1412
return self._comparison(other, "<")
1413
1414
def __gt__(self, other):
1415
"""
1416
Greater than testing term by term.
1417
1418
INPUT:
1419
1420
- other -- RElement
1421
1422
OUTPUT: RElement -- an R element; not a bool!
1423
1424
EXAMPLES:
1425
1426
Notice that comparison is term by term and returns an R element. ::
1427
1428
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1429
sage: x > 8
1430
[1] TRUE FALSE FALSE FALSE TRUE
1431
"""
1432
return self._comparison(other, ">")
1433
1434
def __le__(self, other):
1435
"""
1436
Less than or equal testing term by term.
1437
1438
INPUT:
1439
1440
- other -- RElement
1441
1442
OUTPUT: RElement -- an R element; not a bool!
1443
1444
EXAMPLES::
1445
1446
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1447
sage: x <= 10.4
1448
[1] TRUE TRUE TRUE TRUE FALSE
1449
"""
1450
return self._comparison(other, "<=")
1451
1452
def __ge__(self, other):
1453
"""
1454
Greater than or equal testing term by term.
1455
1456
INPUT:
1457
1458
- other -- RElement
1459
1460
OUTPUT: RElement -- an R element; not a bool!
1461
1462
EXAMPLES::
1463
1464
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1465
sage: x >= 10.4
1466
[1] TRUE FALSE FALSE FALSE TRUE
1467
"""
1468
return self._comparison(other, ">=")
1469
1470
def __ne__(self, other):
1471
"""
1472
Not equal testing term by term.
1473
1474
INPUT:
1475
1476
- other -- RElement
1477
1478
OUTPUT: RElement -- an R element; not a bool!
1479
1480
EXAMPLES::
1481
1482
sage: x = r([10.4,5.6,3.1,6.4,21.7])
1483
sage: x != 10.4
1484
[1] FALSE TRUE TRUE TRUE TRUE
1485
1486
"""
1487
return self._comparison(other, "!=")
1488
1489
def __cmp__(self, other):
1490
r"""
1491
Return 0, 1, or -1 depending on how self and other compare.
1492
1493
This is *not* called by the comparison operators, which
1494
do term-by-term comparison and return R elements.
1495
1496
INPUT:
1497
1498
- self, other -- R elements
1499
1500
OUTPUT: 0, 1, or -1
1501
1502
EXAMPLES::
1503
1504
sage: one = r(1)
1505
sage: two = r(2)
1506
sage: cmp(one,one)
1507
0
1508
sage: cmp(one,two)
1509
-1
1510
sage: cmp(two,one)
1511
1
1512
"""
1513
P = self.parent()
1514
if P.eval("%s %s %s"%(self.name(), P._equality_symbol(),
1515
other.name())) == P._true_symbol():
1516
return 0
1517
elif P.eval("%s %s %s"%(self.name(), P._lessthan_symbol(), other.name())) == P._true_symbol():
1518
return -1
1519
elif P.eval("%s %s %s"%(self.name(), P._greaterthan_symbol(), other.name())) == P._true_symbol():
1520
return 1
1521
else:
1522
return -1 # everything is supposed to be comparable in Python, so we define
1523
# the comparison thus when no comparable in interfaced system.
1524
1525
1526
def dot_product(self, other):
1527
"""
1528
Implements the notation self . other.
1529
1530
INPUT:
1531
1532
- self, other -- R elements
1533
1534
OUTPUT: R element
1535
1536
EXAMPLES::
1537
1538
sage: c = r.c(1,2,3,4)
1539
sage: c.dot_product(c.t())
1540
[,1] [,2] [,3] [,4]
1541
[1,] 1 2 3 4
1542
[2,] 2 4 6 8
1543
[3,] 3 6 9 12
1544
[4,] 4 8 12 16
1545
1546
sage: v = r([3,-1,8])
1547
sage: v.dot_product(v)
1548
[,1]
1549
[1,] 74
1550
"""
1551
P = self._check_valid()
1552
Q = P(other)
1553
# the R operator is %*% for matrix multiplication
1554
return P('%s %%*%% %s'%(self.name(), Q.name()))
1555
1556
def _subs_dots(self, x):
1557
"""
1558
Replace dots by underscores; used internally to implement
1559
conversation from R expression to Sage objects.
1560
1561
INPUT:
1562
1563
- x -- regular expression match: ``<type '_sre.SRE_Match'>``
1564
1565
OUTPUT: string
1566
1567
EXAMPLES::
1568
1569
sage: import re
1570
sage: a = r([1,2,3])
1571
sage: rel_re_param = re.compile('\s([\w\.]+)\s=')
1572
sage: rel_re_param.sub(a._subs_dots, ' test.test =')
1573
' test_test ='
1574
"""
1575
return x.group().replace('.','_')
1576
1577
def _subs_xrange(self, x):
1578
"""
1579
Change endpoints of xranges. This is used internally in the
1580
code for converting R expressions to Sage objects.
1581
1582
INPUT:
1583
1584
- x -- regular expression match: ``<type '_sre.SRE_Match'>``
1585
1586
OUTPUT: string
1587
1588
EXAMPLES::
1589
1590
sage: import re
1591
sage: a = r([1,2,3])
1592
sage: rel_re_xrange = re.compile('([\d]+):([\d]+)')
1593
sage: rel_re_xrange.sub(a._subs_xrange, ' 1:10')
1594
' xrange(1,11)'
1595
"""
1596
g = x.groups()
1597
g1 = int(g[1]) + 1
1598
return 'xrange(%s,%s)'%(g[0],g1)
1599
1600
def _subs_integer(self, x):
1601
"""
1602
Replaces strings like 'dL' with 'Integer(d)' where d is some
1603
integer. This is used internally in the code for converting R
1604
expressions to Sage objects.
1605
1606
EXAMPLES::
1607
1608
sage: import re
1609
sage: a = r([1,2,3])
1610
sage: rel_re_integer = re.compile('([^\d])([\d]+)L')
1611
sage: rel_re_integer.sub(a._subs_integer, ' 1L 2L')
1612
' Integer(1) Integer(2)'
1613
sage: rel_re_integer.sub(a._subs_integer, '1L 2L')
1614
'1L Integer(2)'
1615
1616
"""
1617
return '%sInteger(%s)'%x.groups()
1618
1619
def _convert_nested_r_list(self, exp):
1620
"""
1621
Converts a string representing a (possibly) nested list in R
1622
to a (possibly) nested Python list. This is used internally
1623
in the code for converting R expressions to Sage objects.
1624
1625
INPUT:
1626
1627
- exp -- a string
1628
1629
OUTPUT: a string
1630
1631
EXAMPLES::
1632
1633
sage: a = r([1,2,3])
1634
sage: s = 'c(1, 2, 3)'
1635
sage: a._convert_nested_r_list(s)
1636
'[1, 2, 3]'
1637
"""
1638
from re import compile as re_compile
1639
from re import split as re_split
1640
splt = re_compile('(c\(|\(|\))') # c( or ( or )
1641
lvl = 0
1642
ret = []
1643
for token in re_split(splt, exp):
1644
if token == 'c(':
1645
ret.append('[')
1646
lvl += 1
1647
elif token == '(':
1648
ret.append(token)
1649
if lvl > 0 : lvl += 1
1650
elif token == ')':
1651
if lvl == 1:
1652
ret.append(']')
1653
lvl -= 1
1654
else:
1655
ret.append(token)
1656
if lvl > 0:
1657
lvl -= 1
1658
else:
1659
ret.append(token)
1660
1661
return ''.join(ret)
1662
1663
1664
def _r_list(self, *args, **kwds):
1665
"""
1666
This is used internally in the code for converting R
1667
expressions to Sage objects.
1668
1669
EXAMPLES::
1670
1671
sage: a = r([1,2,3])
1672
sage: list(sorted(a._r_list(1,2,3,k=5).items()))
1673
[('#0', 1), ('#1', 2), ('#2', 3), ('k', 5)]
1674
"""
1675
ret = dict(kwds)
1676
i = 0
1677
for k in args:
1678
ret['#%s'%i] = k
1679
i += 1
1680
return ret
1681
1682
def _r_structure(self, __DATA__, **kwds):
1683
"""
1684
This is used internally in the code for converting R
1685
expressions to Sage objects.
1686
1687
EXAMPLES::
1688
1689
sage: a = r([1,2,3])
1690
sage: d = a._r_structure('data', a=1, b=2)
1691
sage: list(sorted(d.items()))
1692
[('DATA', 'data'), ('a', 1), ('b', 2)]
1693
sage: a._r_structure([1,2,3,4], _Dim=(2,2))
1694
[1 3]
1695
[2 4]
1696
1697
"""
1698
if '_Dim' in kwds: #we have a matrix
1699
# TODO what about more than 2 dimensions?
1700
# additional checks!!
1701
try:
1702
from sage.matrix.constructor import matrix
1703
d = kwds.get('_Dim')
1704
# TODO: higher dimensions? happens often in statistics
1705
if len(d) != 2:
1706
raise TypeError
1707
#since R does it the other way round, we assign
1708
#transposed and then transpose the matrix :)
1709
m = matrix(d[1], d[0], [i for i in __DATA__])
1710
return m.transpose()
1711
except TypeError:
1712
pass
1713
d = dict(DATA=__DATA__)
1714
d.update(kwds)
1715
return d
1716
1717
def _sage_(self):
1718
r"""
1719
Returns Sage representation of the R object.
1720
1721
R objects are basic C structures, of different kind, that can
1722
be stacked together. This is similar to Python lists with
1723
variable objects, including lists of lists. If R lists have
1724
names, they are translated to a Python dictionary, with anonymous
1725
list entries called ``#{number}``.
1726
1727
OUTPUT: object -- Python object
1728
1729
EXAMPLES::
1730
1731
sage: rs = r.summary(r.c(1,4,3,4,3,2,5,1))
1732
sage: d = rs._sage_()
1733
sage: list(sorted(d.items()))
1734
[('DATA', [1, 1.75, 3, 2.875, 4, 5]),
1735
('_Names', ['Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.']),
1736
('_r_class', ['summaryDefault', 'table'])]
1737
"""
1738
self._check_valid()
1739
P = self.parent()
1740
1741
# This is the core of the trick: using dput
1742
# dput prints out the internal structure of R's data elements
1743
# options via .deparseOpts(control=...)
1744
# TODO: dput also works with a file, if things get huge!
1745
# [[However, using a file for output often isn't necessary
1746
# since pipe buffering works pretty well for output.
1747
# That said, benchmark this. -- William Stein]]
1748
exp = P.eval('dput(%s)'%self.name())
1749
1750
# Preprocess expression
1751
# example what this could be:
1752
# structure(list(statistic = structure(0.233549683248457, .Names = "t"),
1753
# parameter = structure(5.58461538461538, .Names = "df"), p.value = 0.823657802106985,
1754
# conf.int = structure(c(-2.41722062247400, 2.91722062247400
1755
# ), conf.level = 0.95), estimate = structure(c(2.75, 2.5), .Names = c("mean of x",
1756
# "mean of y")), null.value = structure(0, .Names = "difference in means"),
1757
# alternative = "two.sided", method = "Welch Two Sample t-test",
1758
# data.name = "c(1, 2, 3, 5) and c(1, 2, 3, 4)"), .Names = c("statistic",
1759
# "parameter", "p.value", "conf.int", "estimate", "null.value",
1760
# "alternative", "method", "data.name"), class = "htest")
1761
1762
# R's structure (from help):
1763
# structure(.Data, ...)
1764
# .Data: an object which will have various attributes attached to it.
1765
# ...: attributes, specified in 'tag=value' form, which will be
1766
# attached to data.
1767
#For historical reasons (these names are used when deparsing),
1768
# attributes '".Dim"', '".Dimnames"', '".Names"', '".Tsp"' and
1769
# '".Label"' are renamed to '"dim"', '"dimnames"', '"names"',
1770
# '"tsp"' and '"levels"'.
1771
1772
1773
1774
# we want this in a single line
1775
exp.replace('\n','')
1776
exp = "".join(exp.split("\n"))
1777
1778
# python compatible parameters
1779
exp = rel_re_param.sub(self._subs_dots, exp)
1780
1781
# Rename class since it is a Python keyword
1782
exp = re.sub(' class = ', ' _r_class = ',exp)
1783
1784
# Change 'structure' to '_r_structure'
1785
# TODO: check that we are outside of quotes ""
1786
exp = re.sub(' structure\(', ' _r_structure(', exp)
1787
exp = re.sub('^structure\(', '_r_structure(', exp) #special case
1788
1789
# Change 'list' to '_r_list'
1790
exp = re.sub(' list\(', ' _r_list(', exp)
1791
exp = re.sub('\(list\(', '(_r_list(', exp)
1792
1793
# Change 'a:b' to 'xrange(a,b+1)'
1794
exp = rel_re_xrange.sub(self._subs_xrange, exp)
1795
1796
# Change 'dL' to 'Integer(d)'
1797
exp = rel_re_integer.sub(self._subs_integer, exp)
1798
1799
# Wrap the right hand side of terms = ... in quotes since it
1800
# has a ~ in it.
1801
exp = rel_re_terms.sub(r'terms = "\1",', exp)
1802
1803
1804
# Change call = ..., to call = "...",
1805
exp = rel_re_call.sub(r'call = "\1",', exp)
1806
1807
# seems to work for
1808
# rr = r.summary(r.princomp(r.matrix(r.c(1,2,3,4,3,4,1,2,2),4)))
1809
# rr._sage_()
1810
# but the call expression get's evaluated. why?!? TODO
1811
1812
1813
# translation:
1814
# c is an ordered list
1815
# list is a dictionary (where _Names give the entries names.
1816
# map entries in names to (value, name) in each entry?
1817
# structure is .. see above .. strucuture(DATA,**kw)
1818
# TODO: thinking of just replacing c( with ( to get a long tuple?
1819
1820
1821
exp = self._convert_nested_r_list(exp)
1822
1823
# Set up the globals
1824
globs = {'NA':None, 'NULL':None, 'FALSE':False, 'TRUE':True,
1825
'_r_list':self._r_list, '_r_structure':self._r_structure,
1826
'Integer':sage.rings.integer.Integer,
1827
'character':str}
1828
1829
return eval(exp, globs, globs)
1830
1831
1832
def _latex_(self):
1833
r"""
1834
Return LaTeX representation of this R object.
1835
1836
This calls the ``latex`` command in R.
1837
1838
OUTPUT: a latex expression (basically a string)
1839
1840
EXAMPLES::
1841
1842
sage: latex(r(2)) # optional - Hmisc R package
1843
2
1844
"""
1845
from sage.misc.latex import LatexExpr
1846
self._check_valid()
1847
P = self.parent()
1848
# latex is in Hmisc, this is currently not part of Sage's R!!!
1849
try:
1850
P.library('Hmisc')
1851
except ImportError:
1852
raise RuntimeError, "The R package 'Hmisc' is required for R to LaTeX conversion, but it is not available."
1853
return LatexExpr(P.eval('latex(%s, file="");'%self.name()))
1854
1855
1856
1857
class RFunctionElement(FunctionElement):
1858
def __reduce__(self):
1859
"""
1860
EXAMPLES::
1861
1862
sage: a = r([1,2,3])
1863
sage: a.mean
1864
mean
1865
sage: dumps(a.mean)
1866
Traceback (most recent call last):
1867
...
1868
NotImplementedError: pickling of R element methods is not yet supported
1869
"""
1870
raise NotImplementedError, "pickling of R element methods is not yet supported"
1871
1872
def _sage_doc_(self):
1873
"""
1874
Returns the help for self as a string.
1875
1876
EXAMPLES::
1877
1878
sage: a = r([1,2,3])
1879
sage: length = a.length
1880
sage: print length._sage_doc_()
1881
length package:base R Documentation
1882
...
1883
<BLANKLINE>
1884
"""
1885
M = self._obj.parent()
1886
return M.help(self._name)
1887
1888
def _sage_src_(self):
1889
"""
1890
Returns the source code of self.
1891
1892
EXAMPLES::
1893
1894
sage: a = r([1,2,3])
1895
sage: length = a.length
1896
sage: print length._sage_src_()
1897
function (x) .Primitive("length")
1898
"""
1899
M = self._obj.parent()
1900
return M.source(self._name)
1901
1902
def __call__(self, *args, **kwds):
1903
"""
1904
EXAMPLES::
1905
1906
sage: a = r([1,2,3])
1907
sage: length = a.length
1908
sage: length()
1909
[1] 3
1910
"""
1911
return self._obj.parent().function_call(self._name, args=[self._obj] + list(args), kwds=kwds)
1912
1913
1914
class RFunction(ExpectFunction):
1915
def __init__(self, parent, name, r_name=None):
1916
"""
1917
A Function in the R interface.
1918
1919
INPUT:
1920
1921
- parent -- the R interface
1922
- name -- the name of the function for Python
1923
- r_name -- the name of the function in R itself (which can have dots in it)
1924
1925
EXAMPLES::
1926
1927
sage: length = r.length
1928
sage: type(length)
1929
<class 'sage.interfaces.r.RFunction'>
1930
sage: loads(dumps(length))
1931
length
1932
"""
1933
self._parent = parent
1934
if r_name:
1935
self._name = name
1936
else:
1937
self._name = parent._sage_to_r_name(name)
1938
1939
def __cmp__(self, other):
1940
"""
1941
EXAMPLES::
1942
1943
sage: r.mean == loads(dumps(r.mean))
1944
True
1945
sage: r.mean == r.lr
1946
False
1947
"""
1948
if not isinstance(other, RFunction):
1949
return cmp(type(self), type(other))
1950
return cmp(self._name, other._name)
1951
1952
def _sage_doc_(self):
1953
"""
1954
Returns the help for self.
1955
1956
EXAMPLES::
1957
1958
sage: length = r.length
1959
sage: print length._sage_doc_()
1960
length package:base R Documentation
1961
...
1962
<BLANKLINE>
1963
"""
1964
M = self._parent
1965
return M.help(self._name)
1966
1967
def _sage_src_(self):
1968
"""
1969
Returns the source of self.
1970
1971
EXAMPLES::
1972
1973
sage: length = r.length
1974
sage: print length._sage_src_()
1975
function (x) .Primitive("length")
1976
1977
"""
1978
M = self._parent
1979
return M.source(self._name)
1980
1981
def __call__(self, *args, **kwds):
1982
"""
1983
EXAMPLES::
1984
1985
sage: length = r.length
1986
sage: length([1,2,3])
1987
[1] 3
1988
"""
1989
return self._parent.function_call(self._name, args=list(args), kwds=kwds)
1990
1991
def is_RElement(x):
1992
"""
1993
Return True if x is an element in an R interface.
1994
1995
INPUT:
1996
1997
- x -- object
1998
1999
OUTPUT: bool
2000
2001
EXAMPLES::
2002
2003
sage: from sage.interfaces.r import is_RElement
2004
sage: is_RElement(2)
2005
False
2006
sage: is_RElement(r(2))
2007
True
2008
"""
2009
return isinstance(x, RElement)
2010
2011
# An instance of R
2012
r = R()
2013
2014
def reduce_load_R():
2015
"""
2016
Used for reconstructing a copy of the R interpreter from a pickle.
2017
2018
EXAMPLES::
2019
2020
sage: from sage.interfaces.r import reduce_load_R
2021
sage: reduce_load_R()
2022
R Interpreter
2023
"""
2024
return r
2025
2026
import os
2027
def r_console():
2028
"""
2029
Spawn a new R command-line session.
2030
2031
EXAMPLES::
2032
2033
sage: r.console() # not tested
2034
R version 2.6.1 (2007-11-26)
2035
Copyright (C) 2007 The R Foundation for Statistical Computing
2036
ISBN 3-900051-07-0
2037
...
2038
"""
2039
# This will only spawn local processes
2040
os.system('R --vanilla')
2041
2042
def r_version():
2043
"""
2044
Return the R version.
2045
2046
EXAMPLES::
2047
2048
sage: r_version() # not tested
2049
((3, 0, 1), 'R version 3.0.1 (2013-05-16)')
2050
sage: rint, rstr = r_version()
2051
sage: rint[0] >= 3
2052
True
2053
sage: rstr.startswith('R version')
2054
True
2055
"""
2056
return r.version()
2057
2058
class HelpExpression(str):
2059
"""
2060
Used to improve printing of output of r.help.
2061
"""
2062
def __repr__(self):
2063
"""
2064
Return string representation of self.
2065
2066
OUTPUT: string
2067
2068
EXAMPLES::
2069
2070
sage: a = sage.interfaces.r.HelpExpression("This\nis\nR!")
2071
sage: type(a)
2072
<class 'sage.interfaces.r.HelpExpression'>
2073
sage: a
2074
This
2075
is
2076
R!
2077
"""
2078
return self.__str__()
2079
2080
2081