finds.utils.pyR

Wrapper class over rpy2 package to interface with R environment

Deconstruct and expose an rpy2 or numpy/pandas object interchangeably.

  • rpy2

Copyright 2022, Terence Lim

MIT License

class finds.utils.pyR.PyR(item: Any, names: StrVector | ListVector | str | List[str] | None = None, verbose: int = 0)[source]

Bases: object

Store and expose as rpy2 or numpy/pandas objects

Parameters:
  • item – input R object, or numpy array, or pandas DataFrame or Series

  • names – named labels of object items

Variables:
  • iloc

    dict or numpy array

    • internally, objects are stored as either numpy array, or dict of objects (when input was R ListVector or DataFrame, or Python dict).

    • TODO: should use other safer property getters to view object in target types: e,g, .frame (pandas), .ro (RObject), or .values (python dict or ndarray)

  • dim (tuple of int) – dimensions of data objects

  • colnames (names, rownames,) – named labels of object items

Notes:

  • input item can be of either Python or R object type

  • input labels in each dimension can be explicitly provided, and should have same dim as object, as error checking is minimal

  • In R, matrices are column-major ordered (aka Fortran-like index order, with the first index changing fastest) although the R constructor matrix() accepts a boolean argument byrow that, when true, will build the matrix as if row-major ordered (aka C-like, which is also Python numpy default order, where the last axis index changes fastest)

  • A suggested convention is to append ‘_’ to R function names and ‘_r’ to R objects, and capitalize initial letter of PyR instances.

  • r[‘plot’] may need to explicitliy set xlab=’’, ylab=’’

  • TODO: if hasattri(‘slots’), esp ‘ts’ class, e.g. nile.slots.items()

Examples:

>>> from rpy2.robjects import r
>>> from rpy2.robjects.packages import importr
>>> amen_r = importr('amen')                    # use R library
>>> c_ = r['c']                                 # link R routines
>>> Nodevars = PyR(r['IR90s'].rx2['nodevars'])  # retrieve R data
>>> Gdp = Nodevars[:, 'gdp']                    # getitem subset with slice
>>> topgdp = Gdp.values > sorted(Gdp.py, reverse=True)[30] # python calcs
>>> Dyadvars = PyR(r['IR90s'].rx2['dyadvars'])
>>> Y = Dyadvars[topgdp, topgdp, 'exports']  # getitem with boolean index
>>> Y.iloc = np.log(Y.iloc + 1)          # update with python calculations
__getitem__(args)[source]

Returns copy of subset of data object from given slice or index

assign(obj)[source]

Directly update internal data object (must be same numpy shape)

index(s: str | List[str], axis: int = -1)[source]

Helper method to lookup index/es of (list of) str label in names

static savefig(filename, display=True, ax=None, figsize=(12, 12))[source]

Save R graphics to file, or return R command, optionally imshow

property frame

Expose a view as pandas DataFrame

property ncol

Length of second dimension, as R IntVector type

property nrow

Length of first dimension, as R IntVector type

property ro

Expose a view as RObject, so that can pass to R environment

property values

Expose view as python dict (when ListVector) or ndarray (when not)

finds.utils.pyR.StrListVector(strList: ListVector | StrVector | str | List) StrVector | ListVector[source]

Convert nested list input to StrVector or ListVector