Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing rpy2.robjects fails with R>=4.2 #874

Closed
scottgigante-immunai opened this issue Apr 26, 2022 · 19 comments
Closed

Importing rpy2.robjects fails with R>=4.2 #874

scottgigante-immunai opened this issue Apr 26, 2022 · 19 comments
Assignees
Labels
bug Something isn't working

Comments

@scottgigante-immunai
Copy link

Describe the issue or bug

rpy2 cannot be imported if the R version is >=4.2

To Reproduce

import rpy2.robjects

Error

Error in .makeMessage(..., domain = domain) : 
  3 arguments passed to .Internal(gettext) which requires 2
Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2
R[write to console]: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/rpy2/robjects/__init__.py", line 19, in <module>
    import rpy2.robjects.functions
  File "/opt/hostedtoolcache/Python/3.7.12/x[64](https://github.com/scottgigante-immunai/scprep/runs/6183376716?check_suite_focus=true#step:4:64)/lib/python3.7/site-packages/rpy2/robjects/functions.py", line 11, in <module>
    from rpy2.robjects import help
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/rpy2/robjects/help.py", line 45, in <module>
    quiet_require('tools')
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/rpy2/robjects/help.py", line 41, in quiet_require
    ok = _eval(expr)
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py", line 45, in _
    cdata = function(*args, **kwargs)
  File "/opt/hostedtoolcache/Python/3.7.12/x64/lib/python3.7/site-packages/rpy2/rinterface.py", line 810, in __call__
    raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

Additional context
Example Github Actions workflow reproducing the error is here: https://github.com/scottgigante-immunai/scprep/actions/runs/2229334197/workflow

@lgautier
Copy link
Member

rpy2's test matrix has R-4.2.0 (for example, see https://github.com/rpy2/rpy2/runs/6141867886?check_suite_focus=true) and it appears that it installs and passes all automated tests.

The error message observed might be caused by the R environment setup not telling the linker where to find the R shared library. The rpy2 tests suite ensures that this is the case:
https://github.com/rpy2/rpy2/blob/master/.github/workflows/pythonpackage.yml

I am closing this as resolved. Reopen if the diagnosis is incorrect.

@scottgigante-immunai
Copy link
Author

Okay, I figured it out. In order for rpy2 to work on Actions with R >= 4.2, you need to

  • use actions/setup-python@v1 (NOT actions/setup-python@v2)
  • Set LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH):${LD_LIBRARY_PATH}

Example here: https://github.com/scottgigante-immunai/scprep/runs/6241490722?check_suite_focus=true

lgautier added a commit that referenced this issue May 1, 2022
Comment in issue #874 suggests that v1 works but v2 does not.
@vnijs
Copy link

vnijs commented May 3, 2022

@scottgigante-immunai I'm having a similar issue with rpy2, installed using conda, and rpy2 (see below). Can you provide a bit more detail perhaps on how you got this working? Thanks

❯ export LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH):${LD_LIBRARY_PATH}
❯ python -c 'from rpy2.robjects import r'

Error in .makeMessage(..., domain = domain) : 
  3 arguments passed to .Internal(gettext) which requires 2
Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2
R[write to console]: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/conda/lib/python3.9/site-packages/rpy2/robjects/__init__.py", line 19, in <module>
    import rpy2.robjects.functions
  File "/opt/conda/lib/python3.9/site-packages/rpy2/robjects/functions.py", line 11, in <module>
    from rpy2.robjects import help
  File "/opt/conda/lib/python3.9/site-packages/rpy2/robjects/help.py", line 45, in <module>
    quiet_require('tools')
  File "/opt/conda/lib/python3.9/site-packages/rpy2/robjects/help.py", line 41, in quiet_require
    ok = _eval(expr)
  File "/opt/conda/lib/python3.9/site-packages/rpy2/rinterface_lib/conversion.py", line 45, in _
    cdata = function(*args, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/rpy2/rinterface.py", line 810, in __call__
    raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

❯ python -m rpy2.situation

rpy2 version:
3.5.1
Python version:
3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:51:20) 
[GCC 10.3.0]
Looking for R's HOME:
    Environment variable R_HOME: /usr/local/lib/R
    Calling `R RHOME`: /usr/local/lib/R
    Environment variable R_LIBS_USER: None
