-->

rpy2 importr failing with xts and quantmod

2019-02-18 22:48发布

问题:

I'm new to rpy2 and am having trouble using importr to import the R packages 'xts' and 'quantmod'

Code is:

from rpy2.robjects.packages import importr
xts = importr('xts')
quantmod = importr('quantmod')

Errors are:

LibraryError: Conflict when converting R symbol in the package "xts" to a Python symbol (.subset.xts -> _subset_xts while there is already _subset_xts)

LibraryError: Conflict when converting R symbol in the package "quantmod" to a Python symbol (skeleton.TA -> skeleton_TA while there is already skeleton_TA)

I don't get this problem using importr for many other packages, e.g. 'stats', 'graphics', 'zoo', 'ggplot2'

Versions:

  • python version 2.7.3
  • R version 2.15.2
  • rpy2 version '2.3.0beta1'

Any help would be greatly appreciated

回答1:

Rpy2's importr() is trying to convert any "." in R object names to "_" for usage with Python.

However, whenever there are two R object names with either "." or "_" (both characters are valid for names in R) rpy2 is reporting an error. Here the R package "xts" is defining the two objects .subset_xts and .subset.xts. The workaround is specify manually how to convert names:

from rpy2.robjects.packages import import
xts = importr("xts", robject_translations = {".subset.xts": "_subset_xts2", 
                                             "to.period": "to_period2"})

More is available in the rpy2 documentation about importing R packages.