ipython - 如何解决解析错误?使用 rpy2 时
问题描述
测试一些非常简单的 rpy2 示例代码,但使用 ggplot 在最后一个单元格中经常遇到一些错误消息。运行其他一些 R 代码或库似乎可以工作,所以我不知道为什么我一直看到这条消息。在 Google Colab 上测试了代码并且也可以工作。也许是 rpy2 设置或 ipython 问题?
%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()
这是错误消息:
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
解决方案
解决方法是这样的:
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()
'''
)
推荐阅读
- javascript - 在 android 中监听 JavaScript 数据
- c# - 更改调试字体颜色 - Serilog C# .NET Core 3.1 Jetbrains Rider Mac OSX
- pyautogui - pyautogui 位置总是返回相同的值
- javascript - 如何使用 webpack 缩小/混淆反应属性值(如 aria)?
- java - 如何在android中查找json日期列表是否一个月
- r - Python中时间序列的时间分解
- angular - GitHub 无法从 npm repo 中提取
- apache-nifi - 使用具有不同 URL 的 NiFi 循环 API
- scala - 如何从 spark 应用程序中知道 spark 用户的组名
- ios - 在设置应用程序中看不到 MyAppNotificationSettings 选项