如何在R代码中从python调用变量

时间:2019-02-12 17:35:19

标签: python r pandas rpy2 read.csv

我正在使用rpy2(在python中使用r代码)在python环境中工作。

我试图在dong read.csv时在r中传递python变量。 python变量包含一个数组,如下所示:

var是python中的字符串

var =  CAM_4209

我正在r中尝试使用的命令:

 data1 = robjects.r('read.csv("/Users/Downloads/SITES/awFC_{}.txt".format(var), check.names=FALSE)')

但是,我不断遇到运行时错误。我不确定为什么会这样。

我该如何克服?任何帮助将不胜感激

*************更新********************* 这是运行时错误:

    /Users/anaconda/lib/python3.6/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"),  : 
  <text>:1:74: unexpected symbol
1: read.csv("/Users/Downloads/SITES/awFC_{}.txt".format
                                                                             ^

  warnings.warn(x, RRuntimeWarning)
---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-24-4ae8b8e7e4b9> in <module>()
----> 1 data1 = robjects.r('read.csv("/Users/Downloads/SITES/awFC_{}.txt".format(var), check.names=FALSE)')

/Users/anaconda/lib/python3.6/site-packages/rpy2/robjects/__init__.py in __call__(self, string)
    349 
    350     def __call__(self, string):
--> 351         p = _rparse(text=StrSexpVector((string,)))
    352         res = self.eval(p)
    353         return conversion.ri2py(res)

RRuntimeError: Error in (function (file = "", n = NULL, text = NULL, prompt = "?", keep.source = getOption("keep.source"),  : 
  <text>:1:74: unexpected symbol
1: read.csv("/Users/Downloads/SITES/awFC_{}.txt".format

0 个答案:

没有答案