ralhei / pyRserve

A python client for Rserve (network layer to remote R server)
Other
47 stars 13 forks source link

data.frame object unrecoginized with pyRserve #24

Closed noirot closed 1 year ago

noirot commented 3 years ago

I'm new in pyRserve and Rserve, and I have a problem with data.frame representation. I do have a function that read a data.frame object and print 1 variable content, given by :

### test.R
r_test <- function(dataset, varname) {
    print (dataset[, varname])
}

and a main function in python, that uses the previous function on the iris data.frame :

### main.py
import pyRserve
conn = pyRserve.connect()
conn.r.data('iris')

conn.r.source("tmp.R")
conn.r.r_test(conn.r.iris, 'Species')

This return an error :

pyRserve.rexceptions.REvalError: Error in dataset[, varname] : number of dimensions incorrect
ralhei commented 3 years ago

Are you sure you are sourcing the correct R file? The one at the top is called 'test.R', but you are sourcing 'tmp.R'. The other thing you can try is to replace the last line with: conn.r.r_test(conn.ref.iris, 'Species') By using conn.ref instead of conn.r the value of iris will be directly taken from the R namespace without transferring it first to Python and then back to R.

noirot commented 3 years ago

Perfect it work's well ! Thanks you.

noirot commented 3 years ago

Thanks a lot for the conn.ref, it works on this example ! Actually, this example was not what I was doing, it was more to understand. My problem occurs with R functions returning a data.frame. To reproduce, I have the test.R file, with the function r_test returning a data.frame, as test.R

r_test <- function() { df <- data.frame (first_column = c("value_1 ", "value_2"), second_column = c("value_1", "value_2") ) return (df) }

an the main python file loading the file in the R session and assigning the result in a variable named test as following :

import pyRserve conn = pyRserve.connect() conn.ref.source("test.R") conn.ref.assign('test', conn.ref.r_test())

when accessing from R the variable test is no longer a data.frame but a list, thus : conn.eval('test[, "second_column"]')

returns the error : pyRserve.rexceptions.REvalError: Error in test[, "second_column"] : number of dimensions incorrect

The conn.ref solution doesn't seem to work, any idea ? thanks for your help

ralhei commented 3 years ago

Hmm, I haven't worked in that area for a while ... seems like the 'assign()' method still transfers the result of r_test() to Python first, and then retransfers it to R. During that transfer the proper data type seems to get lost. This could indeed be a bug in pyRserve, however currently and in the near future I will not have time to track that down, i'm sorry. But feel free to dig into the code and help fixing it, if you want to.

ralhei commented 1 year ago

Closed.