-
Notifications
You must be signed in to change notification settings - Fork 532
ENH: Interface for R #3291
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
Merged
Merged
ENH: Interface for R #3291
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
931a766
interface for R
Terf b336132
return None if R can't be found
Terf 2638489
remove hardcoded variable
Terf f7cb9b2
R interface tests
Terf 0dcf413
Update nipype/interfaces/r.py
Terf 190b6f4
Update nipype/interfaces/tests/test_r.py
Terf 02104a7
Update nipype/interfaces/tests/test_r.py
Terf 6fcf05b
Update nipype/interfaces/tests/test_r.py
Terf b74fc85
Update nipype/interfaces/tests/test_r.py
Terf f72b510
Update nipype/interfaces/tests/test_r.py
Terf 3d64c26
Update nipype/interfaces/tests/test_r.py
Terf c4cb539
Update nipype/interfaces/tests/test_r.py
Terf 9cad3cd
@effigies R interface suggestions
Terf 6aab778
Update nipype/interfaces/tests/test_r.py
Terf 925905c
Merge branch 'nipy:master' into r-interface
Terf 7f3713b
ran `make specs`
Terf 7f92f63
un-chdir R test
Terf 52e1a98
ran `black .`
Terf e576c90
skip in CI as R isn't installed
Terf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# -*- coding: utf-8 -*- | ||
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
"""Interfaces to run R scripts.""" | ||
import os | ||
|
||
from .. import config | ||
from .base import ( | ||
CommandLineInputSpec, | ||
InputMultiPath, | ||
isdefined, | ||
CommandLine, | ||
traits, | ||
File, | ||
Directory, | ||
) | ||
|
||
|
||
def get_r_command(): | ||
if "NIPYPE_NO_R" in os.environ: | ||
return None | ||
try: | ||
r_cmd = os.environ["RCMD"] | ||
except: | ||
r_cmd = "R" | ||
return r_cmd | ||
Terf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
no_r = get_r_command() is None | ||
|
||
|
||
class RInputSpec(CommandLineInputSpec): | ||
""" Basic expected inputs to R interface """ | ||
|
||
script = traits.Str( | ||
argstr='-e "%s"', desc="R code to run", mandatory=True, position=-1 | ||
) | ||
# non-commandline options | ||
rfile = traits.Bool(True, desc="Run R using R script", usedefault=True) | ||
script_file = File( | ||
"pyscript.R", usedefault=True, desc="Name of file to write R code to" | ||
) | ||
|
||
|
||
class RCommand(CommandLine): | ||
"""Interface that runs R code | ||
|
||
>>> import nipype.interfaces.r as r | ||
>>> r = r.RCommand(rfile=False) # don't write script file | ||
>>> r.inputs.script = "Sys.getenv('USER')" | ||
>>> out = r.run() # doctest: +SKIP | ||
""" | ||
|
||
_cmd = "R" | ||
Terf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_default_r_cmd = None | ||
_default_rfile = None | ||
input_spec = RInputSpec | ||
|
||
def __init__(self, r_cmd=None, **inputs): | ||
"""initializes interface to r | ||
(default 'R') | ||
""" | ||
super(RCommand, self).__init__(**inputs) | ||
if r_cmd and isdefined(r_cmd): | ||
self._cmd = r_cmd | ||
elif self._default_r_cmd: | ||
self._cmd = self._default_r_cmd | ||
|
||
if self._default_rfile and not isdefined(self.inputs.rfile): | ||
self.inputs.rfile = self._default_rfile | ||
|
||
# For r commands force all output to be returned since r | ||
# does not have a clean way of notifying an error | ||
self.terminal_output = "allatonce" | ||
|
||
@classmethod | ||
def set_default_r_cmd(cls, r_cmd): | ||
"""Set the default R command line for R classes. | ||
|
||
This method is used to set values for all R | ||
subclasses. However, setting this will not update the output | ||
type for any existing instances. For these, assign the | ||
<instance>.inputs.r_cmd. | ||
""" | ||
cls._default_r_cmd = r_cmd | ||
|
||
@classmethod | ||
def set_default_rfile(cls, rfile): | ||
"""Set the default R script file format for R classes. | ||
|
||
This method is used to set values for all R | ||
subclasses. However, setting this will not update the output | ||
type for any existing instances. For these, assign the | ||
<instance>.inputs.rfile. | ||
""" | ||
cls._default_rfile = rfile | ||
|
||
def _run_interface(self, runtime): | ||
self.terminal_output = "allatonce" | ||
runtime = super(RCommand, self)._run_interface(runtime) | ||
if "R code threw an exception" in runtime.stderr: | ||
self.raise_exception(runtime) | ||
return runtime | ||
|
||
def _format_arg(self, name, trait_spec, value): | ||
if name in ["script"]: | ||
argstr = trait_spec.argstr | ||
return self._gen_r_command(argstr, value) | ||
return super(RCommand, self)._format_arg(name, trait_spec, value) | ||
|
||
def _gen_r_command(self, argstr, script_lines): | ||
""" Generates commands and, if rfile specified, writes it to disk.""" | ||
if not self.inputs.rfile: | ||
# replace newlines with ;, strip comments | ||
script = "; ".join([ | ||
line | ||
for line in script_lines.split("\n") | ||
if not line.strip().startswith("#") | ||
]) | ||
# escape " and $ | ||
script = script.replace('"','\\"') | ||
script = script.replace('$','\\$') | ||
else: | ||
script_path = os.path.join(os.getcwd(), self.inputs.script_file) | ||
with open(script_path, "wt") as rfile: | ||
rfile.write(script_lines) | ||
script = "source('%s')" % script_path | ||
|
||
return argstr % script |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.