How do I solve the parsing error? when using rpy2

Viewed 1717

Testing some very simple rpy2 sample code but running into some error messages very often in the last cell using ggplot. Running some other R code or libraries seem to work so I have no idea why I keep seeing this message. Tested the code on Google Colab and also worked. Maybe it's the rpy2 setup or ipython problem?

%load_ext rpy2.ipython
import pandas as pd
import numpy as np
df = pd.DataFrame({
    'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1]
})
df.transpose()

%%R -i df
head(df)

%%R -i df
library(ggplot2)
ggplot(df, aes(x=reorder(cups_of_coffee,productivity), y=productivity)) + geom_col() + coord_flip()

Here is the error message:

RParsingError: Parsing status not OK - PARSING_STATUS.PARSE_ERROR
---------------------------------------------------------------------------
RParsingError                             Traceback (most recent call last)
<ipython-input-6-dcdd226012b5> in <module>
----> 1 get_ipython().run_cell_magic('R', '-w 800 --type=cairo', '\r\ncat("Running an R code cell.\\n")\r\n\r\nlibrary(ggplot2)\r\n\r\np <- ggplot(dataf) +\r\n     aes(x=longd,\r\n         y=latd,\r\n         color=population_total,\r\n         size=area_total_km2) +\r\n     geom_point(alpha=0.5) +\r\n     scale_x_continuous(\'Longitude\') +\r\n     scale_y_continuous(\'Latitude\') +\r\n     scale_size(range=c(1, 18)) +\r\n     ggtitle(\'California Cities: Area and Population\') +\r\n     theme_light(base_size=16)\r\nprint(p)\n')

C:\Software\anaconda\envs\work\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2397             with self.builtin_trap:
   2398                 args = (magic_arg_s, cell)
-> 2399                 result = fn(*args, **kwargs)
   2400             return result
   2401 

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

C:\Software\anaconda\envs\work\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:\Software\anaconda\envs\work\lib\site-packages\rpy2\ipython\rmagic.py in R(self, line, cell, local_ns)
    761                     return_output = False
    762             else:
--> 763                 text_result, result, visible = self.eval(code)
    764                 text_output += text_result
    765                 if visible:

C:\Software\anaconda\envs\work\lib\site-packages\rpy2\ipython\rmagic.py in eval(self, code)
    266             try:
    267                 # Need the newline in case the last line in code is a comment.
--> 268                 value, visible = ro.r("withVisible({%s\n})" % code)
    269             except (ri.embedded.RRuntimeError, ValueError) as exception:
    270                 # Otherwise next return seems to have copy of error.

C:\Software\anaconda\envs\work\lib\site-packages\rpy2\robjects\__init__.py in __call__(self, string)
    435 
    436     def __call__(self, string):
--> 437         p = rinterface.parse(string)
    438         res = self.eval(p)
    439         return conversion.rpy2py(res)

C:\Software\anaconda\envs\work\lib\site-packages\rpy2\rinterface_lib\conversion.py in _(*args, **kwargs)
     43 def _cdata_res_to_rinterface(function):
     44     def _(*args, **kwargs):
---> 45         cdata = function(*args, **kwargs)
     46         # TODO: test cdata is of the expected CType
     47         return _cdata_to_rinterface(cdata)

C:\Software\anaconda\envs\work\lib\site-packages\rpy2\rinterface.py in parse(text, num)
    101     robj = StrSexpVector([text])
    102     with memorymanagement.rmemory() as rmemory:
--> 103         res = _rinterface._parse(robj.__sexp__._cdata, num, rmemory)
    104     return res
    105 

C:\Software\anaconda\envs\work\lib\site-packages\rpy2\rinterface_lib\_rinterface_capi.py in _parse(cdata, num, rmemory)
    650     # PARSE_EOF
    651     if status[0] != openrlib.rlib.PARSE_OK:
--> 652         raise RParsingError('Parsing status not OK',
    653                             status=PARSING_STATUS(status[0]))
    654     return res

RParsingError: Parsing status not OK - PARSING_STATUS.PARSE_ERROR
1 Answers

A work around is something like this:

get_ipython().run_cell_magic(
"R",
"-i df",
'''
library(ggplot2)
ggplot(df, aes(x=reorder(cups_of_coffee,productivity), y=productivity)) +
geom_col() +
coord_flip()
'''
)
Related