## 6 Miscellaneous

The `rpy` module includes some utility functions:

`as_list(obj)`

  If `obj` is a list or an object which supports the list protocol, it returns `obj`. Otherwise, it returns the one element list `[obj]`.

  This function is useful when testing whether a `Robj` has some given attribute. For example:

      >>> 'class' in as_list(r.attributes(robj))

  The reason for not doing `'class' in r.attributes(robj)` is that `r.attributes` can return either `None` (when `robj` has no attributes), a string (when `robj` has only one attribute) or a list of strings (when it has several attributes). Function `as_list` unify these three cases to allow the `in` test.

`r(s)`

  Parameter `s` is a string containing arbitrary R code. Function `r` evaluates the string `s` in the R interpreter and returns its result.

  This function is useful when working with R constructions which have no parallel Python syntax, such as linear models, for example.

      >>> set_default_mode(NO_CONVERSION)
      >>> d = r.data_frame(x=[1,2,3], y=[4,5,6])
      >>>
      >>> model = r("y ~ x")
      >>> fitted_model = r.lm(model, data = d)

  Complete fragments of R code can also be evaluated (note that the value returned by the function `r` is the value of the last expression):

      >>> r("""
      ... print(5)
      ... x <- "foo"
      ... print(x)
      ... """)
      [1] 5
      [1] "foo"
      'foo'

  This function is useful, also, when a changing R object is required. Since the expression `r.foo` is cached in a Python dictionary, later changes in the object pointed by `r.foo` are not seen. In that case, the proper expression to use is `r('foo')`, which evaluates `foo` and returns its result every time it is called.

`start_r_eventloop()`
`stop_r_eventloop()`

  (new in 0.3) [Not available on Windows] These functions start and stop the R event loop. When RPy is imported, the `start_r_eventloop` function is executed. Normally, in interactive use, you needn’t stop it.

  The R event loop keeps running in a daemonic thread. In case you need finer control over that loop, you can use the `r_events` function.

`r_events([usec])`

  (new in 0.3) [parameter usec not available on Windows] This function processes a pending R event or blocks with a usec microseconds timeout. The default value for usec is 10000 microseconds.

  If, for some reason, you don’t want to use the threaded event loop and you want to manually use `r_events`, you can do some loop like the following:

      >>> r.plot([1])
      >>> while 'X11' in as_list(r('.Devices')):
      ...     r_events()

  The `while` loop will run until the graphics window is closed.