RStudio + Python + rpy2 (+ reticulate)

We would like to use Python in RStudio for some financial models. However, we would like to leverage our existing R infrastructure (we use RStudio server on Ubuntu, with our data stored in MariaDB). For example, we have a getprices R function that retrieves stock prices from our database, which we would ideally like to use to get these data into the Python models, rather than write a Python "get prices" funcion.

The rpy2 python package allows access to R functions from Python, so this seems like a good place to start, and we have a proof of concept that works when run directly in a Python REPL in the RStudio terminal:

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
FlightBag = importr('FlightBag')  # FlightBag is our internal package.
result = robjects.r['getprices']('AAPL_US', retdata = True)
result = robjects.r['last'](result)
#             Close
# 2022-05-12 142.56

However, when I try the same code in the RStudio console using reticulate, it crashes when importing our FlightBag pakage:

FlightBag = importr('FlightBag')
# A fatal error has been detected by the Java Runtime Environment:
#  SIGSEGV (0xb) at pc=0x00007f7af04038ce (sent by kill), pid=133533, tid=133533
# JRE version: OpenJDK Runtime Environment (11.0.15+10) (build 11.0.15+10-Ubuntu-0ubuntu0.20.04.1)
# Java VM: OpenJDK 64-Bit Server VM (11.0.15+10-Ubuntu-0ubuntu0.20.04.1, mixed mode, sharing, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  []  method_dealloc+0x6e
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %d %P %E" (or dumping to /srv/shiny-server/FlightBag/core.133533)
# An error report file with more information is saved as:
# /srv/shiny-server/FlightBag/hs_err_pid133533.log

Similarly, trying to import other packages, such as stats or the tidyverse, also fails with the same error.

The reason we would ideally want to run the code in the RStudio console using reticulate is so that we can inspect the objects in the Environment pane etc.

Is there a reason why this won't work, or should it be tractable?

While you may be able to call/access R functions from Python, I don't think the reverse is going to work. Something is bound to fail somewhere whether in the import process or data conversion.
Sorry can't be more helpful!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.