Skip to content

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 19 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
@effigies R interface suggestions
  • Loading branch information
Terf committed Jun 1, 2021
commit 9cad3cdddc66f8d0d2f8dc6c5c8a22cb3e071c0a
25 changes: 6 additions & 19 deletions nipype/interfaces/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class RCommand(CommandLine):
"""

_cmd = get_r_command()
_default_r_cmd = None
_default_rfile = None
input_spec = RInputSpec

def __init__(self, r_cmd=None, **inputs):
Expand All @@ -62,37 +60,26 @@ def __init__(self, r_cmd=None, **inputs):
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):
def set_default_r_cmd(self, 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.
subclasses.
"""
cls._default_r_cmd = r_cmd
self._cmd = r_cmd

@classmethod
def set_default_rfile(cls, rfile):
def set_default_rfile(self, 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.
subclasses.
"""
cls._default_rfile = rfile
self._rfile = rfile

def _run_interface(self, runtime):
self.terminal_output = "allatonce"
Expand Down
57 changes: 11 additions & 46 deletions nipype/interfaces/tests/test_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,28 @@

no_r = r.no_r


def clean_workspace_and_get_default_script_file():
# Make sure things are clean.
default_script_file = r.RInputSpec().script_file
if os.path.exists(default_script_file):
os.remove(
default_script_file
) # raise Exception('Default script file needed for tests; please remove %s!' % default_script_file)
return default_script_file


@pytest.mark.skipif(no_r, reason="R is not available")
def test_cmdline(tmp_path):
ri = r.RCommand(script="1 + 1", script_file=str(tmp_path / "testscript"), rfile=False)
default_script_file = str(tmp_path / "testscript")
ri = r.RCommand(script="1 + 1", script_file=default_script_file, rfile=False)
r_cmd = r.get_r_command()

assert ri.cmdline == r_cmd + (
' -e "1 + 1"'
)

assert ri.inputs.script == "1 + 1"
assert ri.inputs.script_file == str(tmp_path / "testscript")
assert ri.inputs.script_file == default_script_file
assert not os.path.exists(ri.inputs.script_file), "scriptfile should not exist"
assert not os.path.exists(
default_script_file
), "default scriptfile should not exist."


@pytest.mark.skipif(no_r, reason="R is not available")
def test_r_init():
default_script_file = clean_workspace_and_get_default_script_file()

assert r.RCommand._cmd == r.get_r_command()
assert r.RCommand.input_spec == r.RInputSpec

assert r.RCommand().cmd == r_cmd
rc = r.RCommand(r_cmd="foo_m")
assert rc.cmd == "foo_m"


@pytest.mark.skipif(no_r, reason="R is not available")
def test_run_interface(tmpdir):
default_script_file = clean_workspace_and_get_default_script_file()
os.chdir(tmpdir)
default_script_file = r.RInputSpec().script_file

rc = r.RCommand(r_cmd="foo_m")
assert not os.path.exists(default_script_file), "scriptfile should not exist 1."
Expand All @@ -67,31 +47,16 @@ def test_run_interface(tmpdir):
if os.path.exists(default_script_file): # cleanup
os.remove(default_script_file)

cwd = tmpdir.chdir()

# bypasses ubuntu dash issue
rc = r.RCommand(script="foo;", rfile=True)
assert not os.path.exists(default_script_file), "scriptfile should not exist 4."
with pytest.raises(RuntimeError):
rc.run()
assert os.path.exists(default_script_file), "scriptfile should exist 4."
if os.path.exists(default_script_file): # cleanup
os.remove(default_script_file)

# bypasses ubuntu dash issue
res = r.RCommand(script="a=1;", rfile=True).run()
assert res.runtime.returncode == 0
assert os.path.exists(default_script_file), "scriptfile should exist 5."
cwd.chdir()


@pytest.mark.skipif(no_r, reason="R is not available")
def test_set_rcmd():
default_script_file = clean_workspace_and_get_default_script_file()
def test_set_rcmd(tmpdir):
os.chdir(tmpdir)
default_script_file = r.RInputSpec().script_file

ri = r.RCommand()
_default_r_cmd = ri._default_r_cmd
_default_r_cmd = ri._cmd
ri.set_default_r_cmd("foo")
assert not os.path.exists(default_script_file), "scriptfile should not exist."
assert ri._default_r_cmd == "foo"
assert ri._cmd == "foo"
ri.set_default_r_cmd(_default_r_cmd)