def rpy2_to_pandas(r_df, rpy2_version_major=None): if rpy2_version_major is None: from rpy2 import __version__ as rpy2_version rpy2_version_major = int(rpy2_version.split(".")[0]) if rpy2_version_major == 2: # v2.x return pandas2ri.ri2py(r_df) if rpy2_version_major == 3: # v3.x from rpy2.robjects.conversion import localconverter with localconverter(ro.default_converter + pandas2ri.converter): return ro.conversion.rpy2py(r_df)
# ============================================================================== # Imports # ============================================================================== # Built-ins import os, sys # ============================================================================== # R Compatibility # ============================================================================== from ..utils import check_packages # if "rpy2" in sys.modules: from rpy2 import robjects as ro from rpy2 import rinterface as ri from rpy2.robjects import pandas2ri from rpy2 import __version__ as rpy2_version rpy2_version_major = int(rpy2_version.split(".")[0]) rpy2_version_minor = int(rpy2_version.split(".")[1]) # rpy2_version_micro = int(rpy2.__version__[2]) assert rpy2_version_major > 1, "Please update your rpy2 version" # Converters # @check_packages(["rpy2"], language="python", import_into_backend=False) def pandas_to_rpy2(df, rpy2_version_major=None): if rpy2_version_major is None: from rpy2 import __version__ as rpy2_version rpy2_version_major = int(rpy2_version.split(".")[0]) if rpy2_version_major == 2: # v2.x return pandas2ri.py2ri(df) if rpy2_version_major == 3: # v3.x from rpy2.robjects.conversion import localconverter