File: interactive.rst

package info (click to toggle)
rpy2 3.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,412 kB
  • sloc: python: 18,448; ansic: 492; makefile: 197; sh: 166
file content (255 lines) | stat: -rw-r--r-- 7,529 bytes parent folder | download
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
****************
Interactive work
****************

Overview
========

.. note::

   This is an experimental package, and some of the ideas experimented
   here have already made it to :mod:`rpy2.robjects`.

   For interactive work the "R magic" extension to `ipython` is
   the preferred way / most tested way for interactive work.

.. module:: rpy2.ipython

IPython magic integration (was rmagic)
======================================

.. automodule:: rpy2.ipython.rmagic
   :members:

.. module:: rpy2.interactive.process_revents
   :synopsis: Processing R events for interactivity

.. _interactive-reventloop:

R event loop
============

.. codeauthor:: Thomas Kluyver, Laurent Gautier

In order to perform operations like refreshing interactive graphical
devices, R need to process the events triggering the refresh.

>>> from rpy2.interactive import process_revents
>>> process_revents.start()

>>> from rpy2.robjects.packages import importr
>>> from rpy2.robjects.vectors import IntVector
>>> graphics = importr("graphics")
>>> graphics.barplot(IntVector((1,3,2,5,4)), ylab="Value")

Now the R graphical device is updated when resized.
Should one wish to stop processing the events:

>>> process_revents.stop()

The processing can be resumed, stopped again, and this repeated *ad libitum*.

The frequency with which the processing of R events is performed can be roughly
controlled. The thread is put to sleep for an arbitray duration between
each processing of R events.

>>> process_revents.EventProcessor.interval
0.2

This value can be changed and set to an other value if more or less frequent
processing is wished. This can be done while the threaded processing is
active and will be taken into account at the next sleep cycle.

>>> process_revents.EventProcessor.interval = 1.0

.. autofunction:: process_revents()



.. module:: rpy2.interactive

Utilities for interactive work
==============================

.. note:: 

   This module contains a number of experimental features, some of
   them no longer necessary since the "R magic" extension for ipython. 
   They are becoming deprecated, and will removed from the code base
   in future versions.

R is very often used as an interactive toplevel, or read-eval-print loop (REPL).
This is convenient when analyzing data: type some code, get the result,
type some new code and further analysis based on the results.

Python can also be used in a similar fashion, but limitations of the
default Python console have lead to the creation of alternative consoles
and interactive development editors
(idle, ipython, bpython, emacs mode, komodo, ...).
Features such as code highlighting, autocompletion, and convenient display of
help strings or function signatures have made those valuable tools.


The package :mod:`rpy2.interactive` aims at interactive users, but can be used
in non-interactive code as well. It is trading flexibility
or performances for ease-of-use.
 
>>> import rpy2.interactive as r
>>> import rpy2.interactive.packages # this can take few seconds
>>> v = r.IntVector((1,2,3))
>>> r.packages.importr('datasets')
rpy2.robjecs.packages.Package as a <module 'datasets' (built-in)>
>>> data = rpy2.interactive.packages.data
>>> rpackages = r.packages.packages
>>> # list of datasets
>>> data(rpackages.datasets).names()
# list here
>>> env = data(rpackages.datasets).fetch('trees')
>>> tuple(env['trees'].names)
('Girth', 'Height', 'Volume')

R vectors
---------

>>> import rpy2.interactive as r
>>> r.IntVector(range(10))
<IntVector - Python:0x935774c / R:0xa22b440>
[       0,        1,        2, ...,        7,        8,        9]
>>> r.IntVector(range(100))
<IntVector - Python:0xa1c2dcc / R:0x9ac5540>
[       0,        1,        2, ...,       97,       98,       99]

In R, there are no scalars.

>>> r.packages.base.pi
<FloatVector - Python:0xa1d7a0c / R:0x9de02a8>
[3.141593]

To know more, please check Section :ref:`introduction-vectors`.


R packages
----------

R has a rich selection of packages, known in other computer
languages and systems as libraries.

R Packages can be:

- available in R repositories (public or private)

- installed 

- attached (loaded)


Loading installed packages
^^^^^^^^^^^^^^^^^^^^^^^^^^

When starting R, the *base* package as well as by default the packages
*grDevices*, *graphics*, *methods*, *stats*, and *utils* are loaded.

We start with the loading of R packages since this is a very common
operation in R, and since R is typically distributed
with *recommended* packages one can immediately start playing with.

Loading installed R packages can be done through the function :func:`importr`. 

>>> import rpy2.interactive as r
>>> import rpy2.interactive.packages # this can take few seconds
>>> r.packages.importr("cluster")
rpy2.robjecs.packages.Package as a <module 'cluster' (built-in)>

The function returns a package object, and also adds a reference to it
in :attr:`r.packages.packages`

>>> rlib = r.packages.packages
>>> rlib.cluster
rpy2.robjecs.packages.Package as a <module 'cluster' (built-in)>

All objects in the R package *cluster* can subsequently be accessed
through that namespace object. For example, for the function barplot:

>>> rlib.cluster.silhouette
<SignatureTranslatedFunction - Python:0x24f9418 / R:0x2f5b008>


Similarly, other packages such as *nlme*, and *datasets*
can be loaded.

>>> r.packages.importr("nlme")
rpy2.robjecs.packages.Package as a <module 'stats' (built-in)>
>>> r.packages.importr("datasets")
rpy2.robjecs.packages.Package as a <module 'datasets' (built-in)>

We can then demonstrate how to access objects in R packages through
a graphical example:

.. code-block:: python

   r.packages.graphics.coplot(r.Formula('Time ~ conc | Wt'),
                              r.packages.datasets.Theoph) 



Available packages
^^^^^^^^^^^^^^^^^^

R has a function to list the available packages.

>>> import rpy2.interactive as r
>>> import rpy2.interactive.packages # this can take few seconds
>>> r.packages.importr("utils")
>>> rlib = r.packages.packages
>>> m = rlib.utils.available_packages()

The object returned is a :class:`rpy2.robjects.vectors.Matrix`, with one
package per row (there are many packages in the default CRAN repository).

>>> tuple(m.dim)
(2692, 13)

>>> tuple(m.colnames)
('Package', 'Version', 'Priority', 'Depends', 'Imports', 'LinkingTo', 'Suggests', 'Enhances', 'OS_type', 'License', 'Archs', 'File', 'Repository')


.. note::

   Specific repositories can be specified.

   For example with bioconductor.

   .. code-block:: python

      import rpy2.rinteractive as r

      bioc_rooturl = "http://www.bioconductor.org/packages"
      bioc_version = "2.7"
      bioc_sections = ("bioc", "data/annotation", "data/experiment", "extra")
   
      repos = r.vectors.StrVector(["/".join((bioc_rooturl, bioc_version, x)) for x in bioc_sections])   

      m_bioc = rlib.utils.available_packages(contriburl = r.packages.utils.contrib_url(repos))   



Installing packages
^^^^^^^^^^^^^^^^^^^

.. note::

   To install a package for repositories, we have to load the package `utils`.
   See Section load-packages for details about loading packages


>>> import rpy2.interactive as r
>>> import rpy2.interactive.packages # this can take few seconds
>>> rlib = r.packages.packages
>>> r.packages.importr("utils")
>>> package_name = "lme4"
>>> rlib.utils.install_packages(package_name)

Once a package is installed it is available for future use without having
the need to install it again (unless a different version of R is used).