@@ -156,11 +156,13 @@ p = (gp.ggplot(dataf) +
156
156
image_png(p)
157
157
```
158
158
159
+ # R "magics"
160
+
159
161
So far we have shown that using ` ggplot2 ` can be done from Python as if it
160
162
was just an other Python library for visualization, but R can also be used
161
163
in cells.
162
164
163
- First the so-called "R magic" extension should be loaded.
165
+ First the so-called "R magic" extensions should be loaded.
164
166
165
167
``` python
166
168
% load_ext rpy2.ipython
@@ -205,3 +207,105 @@ p <- ggplot(dataf) +
205
207
theme_light(base_size = 16 )
206
208
print (p)
207
209
```
210
+
211
+ ## Options.
212
+
213
+ The R "magics" use a number of configurable options that control
214
+ aspects of the interaction between Python and R, or between ipython / jupyter
215
+ and R. Some of the options can specified through arguments to "magics",
216
+ but it is also possible to configure the default value for some of them
217
+ when an argument to a "magic" does not specify otherwise. The easiest
218
+ way to list these default-configurable options can be achieved
219
+ with a "magic":
220
+
221
+ ``` python
222
+ % Roptions show
223
+ ```
224
+
225
+ Changing these defaults requires to modify components of the attribute
226
+ :attr:` rpy2.ipython.rmagic.RMagic.options ` . Here is an example showing
227
+ how to changing the default of R figures rendered in the notebook to "svg".
228
+
229
+ ``` python
230
+ import IPython
231
+ ipy = IPython.get_ipython()
232
+ ipy.magics_manager.registry[' RMagics' ].options.graphics_device_name = ' svg'
233
+ ```
234
+
235
+ Another example is how to change conversion rules, that can be
236
+ an expensive operation depending object sizes and rules, to a no-op:
237
+ no convertion rules to keep all rpy2 objects as proxy wrappers
238
+ to R objects for example, no conversion of Python objects to R:
239
+
240
+ ``` python
241
+ import rpy2.robjects
242
+ import IPython
243
+ ipy = IPython.get_ipython()
244
+
245
+ noop_conv = rpy2.robjects.conversion.Converter(' No-Op' )
246
+ noop_conv.rpy2py.register(rpy2.robjects.RObject,
247
+ rpy2.robjects._rpy2py_robject)
248
+
249
+ ipy.magics_manager.registry[' RMagics' ].options.converter = noop_conv
250
+ ```
251
+
252
+ Modifying this come with risks though, or afterthoughts about changes made.
253
+ While there are some checks in place, the new default options set this way
254
+ may break the R "magics".
255
+ To address this, options can be restore to
256
+ "factory settings". Here again, there is no guarantee that this can work no
257
+ matter what. A driven
258
+ (or unlucky) user may break this mechanism if modifying it in the module.
259
+ Restarting kernel will be needed in that case.
260
+
261
+ ``` python
262
+ % Roptions refresh
263
+ ```
264
+
265
+ ## Graphics devices
266
+
267
+ Graphics devices can be specified precisely as a function in an R namespace.
268
+ For example, "grDevices: png ". It is also possible to specify a generic graphical
269
+ device name that is a sequence of graphics devices to try. The first one in that
270
+ sequence that appears to be functioning is then selected.
271
+
272
+ For example, this is the case when
273
+ the graphics device option is set to "svg". The R package ` svglite ` is
274
+ the first to try in the sequence. If R fails to load that package there
275
+ are other SVG devices to try try as alternatives.
276
+
277
+ To demonstrate this, let's set the graphics devices to "svg":
278
+
279
+ ``` python
280
+ import IPython
281
+ ipy = IPython.get_ipython()
282
+ ipy.magics_manager.registry[' RMagics' ].options.graphics_device_name = ' svg'
283
+ ```
284
+
285
+ The output of the following magic depends on the R
286
+ packages installed on your system. If the R package ` svglite ` is installed
287
+ its device will be used. Otherwise alternatives will be tried.
288
+
289
+ ``` python
290
+ % Roptions show
291
+ ```
292
+
293
+ ### Options for graphics device
294
+
295
+ Graphics devices, selected from a generic names when the case (see above),
296
+ may havee options. These options can be specific to each graphics device.
297
+
298
+ ** note: this is prefixed with an underscore to indicate that the API is
299
+ subject to changes.**
300
+
301
+ Options to display graphics are under ` _options_display ` . For example,
302
+ to change the default width:
303
+
304
+ ``` python
305
+ (
306
+ ipy.magics_manager.registry[' RMagics' ].graphics_device
307
+ ._options_display
308
+ .width
309
+ ) = 10
310
+ ```
311
+
0 commit comments