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

Non-interactive (batch) mode #976

Closed
djbios opened this issue Jan 16, 2023 · 4 comments
Closed

Non-interactive (batch) mode #976

djbios opened this issue Jan 16, 2023 · 4 comments

Comments

@djbios
Copy link

djbios commented Jan 16, 2023

We are currently utilizing rpy2 in our production code and have encountered issues with the interactive mode causing the entire application to become stuck when certain libraries are awaiting input.

A solution would be to implement a way to force rpy2 to operate in non-interactive mode for R.

@lgautier
Copy link
Member

lgautier commented Jan 16, 2023

Initializing the embedded R in non-interactive mode is more tedious and less documented than ideal, but you might be able to get away with it by setting R to non-interactive after initialization. Try:

import rpy2.rinterface as ri
ri.initr()
ri.openrlib.rlib.R_Interactive = False

djbios added a commit to immunomind/rpy2 that referenced this issue Jan 17, 2023
djbios added a commit to immunomind/rpy2 that referenced this issue Jan 17, 2023
Fix rpy2#976 by configuring interactive mode through environment variable
@djbios
Copy link
Author

djbios commented Jan 17, 2023

@lgautier Thanks for the answer! Unfortunately, because it could be more than one entry point in production code (for example in the application and in the tests) this solution leads to a violation of DRY and it's easy to forget to write it in some new entry point. Could you please consider my PR #980 with an environment variable-based setting? It allows setting batch mode in a clean way in one place.

djbios added a commit to immunomind/rpy2 that referenced this issue Jan 20, 2023
lgautier added a commit that referenced this issue Jan 29, 2023
The initialization variables are the R interactivity state (issue #976) and
the max size of the C stack.
@lgautier
Copy link
Member

I am not a big fan of having an environment variable here. The interactivity status can be set (changed) at any time in the Python code.

The embedded R is initialized only once (the public function initr is protected from accidental repeated attempts to initialize). I believe that applying DRY principles is up to the user of the library here. Having one entry point to set up the initialization is straightforward. For example, you have a module my-prod-code-setup.py that is called at top-level. That module can have the lines:

import rpy2.rinterface_lib.embedded
rpy2.rinterface_lib.embedded._initr(interactive=False)
# from now R will stay initialized and in non-interactive mode

I caught missed in the API though, and wrote an PR to fix them. With PR #986 it can be written

import rpy2.rinterface as ri
ri.initr(interactive=False)
# from now R will stay initialized and in non-interactive mode

I have also made the default initialization a module-level variable, albeit not a public one to indicate that things are less safe.
It is possible to write the following

import rpy2.rinterface.embedded
rpy2.rinterface_embedded._DEFAULT_R_INTERACTIVE = False
# from now on the default interactive-mode for R as initialization is the non-interactive mode...
# ... unless this is reverted before R is initialized, or R is initialized when a different interactive mode.

lgautier added a commit that referenced this issue Feb 4, 2023
* Default for R initialization are now module-level variables.

The initialization variables are the R interactivity state (issue #976) and
the max size of the C stack.
@lgautier
Copy link
Member

Fixed with PR #986 , and included in rpy2 release 3.5.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants