ContinuumIO / anaconda-issues

Anaconda issue tracking
648 stars 223 forks source link

Issue with rpy2 IPython magic on Windows #1263

Open alexanderwhatley opened 7 years ago

alexanderwhatley commented 7 years ago

Also posted here: https://bitbucket.org/rpy2/rpy2/issues/385/cell-magic-not-working-in-jupyter-notebook. I am running into issues with the rpy2 ipython magics on windows. The following code throws an error message:

%R library(edgeR)
%R y <- matrix( rpois(1000, lambda=5), nrow=200 )
%R calcNormFactors(y, method = "upperquartile")
Error in match.arg(method) : object 'upperquartile' not found
---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\ipython\rmagic.py in eval(self, code)
    241             # Need the newline in case the last line in code is a comment
--> 242             value, visible = ro.r("withVisible({%s\n})" % code)
    243         except (ri.RRuntimeError, ValueError) as exception:

C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\robjects\__init__.py in __call__(self, string)
    358         p = _rparse(text=StrSexpVector((string,)))
--> 359         res = self.eval(p)
    360         return conversion.ri2py(res)

C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    177                 kwargs[r_k] = v
--> 178         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    179 

C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    105             new_kwargs[k] = conversion.py2ri(v)
--> 106         res = super(Function, self).__call__(*new_args, **new_kwargs)
    107         res = conversion.ri2ro(res)

RRuntimeError: Error in match.arg(method) : object 'upperquartile' not found

During handling of the above exception, another exception occurred:

RInterpreterError                         Traceback (most recent call last)
C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\ipython\rmagic.py in R(self, line, cell, local_ns)
    670                 for line in code.split(';'):
--> 671                     text_result, result, visible = self.eval(line)
    672                     text_output += text_result

C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\ipython\rmagic.py in eval(self, code)
    244             warning_or_other_msg = self.flush() # otherwise next return seems to have copy of error
--> 245             raise RInterpreterError(code, str_to_unicode(str(exception)), warning_or_other_msg)
    246         text_output = self.flush()

RInterpreterError: Failed to parse and evaluate line 'calcNormFactors(y, method = upperquartile)'.
R error message: "Error in match.arg(method) : object 'upperquartile' not found"

During handling of the above exception, another exception occurred:

PermissionError                           Traceback (most recent call last)
<ipython-input-19-b44092050f2b> in <module>()
      1 get_ipython().magic('R library(edgeR)')
      2 get_ipython().magic('R y <- matrix( rpois(1000, lambda=5), nrow=200 )')
----> 3 get_ipython().magic('R calcNormFactors(y, method = "upperquartile")')

C:\Users\Alexander\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in magic(self, arg_s)
   2156         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2157         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2158         return self.run_line_magic(magic_name, magic_arg_s)
   2159 
   2160     #-------------------------------------------------------------------------

C:\Users\Alexander\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line)
   2077                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2078             with self.builtin_trap:
-> 2079                 result = fn(*args,**kwargs)
   2080             return result
   2081 

<decorator-gen-127> in R(self, line, cell, local_ns)

C:\Users\Alexander\Anaconda3\lib\site-packages\IPython\core\magic.py in <lambda>(f, *a, **k)
    186     # but it's overkill for just that one bit of state.
    187     def magic_deco(arg):
--> 188         call = lambda f, *a, **k: f(*a, **k)
    189 
    190         if callable(arg):

C:\Users\Alexander\Anaconda3\lib\site-packages\rpy2\ipython\rmagic.py in R(self, line, cell, local_ns)
    689             if not e.stdout.endswith(e.err):
    690                 print(e.err)
--> 691             if tmpd: rmtree(tmpd)
    692             return
    693         finally:

C:\Users\Alexander\Anaconda3\lib\shutil.py in rmtree(path, ignore_errors, onerror)
    486             os.close(fd)
    487     else:
--> 488         return _rmtree_unsafe(path, onerror)
    489 
    490 # Allow introspection of whether or not the hardening against symlink

C:\Users\Alexander\Anaconda3\lib\shutil.py in _rmtree_unsafe(path, onerror)
    381                 os.unlink(fullname)
    382             except OSError:
--> 383                 onerror(os.unlink, fullname, sys.exc_info())
    384     try:
    385         os.rmdir(path)

C:\Users\Alexander\Anaconda3\lib\shutil.py in _rmtree_unsafe(path, onerror)
    379         else:
    380             try:
--> 381                 os.unlink(fullname)
    382             except OSError:
    383                 onerror(os.unlink, fullname, sys.exc_info())

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\ALEXAN~1\\AppData\\Local\\Temp\\tmp6qk60d_y\\Rplots001.png'

The above code however does work on my mac.

davclark commented 7 years ago

I posted a reply on the above bitbucket issue tracker. Pretty sure it's an argparse on windows issue stripping out the double-quotes. Haven't compared to non-conda install, as I don't have admin rights on my windows computer.

stefanuddenberg commented 7 years ago

Any luck getting around this issue? I'm encountering it as well; I can't get any libraries to load when using the line magic (though I can use R in separate R-specific notebooks).

davclark commented 7 years ago

This is not an anaconda issue and so this is probably not the right issue tracker. It's actually a "bug" in argparse, which is in the standard library and I'm not sure it's considered a bug by the core python team. The fix is to:

  1. abandon IPython's parse_arguments and write something from scratch, or
  2. Get this functionality changed in core python, or
  3. Use single quotes instead of double quotes, or
  4. Use a virtualized solution, such as docker or a full VM.

The latter two are good enough for me. I know it's painful to leave something obviously "broken," but given the availability of an easy fix, it's not clear that it's worth fixing.