vitorcurtis / RWinOut

Workaround for R output on IPython
MIT License
7 stars 1 forks source link

RWinOut no longer works with rpy2 in Jupyter NB... RWinOut throws error #2

Open richlysakowski opened 4 years ago

richlysakowski commented 4 years ago

As soon as I use RWinout, I get a fatal error that requires me to restart Jupyter NB server, it kills the Jupyter Notebook Python kernel.

I wish I could get this to work, because polyglot NBs with Plotly-Dash, Jupyter NB extensions like Qgrid, and many others make a nice combination for richly interactive data science applications, without having to run additional servers like RShiny or others.

I can load rpy2 and use its functions and objects, but not R cell magics.

###################################################### Here's my version information:

R version: ('3', '6.0', '', 76424) Rpy2 veresion: 2.9.4 Python version 3.7.4: jupyter core : 4.5.0 jupyter-notebook : 6.0.1 qtconsole : 4.5.5 ipython : 7.8.0 ipykernel : 5.1.2 jupyter client : 5.3.3 jupyter lab : 1.1.4 nbconvert : 5.6.0 ipywidgets : 7.5.1 nbformat : 4.4.0 traitlets : 4.3.3 7.8.0 Everything was installed in this environment using Anaconda Navigator, or conda from the command console, unless pip install was required for something that didn't have a conda package.

Conda is the strongly preferred package manager for anything to do with the Jupyter ecosystem, because it is designed to be language-agnostic.
######################################################

The R inline magics produce output into the Jupyter Output Cell, but R cell magics put the results out to the Windows Console that I used to start the Jupyter Notebook.

As soon as I run RWinOut, the errors below show up and the kernel dies.

I also get the following messages in the Windows Console:

Loading required package: R.oo
Loading required package: R.methodsS3
R.methodsS3 v1.8.0 (2020-02-14 07:10:20 UTC) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.23.0 successfully loaded. See ?R.oo for help.
Attaching package: 'R.oo'
The following object is masked from 'package:R.methodsS3':
    throw
The following objects are masked from 'package:methods':
    getClasses, getMethods
The following objects are masked from 'package:base':
    attach, detach, load, save
R.utils v2.9.2 successfully loaded. See ?R.utils for help.
Attaching package: 'R.utils'
The following object is masked from 'package:utils':
    timestamp
The following objects are masked from 'package:base':
    cat, commandArgs, getOption, inherits, isOpen, nullfile, parse,
    warnings

The strange thing is that inline R magics work okay, but NOT Cell magics.

When I run RWinOut as follows:

    !curl -O "https://raw.githubusercontent.com/vitorcurtis/RWinOut/master/RWinOut.py"

    %reload_ext RWinOut

I get a long string of errors (below). It also kills the Python kernel for the Jupyter NB.

The rpy2.ipython extension is already loaded. To reload it, use:
  %reload_ext rpy2.ipython
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-49c9701f263c> in <module>
----> 1 get_ipython().run_line_magic('reload_ext', 'RWinOut')

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2312                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2313             with self.builtin_trap:
-> 2314                 result = fn(*args, **kwargs)
   2315             return result
   2316 

<C:\ProgramData\Anaconda3\lib\site-packages\decorator.py:decorator-gen-67> in reload_ext(self, module_str)

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

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\magics\extension.py in reload_ext(self, module_str)
     61         if not module_str:
     62             raise UsageError('Missing module name.')
---> 63         self.shell.extension_manager.reload_extension(module_str)

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\extensions.py in reload_extension(self, module_str)
    128                 self.loaded.add(module_str)
    129         else:
--> 130             self.load_extension(module_str)
    131 
    132     def _call_load_ipython_extension(self, mod):

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\extensions.py in load_extension(self, module_str)
     85                               dir=compress_user(self.ipython_extension_dir)))
     86             mod = sys.modules[module_str]
---> 87             if self._call_load_ipython_extension(mod):
     88                 self.loaded.add(module_str)
     89             else:

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\extensions.py in _call_load_ipython_extension(self, mod)
    132     def _call_load_ipython_extension(self, mod):
    133         if hasattr(mod, 'load_ipython_extension'):
--> 134             mod.load_ipython_extension(self.shell)
    135             return True
    136 

c:\Users\rlysakow\Desktop\00-PayEquity-WIP\RWinOut.py in load_ipython_extension(ip)
     39         # loading magic and libraries
     40         ip.run_line_magic('load_ext', 'rpy2.ipython')
---> 41         ip.run_line_magic('R', 'library(R.utils)')
     42         ip.run_line_magic('config', 'Application.verbose_crash=True')
     43 

C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2312                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2313             with self.builtin_trap:
-> 2314                 result = fn(*args, **kwargs)
   2315             return result
   2316 

<C:\ProgramData\Anaconda3\lib\site-packages\decorator.py:decorator-gen-133> in R(self, line, cell, local_ns)

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

C:\ProgramData\Anaconda3\lib\site-packages\rpy2\ipython\rmagic.py in R(self, line, cell, local_ns)
    755             if result is not ri.NULL:
    756                 with localconverter(converter) as cv:
--> 757                     res = cv.ri2py(result)
    758                 return res
    759 

C:\ProgramData\Anaconda3\lib\functools.py in wrapper(*args, **kw)
    838                             '1 positional argument')
    839 
--> 840         return dispatch(args[0].__class__)(*args, **kw)
    841 
    842     funcname = getattr(func, '__name__', 'singledispatch function')

C:\ProgramData\Anaconda3\lib\site-packages\rpy2\robjects\pandas2ri.py in ri2py_vector(obj)
    137 @ri2py.register(SexpVector)
    138 def ri2py_vector(obj):
--> 139     res = numpy2ri.ri2py(obj)
    140     return res
    141 

C:\ProgramData\Anaconda3\lib\functools.py in wrapper(*args, **kw)
    838                             '1 positional argument')
    839 
--> 840         return dispatch(args[0].__class__)(*args, **kw)
    841 
    842     funcname = getattr(func, '__name__', 'singledispatch function')

C:\ProgramData\Anaconda3\lib\site-packages\rpy2\robjects\numpy2ri.py in ri2py_sexp(obj)
    157 def ri2py_sexp(obj):
    158     if (obj.typeof in _vectortypes) and (obj.typeof != VECSXP):
--> 159         res = numpy.asarray(obj)
    160     else:
    161         res = ro.default_converter.ri2py(obj)

C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    536 
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539 
    540 

ValueError: Buffer for this type not yet supported.
khof312 commented 4 years ago

I am not sure, but it seems like this issue is traced to a clash between an update to numpy and old versions of rpy2.

I uninstalled rpy2 and reinstalled the latest development branch (from Github), which has made some windows fixes (https://github.com/rpy2/rpy2/issues/584).

This seems to have introduced a new host of issues, so I had to update the paths as described here (https://github.com/rpy2/rpy2/issues/619). It is now working for me.