<commit_before>pyRserve manual
===============

Setting up a connection to Rserve
---------------------------------

First of all startup Rserve if it is not yet running::

  $ R CMD Rserve

If you started it on your local machine (i.e. ``localhost``) without any extra options Rserve should be listening on port 6311 (its default).

From the python interpreter import the pyRserve package and by omitting any arguments to the ``rconnect()`` function setup the connection to your locallly running Rserve::

  $ python
  >>> import pyRserve
  >>> conn = pyRserve.rconnect()

To connect to a different location host and port can be specified explicitely::

  pyRserve.rconnect(host='localhost', port=6311)

The resulting connection handle can tell you where it is connected to::

  >>> conn
  <Handle to Rserve on localhost:6311>

The connection will be closed automatically when conn is deleted, or by explicitely calling the ``close()``-method::

  >>> conn.close()
  >>> conn
  <Closed handle to Rserve on localhost:6311>

Running operations on a closed pyRserve connector results in an exception. However a connection can be reopened by calling the ``connect()`` method. It reuses the previously given values (or defaults) for ``host`` and ``port``::

  >>> conn.connect()
  <Handle to Rserve on localhost:6311>

To check the status of the connection use::

  >>> conn.isClosed
  False



String evaluation in R
-------------------------------

Having established a connection to Rserve you can run the first commands on it. A valid R command can be executed by making a call directly on the connector, providing a string as argument::

  >>> conn('3 + 5')
  8.0

In this example the string ``"3 + 5"`` will be sent to the remote side and evaluated by R. The result is then delivered back into a native Python object, a floating point number in this case. As an R expert you are probably aware of the fact that R uses vectors for all numbers internally by default. But why did we received a single floating point number? The reason is that pyRserve looks at vectors coming from Rserve and converts vectors with only one single item into an atomic value. This behaviour is for convenience reasons only, and might change in the future.

Of course also more complex data types can be sent from R to Python, e.g. lists or real vectors. Here are some examples::

  >>> conn('list(1, 'otto')')
  [1, 'otto']
  >>> conn('c(1, 5, 7)')
  array([ 1.,  2.])

As demonstrated R lists are converted into plain Python lists whereas R vectors are converted into numpy arrays on the Python side 

It is also possible to create functions inside the R interpreter through the connector, or even to execute entire scripts. Basically you can do everything which is possible inside a normal R console::

  # create a function and execute it:
  >>> conn('doubleit <- function(x) { x*2 }')
  >>> conn('doubleit(2)')
  4.0
  # store a script as a string ...
  >>> my_r_script = '''
  squareit <- function(x)
    { x**2 }
  squareit(4)
  '''
  # and execute the mini-script in R:
  >>> conn(my_r_script)
  16.0




The R namespace<commit_msg>fixed quotation bug<commit_after>pyRserve manual
===============

Setting up a connection to Rserve
---------------------------------

First of all startup Rserve if it is not yet running::

  $ R CMD Rserve

If you started it on your local machine (i.e. ``localhost``) without any extra options Rserve should be listening on port 6311 (its default).

From the python interpreter import the pyRserve package and by omitting any arguments to the ``rconnect()`` function setup the connection to your locallly running Rserve::

  $ python
  >>> import pyRserve
  >>> conn = pyRserve.rconnect()

To connect to a different location host and port can be specified explicitely::

  pyRserve.rconnect(host='localhost', port=6311)

The resulting connection handle can tell you where it is connected to::

  >>> conn
  <Handle to Rserve on localhost:6311>

The connection will be closed automatically when conn is deleted, or by explicitely calling the ``close()``-method::

  >>> conn.close()
  >>> conn
  <Closed handle to Rserve on localhost:6311>

Running operations on a closed pyRserve connector results in an exception. However a connection can be reopened by calling the ``connect()`` method. It reuses the previously given values (or defaults) for ``host`` and ``port``::

  >>> conn.connect()
  <Handle to Rserve on localhost:6311>

To check the status of the connection use::

  >>> conn.isClosed
  False



String evaluation in R
-------------------------------

Having established a connection to Rserve you can run the first commands on it. A valid R command can be executed by making a call directly on the connector, providing a string as argument::

  >>> conn('3 + 5')
  8.0

In this example the string ``"3 + 5"`` will be sent to the remote side and evaluated by R. The result is then delivered back into a native Python object, a floating point number in this case. As an R expert you are probably aware of the fact that R uses vectors for all numbers internally by default. But why did we received a single floating point number? The reason is that pyRserve looks at vectors coming from Rserve and converts vectors with only one single item into an atomic value. This behaviour is for convenience reasons only, and might change in the future.

Of course also more complex data types can be sent from R to Python, e.g. lists or real vectors. Here are some examples::

  >>> conn("list(1, 'otto')")
  [1, 'otto']
  >>> conn('c(1, 5, 7)')
  array([ 1.,  2.])

As demonstrated R lists are converted into plain Python lists whereas R vectors are converted into numpy arrays on the Python side 

It is also possible to create functions inside the R interpreter through the connector, or even to execute entire scripts. Basically you can do everything which is possible inside a normal R console::

  # create a function and execute it:
  >>> conn('doubleit <- function(x) { x*2 }')
  >>> conn('doubleit(2)')
  4.0
  # store a script as a string ...
  >>> my_r_script = '''
  squareit <- function(x)
    { x**2 }
  squareit(4)
  '''
  # and execute the mini-script in R:
  >>> conn(my_r_script)
  16.0




The R namespace
{'fraction_non_alphanumeric': 0.1031390134529148, 'fraction_numerical': 0.013452914798206279, 'mean_word_length': 3.595290654893304, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7098700', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1600, 'n_words': 895}