I am working on a package with a dependency on a particular program installed via R
. I would rather simplify the installation process and not make the users create a backend for R
with all of the packages. This may be completely unavoidable but I'm wondering if there is any possible way to supply the R objects in a serialized form via pickle
or any serialization module in Python?
I tried pickle
first and then tried dill
but had no luck.
>>> import pickle
>>> from rpy2.robjects.packages import importr
>>> r_package = importr("dynamicTreeCut")
>>> r_package
rpy2.robjects.packages.Package as a <module 'dynamicTreeCut'>
>>> with open("./dynamicTreeCut.rpy2.pkl", "wb") as f:
... pickle.dump(r_package, f)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: can't pickle InstalledSTPackage objects
You need R to use rpy2, and if you wish to use R packages they must be installed with R. How to package R and packages is a question that is relatively independent from rpy2, and detailing the options probably beyond the scope of a SO answer: docker image (see rpy2's own images with Jupyterlab or jupyterlab and spark), dedicated installer for precompiled binaries, .deb or .rpm packages, etc...