pyServe not able to call basic R functions

9 views
Skip to first unread message

Alain Castro

unread,
Jul 2, 2020, 2:56:54 AM7/2/20
to pyRserve

I'm calling Rserve from python and it runs for basic operations, but not if I call basic functions as min

import pyRserve

conn = pyRserve.connect()

cars = [1, 2, 3]
conn.r.x = cars
print(conn.eval('x'))
print(conn.eval('min(x)'))

The result is:

[1, 2, 3]
Traceback (most recent call last):
  File "test3.py", line 9, in <module>
    print(conn.eval('min(x)'))
  File "C:\Users\acastro\.windows-build-tools\python27\lib\site-packages\pyRserve\rconn.py", line 78, in decoCheckIfClosed
    return func(self, *args, **kw)
  File "C:\Users\acastro\.windows-build-tools\python27\lib\site-packages\pyRserve\rconn.py", line 191, in eval
    raise REvalError(errorMsg)
pyRserve.rexceptions.REvalError: Error in min(x) : invalid 'type' (list) of argument

Do you know where is the problem? Thanks

Ralph Heinkel

unread,
Jun 20, 2021, 2:04:36 PMJun 20
to pyRserve
The problem with your code is that `conn.r.x = [1, 2, 3]` assigns a list of three integers in R. 
This is the same as doing `x <- list(1, 2, 3)` in R. However the `min`-function doesn't work properly on lists, it expects an array.

So what you need to do is:
```
import numpy
conn.r.x = numpy.array([1, 2, 3])
conn.r('min(x)')
```
Reply all
Reply to author
Forward
0 new messages