在python中使用rpy2读取csv文件时出错

时间:2015-07-27 18:53:45

标签: python rpy2

我似乎无法运行以下代码并收到错误:

rpy2.rinterface.RRuntimeError: Error in paste(r.base_dir, r.inp_file, ".csv", sep = "") : 
  object 'r.base_dir' not found

即使我用base_dir替换r.base_dir,我也会得到同样的错误。代码实际上是使用rpy2

读取csv文件
from rpy2.robjects.packages import importr
from rpy2.robjects import r
import rpy2.robjects.numpy2ri as rpyn

r.base_dir = '/Users/r/Documents/Projects/GLM/Visualize/'
r.inp_file = 'Cns'
r.out_file = 'Main.png'
r.inp_mat  = r("read.table(paste(r.base_dir,r.inp_file,'.csv',sep=''), header=T, row.names=1, sep=',')")

1 个答案:

答案 0 :(得分:1)

在R命名空间中,Python符号并不神奇可见。

在此期间,您可以考虑使用Python参数调用R函数。 例如:

from rpy2.robjects.packages import importr
from rpy2.robjects import r
import rpy2.robjects.numpy2ri as rpyn
import os

utils = importr('utils')
base_dir = '/Users/r/Documents/Projects/GLM/Visualize/'
inp_file = 'Cns'
out_file = 'Main.png'
inp_mat  = utils.read_csv(os.path.join(r.base_dir,
                                       r.inp_file +'.csv'),
                          header=True,
                          row_names=1,
                          sep=',')