R's additions to LD_LIBRARY_PATH:
/usr/local/lib/R/lib:/usr/local/lib:/usr/lib/jvm/java-11-openjdk-arm64/lib/server
R version:
    In the PATH: R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
    Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
  include:
  ['/usr/local/lib/R/include']
  libraries:
  ['R', 'pcre2-8', 'lzma', 'bz2', 'z', 'rt', 'dl', 'm', 'iconv', 'icuuc', 'icui18n']
  library_dirs:
  ['/usr/local/lib', '/usr/local/lib/R/lib']
  extra_compile_args:
  []
  extra_link_args:
  ['-Wl,--export-dynamic', '-fopenmp']

@scottgigante-immunai
Copy link
Author

@vnijs unfortunately I don't know what actually fixed it. But since setup-python v2 breaks and v1 works, whatever actually fixed it is located in this diff

@vnijs
Copy link

vnijs commented May 4, 2022

@scottgigante-immunai Not sure I follow. Do you have rpy2 working with R 4.2? If so, what did you do in addition to the below?

LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH):${LD_LIBRARY_PATH}

@scottgigante-immunai
Copy link
Author

I'm running on Github Actions, and in addition to the LD_LIBRARY_PATH step above, I used a different Python install (actions/setup-python@v1 rather than actions/setup-python@v2). There are quite a few differences between those two, and unfortunately I don't know which one made the difference; I just know that v1 works and v2 doesn't.

@vnijs
Copy link

vnijs commented May 8, 2022

@lgautier Would you consider re-opening this issue? It seems there is an issue with rpy2 and R2.4, at least as it relates a specific form of python set up #876

@lgautier lgautier reopened this May 8, 2022
@vnijs
Copy link

vnijs commented May 16, 2022

@lgautier I could use your help to see how to resolve this issue. To get a reproducible example, please run the below. Sorry, the image is large.

docker run --rm -p 8989:8989 vnijs/rsm-jupyter:2.4.0

JupyterLab will be available at the link below:

http://localhost:8989/lab

You will see a popup with the message "JupyterLab Code Formatter Error". The issue linked below describes the problem with rpy2 and R 4.2.0

ryantam626/jupyterlab_code_formatter#258

In jupyterlab you can directly see the problem by using from rpy2.robjects import r

Note that we have been using rpy2 with this docker image design for some time and it works great with R 4.1.2. See below.

docker run --rm -p 8989:8989 vnijs/rsm-jupyter:2.3.0

I would appreciate any guidance you can provide. Thanks

-- Vincent

@lgautier
Copy link
Member

@scottgigante-immunai - This might have been an issue with actions/setup-python@v2. Python 3.10 and R-4.2 appears to be working fine when using v2 now:
#876

lgautier added a commit that referenced this issue May 23, 2022
Upgrading to v2 of Python's setup on GHA.

Issue #874 reported failure with v2, but it no longer seems the case.
@vnijs
Copy link

vnijs commented May 23, 2022

@lgautier FYI The issue we are having with rpy2 is with Python 3.9.12 (conda) and R 4.2.0 installed from source. Both R and Python work fine.

@scottgigante-immunai
Copy link
Author

@lgautier can confirm this is fixed with v1, v2, v3, whether or not we set LD_LIBRARY_PATH. Config here: https://github.com/scottgigante-immunai/scprep/actions/runs/2404691023

@GegznaV
Copy link

GegznaV commented Nov 13, 2022

Similar issue:
https://stackoverflow.com/questions/74420263/ipynb-load-ext-rpy2-ipython-errors-3-arguments-passed-to-internalgette
Could you, please, add your comment on Stack Overflow?

@brainfo
Copy link

brainfo commented Feb 13, 2023

