Skip to content

Problems when using psimpy on jupyterhub

In line https://git.rwth-aachen.de/mbd/psimpy/-/blob/main/src/psimpy/emulator/robustgasp.py?ref_type=heads#L3 it checks for CONDA_PREFIX. However, when using psimpy on jupyterhub conda is not explicitly activated and hence CONDA_PREFIX is not set. This gives the following error:

Unable to determine R home: [Errno 2] No such file or directory: 'R'

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 1
----> 1 from psimpy.emulator import ScalarGaSP

File /opt/conda/envs/python312/lib/python3.12/site-packages/psimpy/emulator/__init__.py:1
----> 1 from psimpy.emulator.robustgasp import ScalarGaSP, PPGaSP

File /opt/conda/envs/python312/lib/python3.12/site-packages/psimpy/emulator/robustgasp.py:10
      7         os.environ['R_HOME'] = R_home
      9 import numpy as np
---> 10 from rpy2.robjects.packages import importr
     11 import rpy2.robjects.numpy2ri
     12 from rpy2.rinterface import NA

File /opt/conda/envs/python312/lib/python3.12/site-packages/rpy2/robjects/__init__.py:18
     15 import rpy2.rinterface as rinterface
     16 import rpy2.rlike.container as rlc
---> 18 from rpy2.robjects.robject import RObjectMixin, RObject
     19 import rpy2.robjects.functions
     20 from rpy2.robjects.environments import (Environment,
     21                                         local_context)

File /opt/conda/envs/python312/lib/python3.12/site-packages/rpy2/robjects/robject.py:11
      7 import rpy2.rinterface_lib.callbacks
      9 from rpy2.robjects import conversion
---> 11 rpy2.rinterface.initr_simple()
     14 def _add_warn_reticulate_hook():
     15     msg = """
     16     WARNING: The R package "reticulate" only fixed recently
     17     an issue that caused a segfault when used with rpy2:
   (...)     20     the fix.
     21     """

File /opt/conda/envs/python312/lib/python3.12/site-packages/rpy2/rinterface.py:998, in initr_simple()
    996 """Initialize R's embedded C library."""
    997 with openrlib.rlock:
--> 998     status = embedded._initr()
    999     atexit.register(endr, 0)
   1000     _rinterface._register_external_symbols()

File /opt/conda/envs/python312/lib/python3.12/site-packages/rpy2/rinterface_lib/embedded.py:272, in _initr(interactive, _want_setcallbacks, _c_stack_limit)
    270     return None
    271 elif openrlib.R_HOME is None:
--> 272     raise ValueError('openrlib.R_HOME cannot be None.')
    273 elif openrlib.rlib.R_NilValue != ffi.NULL:
    274     msg = ('R was initialized outside of rpy2 (R_NilValue != NULL). '
    275            'Trying to use it nevertheless.')

ValueError: openrlib.R_HOME cannot be None.

As a quick fix we hardcoded the CONDA_PREFIX manually as below: os.environ["CONDA_PREFIX"]='/opt/conda/envs/python312'

A better solution needs to be found.