If I understand it correctly, you:
- have a function (classifier) written in R that requires a relatively large body of data to work (k nearest neighbors ?)
- are loading that body of data using Python
- would like to load the parameters /once/ and after that make as many calls to the classifier as required
- plan passing the body of data as a parameter for the classifier
If following 4., copying is not always necessary but currently only if the data is numerical, or boolean, and the memory region is allocated by R.
However, I think that a simpler alternative for that situation is to have the body of data passed to R once for all (and copied if necessary) and use that converted object.
from rpy2.robjects.packages import importr
e1071 = importr('e1071')
from rpy2.robjects.conversion import py2ri
# your model's data are in 'm_data'
# here conversion is happening
r_m_data = py2ri(m_data)
for test_data in many_test_data:
# r_m_data is already a pointer to an R data structure
# (it was converted above - no further copying is made)
res = e1071.knn(r_m_data, test_data)
This will correspond to what you describe as:
How can I load the model file just once, and the in-memory model object can be re-used when the same r function being called though rpy2?