Show More
@@ -0,0 +1,7 b'' | |||||
|
1 | .. _extensions_rmagic: | |||
|
2 | ||||
|
3 | =========== | |||
|
4 | Rmagic | |||
|
5 | =========== | |||
|
6 | ||||
|
7 | .. automodule:: IPython.extensions.rmagic |
@@ -1,12 +1,35 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | R related magics. |
|
3 | ====== | |
|
4 | Rmagic | |||
|
5 | ====== | |||
4 |
|
6 | |||
5 | Author: |
|
7 | Magic command interface for interactive work with R via rpy2 | |
6 | * Jonathan Taylor |
|
8 | ||
|
9 | Usage | |||
|
10 | ===== | |||
|
11 | ||||
|
12 | ``%R`` | |||
|
13 | ||||
|
14 | {R_DOC} | |||
|
15 | ||||
|
16 | ``%Rpush`` | |||
|
17 | ||||
|
18 | {RPUSH_DOC} | |||
|
19 | ||||
|
20 | ``%Rpull`` | |||
|
21 | ||||
|
22 | {RPULL_DOC} | |||
7 |
|
23 | |||
8 | """ |
|
24 | """ | |
9 |
|
25 | |||
|
26 | #----------------------------------------------------------------------------- | |||
|
27 | # Copyright (C) 2012 The IPython Development Team | |||
|
28 | # | |||
|
29 | # Distributed under the terms of the BSD License. The full license is in | |||
|
30 | # the file COPYING, distributed as part of this software. | |||
|
31 | #----------------------------------------------------------------------------- | |||
|
32 | ||||
10 | import sys |
|
33 | import sys | |
11 | import tempfile |
|
34 | import tempfile | |
12 | from glob import glob |
|
35 | from glob import glob | |
@@ -46,11 +69,16 b' class RMagics(Magics):' | |||||
46 | self.cache_display_data = cache_display_data |
|
69 | self.cache_display_data = cache_display_data | |
47 |
|
70 | |||
48 | self.r = ro.R() |
|
71 | self.r = ro.R() | |
49 |
self. |
|
72 | self.Rstdout_cache = [] | |
50 | self.Rconverter = Rconverter |
|
73 | self.Rconverter = Rconverter | |
51 | self.pyconverter = pyconverter |
|
74 | self.pyconverter = pyconverter | |
52 |
|
75 | |||
53 | def eval(self, line): |
|
76 | def eval(self, line): | |
|
77 | ''' | |||
|
78 | Parse and evaluate a line with rpy2. | |||
|
79 | Returns the output to R's stdout() connection | |||
|
80 | and the value of eval(parse(line)). | |||
|
81 | ''' | |||
54 | old_writeconsole = ri.get_writeconsole() |
|
82 | old_writeconsole = ri.get_writeconsole() | |
55 | ri.set_writeconsole(self.write_console) |
|
83 | ri.set_writeconsole(self.write_console) | |
56 | try: |
|
84 | try: | |
@@ -64,13 +92,16 b' class RMagics(Magics):' | |||||
64 |
|
92 | |||
65 | def write_console(self, output): |
|
93 | def write_console(self, output): | |
66 | ''' |
|
94 | ''' | |
67 | A hook to capture R's stdout. |
|
95 | A hook to capture R's stdout in a cache. | |
68 | ''' |
|
96 | ''' | |
69 |
self. |
|
97 | self.Rstdout_cache.append(output) | |
70 |
|
98 | |||
71 | def flush(self): |
|
99 | def flush(self): | |
72 | value = ''.join([str_to_unicode(s, 'utf-8') for s in self.output]) |
|
100 | ''' | |
73 | self.output = [] |
|
101 | Flush R's stdout cache to a string, returning the string. | |
|
102 | ''' | |||
|
103 | value = ''.join([str_to_unicode(s, 'utf-8') for s in self.Rstdout_cache]) | |||
|
104 | self.Rstdout_cache = [] | |||
74 | return value |
|
105 | return value | |
75 |
|
106 | |||
76 | @skip_doctest |
|
107 | @skip_doctest | |
@@ -80,19 +111,19 b' class RMagics(Magics):' | |||||
80 | A line-level magic for R that pushes |
|
111 | A line-level magic for R that pushes | |
81 | variables from python to rpy2. The line should be made up |
|
112 | variables from python to rpy2. The line should be made up | |
82 | of whitespace separated variable names in the IPython |
|
113 | of whitespace separated variable names in the IPython | |
83 |
namespace |
|
114 | namespace:: | |
84 |
|
115 | |||
85 | In [7]: import numpy as np |
|
116 | In [7]: import numpy as np | |
86 |
|
117 | |||
87 | In [8]: X = np.array([4.5,6.3,7.9]) |
|
118 | In [8]: X = np.array([4.5,6.3,7.9]) | |
88 |
|
119 | |||
89 | In [9]: X.mean() |
|
120 | In [9]: X.mean() | |
90 | Out[9]: 6.2333333333333343 |
|
121 | Out[9]: 6.2333333333333343 | |
91 |
|
122 | |||
92 | In [10]: %Rpush X |
|
123 | In [10]: %Rpush X | |
93 |
|
124 | |||
94 | In [11]: %R mean(X) |
|
125 | In [11]: %R mean(X) | |
95 | Out[11]: array([ 6.23333333]) |
|
126 | Out[11]: array([ 6.23333333]) | |
96 |
|
127 | |||
97 | ''' |
|
128 | ''' | |
98 |
|
129 | |||
@@ -122,6 +153,7 b' class RMagics(Magics):' | |||||
122 | array(['a', '3', '4'], |
|
153 | array(['a', '3', '4'], | |
123 | dtype='|S1') |
|
154 | dtype='|S1') | |
124 |
|
155 | |||
|
156 | ||||
125 | Notes |
|
157 | Notes | |
126 | ----- |
|
158 | ----- | |
127 |
|
159 | |||
@@ -233,12 +265,17 b' class RMagics(Magics):' | |||||
233 | In [17]: W |
|
265 | In [17]: W | |
234 | Out[17]: array([ 5., 20., 25., 50.]) |
|
266 | Out[17]: array([ 5., 20., 25., 50.]) | |
235 |
|
267 | |||
236 | If the cell evaluates as False, the resulting value is returned |
|
268 | The return value is determined by these rules: | |
237 | unless the final line prints something to the console. |
|
269 | ||
238 | If the final line results in a NULL value when evaluated |
|
270 | * If the cell is not None, the magic returns None. | |
|
271 | ||||
|
272 | * If the cell evaluates as False, the resulting value is returned | |||
|
273 | unless the final line prints something to the console, in | |||
|
274 | which case None is returned. | |||
|
275 | ||||
|
276 | * If the final line results in a NULL value when evaluated | |||
239 | by rpy2, then None is returned. |
|
277 | by rpy2, then None is returned. | |
240 |
|
278 | |||
241 | If the cell is not None, the magic returns None. |
|
|||
242 |
|
279 | |||
243 | ''' |
|
280 | ''' | |
244 |
|
281 | |||
@@ -331,6 +368,13 b' class RMagics(Magics):' | |||||
331 | if result != ri.NULL: |
|
368 | if result != ri.NULL: | |
332 | return self.Rconverter(result) |
|
369 | return self.Rconverter(result) | |
333 |
|
370 | |||
|
371 | __doc__ = __doc__.format( | |||
|
372 | R_DOC = ' '*8 + RMagics.R.__doc__, | |||
|
373 | RPUSH_DOC = ' '*8 + RMagics.Rpush.__doc__, | |||
|
374 | RPULL_DOC = ' '*8 + RMagics.Rpull.__doc__ | |||
|
375 | ) | |||
|
376 | ||||
|
377 | ||||
334 | _loaded = False |
|
378 | _loaded = False | |
335 | def load_ipython_extension(ip): |
|
379 | def load_ipython_extension(ip): | |
336 | """Load the extension in IPython.""" |
|
380 | """Load the extension in IPython.""" |
General Comments 0
You need to be logged in to leave comments.
Login now