Similar issue: https://stackoverflow.com/questions/74420263/ipynb-load-ext-rpy2-ipython-errors-3-arguments-passed-to-internalgette Could you, please, add your comment on Stack Overflow?

same here: from rpy2.robjects import help

But I found it only in jupyter lab. With python script in the same env, it works fine.

File /mnt/data/hong/anaconda3/envs/scanpy/lib/python3.9/site-packages/rpy2/robjects/help.py:41, in quiet_require(name, lib_loc)
     38 expr_txt = ('suppressPackageStartupMessages(base::require(%s, lib.loc=%s))'
     39             % (name, lib_loc))
     40 expr = rinterface.parse(expr_txt)
---> 41 ok = _eval(expr)
     42 return ok

File /mnt/data/hong/anaconda3/envs/scanpy/lib/python3.9/site-packages/rpy2/rinterface_lib/conversion.py:45, in _cdata_res_to_rinterface.<locals>._(*args, **kwargs)
     44 def _(*args, **kwargs):
---> 45     cdata = function(*args, **kwargs)
     46     # TODO: test cdata is of the expected CType
     47     return _cdata_to_rinterface(cdata)

File /mnt/data/hong/anaconda3/envs/scanpy/lib/python3.9/site-packages/rpy2/rinterface.py:817, in SexpClosure.__call__(self, *args, **kwargs)
    810     res = rmemory.protect(
    811         openrlib.rlib.R_tryEval(
    812             call_r,
    813             call_context.__sexp__._cdata,
    814             error_occured)
    815     )
    816     if error_occured[0]:
--> 817         raise embedded.RRuntimeError(_rinterface._geterrmessage())
    818 return res

RRuntimeError: Error in gettext(fmt, domain = domain, trim = trim) : 
  3 arguments passed to .Internal(gettext) which requires 2

@phantom-duck
Copy link

@GegznaV The issue in stackoverflow you linked to seems very similar to the one I had. In my case, defining an environmental variable R_HOME to the home folder of the R installation seems to have worked.

e.g. in PowerShell: $env:R_HOME = 'C:\Program Files\R\R-4.2.2'

A link that proved a bit helpful: https://rpy2.github.io/doc/v3.5.x/html/rinterface.html#module-rpy2.rinterface

Specifically, the part about rpy2.rinterface.initr has a note which was the only place I found that mentioned something along the lines of the problem we seem to be facing here.

@RichardScottOZ
Copy link

That did seem to stop the error

Could this be related to R installed for system and things installed for user? e.g. AppData etc.

@phantom-duck
Copy link

In my case, I believe that it had to do with the R executable not having been added to my PATH environmental variable. Possibly because I did not allow it during installation of R, but I do not remember. So, if, for example, I open up a terminal and try to invoke an R executable, e.g. Rscript, it does not work, it cannot find it. And most probably the same goes for my rpy2 library, it failed to find the executable of R. and that is why I needed to provide it with the R_HOME variable.

Of course, I do not know if this applies to your case as well.

@ramdhanhdy
Copy link

@GegznaV The issue in stackoverflow you linked to seems very similar to the one I had. In my case, defining an environmental variable R_HOME to the home folder of the R installation seems to have worked.

e.g. in PowerShell: $env:R_HOME = 'C:\Program Files\R\R-4.2.2'

A link that proved a bit helpful: https://rpy2.github.io/doc/v3.5.x/html/rinterface.html#module-rpy2.rinterface

Specifically, the part about rpy2.rinterface.initr has a note which was the only place I found that mentioned something along the lines of the problem we seem to be facing here.

I still get the same error

@EngrStudent
Copy link

Okay, I figured it out. In order for rpy2 to work on Actions with R >= 4.2, you need to

  • use actions/setup-python@v1 (NOT actions/setup-python@v2)
  • Set LD_LIBRARY_PATH=$(python -m rpy2.situation LD_LIBRARY_PATH):${LD_LIBRARY_PATH}

Example here: https://github.com/scottgigante-immunai/scprep/runs/6241490722?check_suite_focus=true

Actions for this link have expired and are no longer available.

@lgautier
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants