%R magic input argument not working properly in Jupyter notebook

Viewed 2076

When I pass a pandas DataFrame as an input argument to magic line I get an error.

%R -i df
ValueError: Buffer for this type not yet supported.

Pasted below is the complete code where I pass a pandas df as an input to a magic cell.

I am running my code on Windows 10. I have the 2019.10 Windows 64 bit version of Anaconda. The packages have the following versions - rpy2 - 2.9.4, python - 3.6.9, jupyter - 1.0.0, pandas - 0.25.2.

I have recently re-installed anaconda on my computer. I have the same packages installed, however different versions from the past. I do not what my previous version of the packages where. My code was working prior to the reinstall. The code below is a sample but

After I ran the magic cell I get the error.

%%R -i df
df$index <- as.numeric(row.names(df))
library(ggplot2)
ggplot(data = df, aes(x = index)) + 
    geom_line(aes(y = col1, color = 'col1'), size = 0.5) + 
    geom_line(aes(y = col2, color = 'col2'), size = 0.5)

ValueError: Buffer for this type not yet supported.

Now I go back to the same cell and delete the input argument and re-run it again and I get the desired output without any error.

%%R
df$index <- as.numeric(row.names(df))
library(ggplot2)
ggplot(data = df, aes(x = index)) + 
    geom_line(aes(y = col1, color = 'col1'), size = 0.5) + 
    geom_line(aes(y = col2, color = 'col2'), size = 0.5)

Below is the complete code.

--> import pandas as pd
df = pd.DataFrame({'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8], 'col3': ['a','b','a','b']})
df

-- >%load_ext rpy2.ipython

--> %%R -i df
df$index <- as.numeric(row.names(df))
library(ggplot2)
ggplot(data = df, aes(x = index)) + 
    geom_line(aes(y = col1, color = 'col1'), size = 0.5) + 
    geom_line(aes(y = col2, color = 'col2'), size = 0.5)

I expected R magic to work in order for me to plot my data using ggplot2 as it had prior to my reinstall of anaconda.

Below is the error.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-57-4a5dfe23f86a> in <module>
----> 1 get_ipython().run_cell_magic('R', '-i df', "df$index <- as.numeric(row.names(df))\nlibrary(ggplot2)\nggplot(data = df, aes(x = index)) + \n    geom_line(aes(y = col1, color = 'col1'), size = 0.5) + \n    geom_line(aes(y = col2, color = 'col2'), size = 0.5)\n")

C:\Anaconda3\envs\keras\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2357             with self.builtin_trap:
   2358                 args = (magic_arg_s, cell)
-> 2359                 result = fn(*args, **kwargs)
   2360             return result
   2361 

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

C:\Anaconda3\envs\keras\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:\Anaconda3\envs\keras\lib\site-packages\rpy2\ipython\rmagic.py in R(self, line, cell, local_ns)
    688                         raise NameError("name '%s' is not defined" % input)
    689                 with localconverter(converter) as cv:
--> 690                     ro.r.assign(input, val)
    691 
    692         tmpd = self.setup_graphics(args)

C:\Anaconda3\envs\keras\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
    176                 v = kwargs.pop(k)
    177                 kwargs[r_k] = v
--> 178         return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
    179 
    180 pattern_link = re.compile(r'\\link\{(.+?)\}')

C:\Anaconda3\envs\keras\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)
    108         return res
    109 

C:\Anaconda3\envs\keras\lib\functools.py in wrapper(*args, **kw)
    805                             '1 positional argument')
    806 
--> 807         return dispatch(args[0].__class__)(*args, **kw)
    808 
    809     funcname = getattr(func, '__name__', 'singledispatch function')

C:\Anaconda3\envs\keras\lib\site-packages\rpy2\ipython\rmagic.py in _(obj)
    147     if 'data.frame' in obj.rclass:
    148         # request to turn it to a pandas DataFrame
--> 149         res = converter.ri2py(obj)
    150     else:
    151         res = ro.sexpvector_to_ro(obj)

C:\Anaconda3\envs\keras\lib\functools.py in wrapper(*args, **kw)
    805                             '1 positional argument')
    806 
--> 807         return dispatch(args[0].__class__)(*args, **kw)
    808 
    809     funcname = getattr(func, '__name__', 'singledispatch function')

C:\Anaconda3\envs\keras\lib\site-packages\rpy2\robjects\pandas2ri.py in ri2py_listvector(obj)
    181 def ri2py_listvector(obj):
    182     if 'data.frame' in obj.rclass:
--> 183         res = ri2py(DataFrame(obj))
    184     else:
    185         res = numpy2ri.ri2py(obj)

C:\Anaconda3\envs\keras\lib\functools.py in wrapper(*args, **kw)
    805                             '1 positional argument')
    806 
--> 807         return dispatch(args[0].__class__)(*args, **kw)
    808 
    809     funcname = getattr(func, '__name__', 'singledispatch function')

C:\Anaconda3\envs\keras\lib\site-packages\rpy2\robjects\pandas2ri.py in ri2py_dataframe(obj)
    188 @ri2py.register(DataFrame)
    189 def ri2py_dataframe(obj):
--> 190     items = tuple((k, ri2py(v)) for k, v in obj.items())
    191     res = PandasDataFrame.from_items(items)
    192     return res

C:\Anaconda3\envs\keras\lib\site-packages\rpy2\robjects\pandas2ri.py in <genexpr>(.0)
    188 @ri2py.register(DataFrame)
    189 def ri2py_dataframe(obj):
--> 190     items = tuple((k, ri2py(v)) for k, v in obj.items())
    191     res = PandasDataFrame.from_items(items)
    192     return res

C:\Anaconda3\envs\keras\lib\functools.py in wrapper(*args, **kw)
    805                             '1 positional argument')
    806 
--> 807         return dispatch(args[0].__class__)(*args, **kw)
    808 
    809     funcname = getattr(func, '__name__', 'singledispatch function')

C:\Anaconda3\envs\keras\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:\Anaconda3\envs\keras\lib\functools.py in wrapper(*args, **kw)
    805                             '1 positional argument')
    806 
--> 807         return dispatch(args[0].__class__)(*args, **kw)
    808 
    809     funcname = getattr(func, '__name__', 'singledispatch function')

C:\Anaconda3\envs\keras\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:\Anaconda3\envs\keras\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.

Jupyter notebook

1 Answers

I just ran into this problem. This seems to be an error due to a numpy update. Upon investigating, the error is coming from passing strings in dataframes to R. The easiest way to fix this is to simply update rpy2 to version 3.1.

Remove rpy2 from your system

conda remove rpy2

Now reinstall

conda install -c conda-forge rpy2
Related