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)
print(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 [libpython3.10.so+0x1568ce] 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?