1

I am using PyCharm and try to import rpy2 (version 3.3.5) with the following code:

import numpy as np
import pandas as pd
import rpy2.robjects.packages as packages

print("TEST")

I get the following error:

R[write to console]: Error: cons memory exhausted (limit reached?)

R[write to console]: Error: no more error handlers available (recursive errors?); invoking 'abort' restart


Process finished with exit code -1073741784 (0xC0000028)

I am using Windows 10, have 16GB RAM available.

buhtz
  • 10,774
  • 18
  • 76
  • 149
Ankur B.
  • 313
  • 1
  • 3
  • 6
  • It might be that you are not getting any answers because you did not provide the details of your environment (R version, rpy2 version, Python version, etc). In your example you import pandas and numpy - does the error also happen if you do not do that? – krassowski Oct 10 '20 at 11:32
  • 1
    On general note, rpy2 usually works better one unix machines; if I were to use it on Windows, I would consider experimenting with the [Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/install-win10), but it might not be the solution if you just expect to work it from an IDE. – krassowski Oct 10 '20 at 11:34

2 Answers2

2

I have run into the same issue and after extensive research and a lot of trial and error, I have two solutions:

  1. If you can use a Jupyter notebook (even in Pycharm), the cons memory exhausted error will not happen when importing rpy2 modules.

  2. If you need to use the Python console in Pycharm when running your code importing rpy2 modules and want to avoid this error, there is a workaround I use. I noticed that this error may happen due to an active renv environment set up for R in the same project. When R is set up in the same project with Python together with renv dependency management, there is a .Rprofile file in the root folder to activate renv for new R sessions. When the code is not executed (e.g. commented out) when importing an rpy2 module then there is no memory exhaust error. Once the rpy2 module is imported, renv can be activated again and all rpy2 code will execute as normal. Moreover, rpy2 can be directed to the renv environment to use packages from R project dependencies. To summarise the steps to the workaround, I wrote a function that: 1. opens .Rprofile and comments out all code 2. using Python's importlib I import the necessary rpy2 module 3. set the R_LIBS_USER environment variable to the path of the renv library path 4. remove the commenting from .Rprofile file. I will put this code on my Github later.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ferkó
  • 21
  • 2
-1

Try to removed all files generated by RStudio in your directory.

Kalana
  • 5,631
  • 7
  • 30
  • 51
Pierre C
  • 1
  • 4
  • 1
    Appreciate that you do not have enough reputation to comment, but this is not a good answer https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead – gofvonx Mar 24 '21 at 10:07