Let's say I have a vector x1 as a numpy array in the python environment. I want to transfert that vector to the R environment, do some computations using R specific functions and then get another vector x2 and then want to transfer x2 back to python as a numpy array. The goal is to have the following output:
[1 2 3]
[2 2 3]
Here is a code (which works until the line 9 but not after) using rpy2 (or any other package)?
from rpy2.robjects import FloatVector
from rpy2.robjects.packages import importr
stats = importr('stats')
base = importr('base')
#lmtest = importr("lmtest")
import rpy2.robjects as robjects
import numpy as np
x1 = np.array([1,2,3])
print(x1)
robjects.r('''
x2 = x1
x2[1] = x2[1] + 1 ## some R specific function
''')
print(x2)
EDIT
I tried to implement the answer of @lgautier as follows:
import rpy2.robjects as robjects
import numpy as np
x1 = np.array([1,2,3])
print(x1)
robjects.r('''
x2 = x1
x2[1] = x2[1] + 1 ## some R specific function
''')
x3 = robjects.r['x2']
but I got the following error message
R[write to console]: Error in (function (expr, envir = parent.frame(), enclos = if (is.list(envir) || :
object 'x1' not found
What am I doing wrong?