简体   繁体   English

打开外部代码时Rpy2解析错误

[英]Rpy2 parsing error while opening external code

I'm having problems in running external R script from python using rpy2 library.我在使用 rpy2 库从 python 运行外部 R 脚本时遇到问题。 The setting is the same of this question but I don't feel it solved my issue. 这个问题的设置是一样的,但我觉得它没有解决我的问题。 By summarizing the problem, I'm able to run an R function by hard typing it but not but calling the script that contains it since I get a parsing error.通过总结问题,我可以通过硬输入来运行 R function 但不是调用包含它的脚本,因为我遇到了解析错误。

More specifically, I have an R script called 'produce_CI.r' which contains the following code更具体地说,我有一个名为“produce_CI.r”的 R 脚本,其中包含以下代码

library(readr)
library(bnlearn)

perform_CI <- function(c1, c2, s, dataset, test_type){
  print(c(c1,c2,s))
  if (!s[1]=="()"){
    t <- ci.test(c1, c2, c(s), data = dataset, test = test_type)
  }else{
    t <- ci.test(c1, c2, data = dataset, test = test_type, debug = TRUE)
  }
  print(t)
  return <- t
}

and I try to call it from python using this code我尝试使用此代码从 python 调用它

import rpy2.robjects as robjects
import os

robjects.r("""source(%s)""" % os.path.join(os.getcwd(), 'produce_CI.r'))

which gives me the following parsing error这给了我以下解析错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "path/ci_venv/lib/python3.8/site-packages/rpy2/robjects/__init__.py", line 437, in __call__
    p = rinterface.parse(string)
  File "path/ci_venv/lib/python3.8/site-packages/rpy2/rinterface_lib/conversion.py", line 45, in _
    cdata = function(*args, **kwargs)
  File "path/ci_venv/lib/python3.8/site-packages/rpy2/rinterface.py", line 103, in parse
    res = _rinterface._parse(robj.__sexp__._cdata, num, rmemory)
  File "path/ci_venv/lib/python3.8/site-packages/rpy2/rinterface_lib/_rinterface_capi.py", line 652, in _parse
    raise RParsingError('Parsing status not OK',
rpy2.rinterface_lib._rinterface_capi.RParsingError: Parsing status not OK - PARSING_STATUS.PARSE_ERROR

The only way I'm able to make the code run is by hard-coding it using a string in python我能够使代码运行的唯一方法是使用 python 中的字符串对其进行硬编码

from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage as STAP

r_code = '''library(readr)
library(bnlearn)

perform_CI <- function(c1, c2, s, dataset, test_type){
  print(c(c1,c2,s))
  if (!s[1]=="()"){
    t <- ci.test(c1, c2, c(s), data = dataset, test = test_type)
  }else{
    t <- ci.test(c1, c2, data = dataset, test = test_type, debug = TRUE)
  }
  print(t)
  return <- t
}
'''

testy = STAP(r_code, "testy")
# ====
# removed code to initialize t,x,z_vec, df_r and r_method
# ====
testy.perform_CI(t,x,z_vec, df_r, r_method)

I'm not satisfied by this solution since I'd like to call directly the code in the R script since then I don't have duplicate code.我对这个解决方案不满意,因为我想直接调用 R 脚本中的代码,因为我没有重复的代码。 I'm therefore searching for a solution to this parsing error, not a way to just "make the code work".因此,我正在寻找解决此解析错误的方法,而不是仅仅“使代码工作”的方法。

I feel like an issue may be that I'm writing the code on MacOS and running it on an Ubuntu machine therefore there may be problems with line endings.我觉得一个问题可能是我在 MacOS 上编写代码并在 Ubuntu 机器上运行它,因此行尾可能存在问题。 Therefore I tried to read the R code with python as it was a string and (obviously) I see lot a '\n' everytime there's a new line.因此,我尝试使用 python 读取 R 代码,因为它是一个字符串,并且(显然)每次有新行时我都会看到很多“\ n”。

I'm running this code with python 3.8 and rpy2 version 3.4.5我正在使用 python 3.8 和 rpy2 版本 3.4.5 运行此代码

The line线

robjects.r("""source(%s)""" % os.path.join(os.getcwd(), 'produce_CI.r'))

will produce a string like source(/path/to/my/produce_CI.r) to be evaluated as R code.将生成一个类似于source(/path/to/my/produce_CI.r)的字符串,将其评估为 R 代码。 Quotes are missing.缺少引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM