Error reading csv file using rpy2 in python

I can't seem to run the following code and get the error:

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

      

I get the same error even though I replace r.base_dir with base_dir. The code is essentially read into a csv file using rpy2

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=',')")

      

+3


source to share


1 answer


Python symbols are not magically visible in the R namespace.

While you might consider R functions with Python arguments. For example here:



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=',')

      

+1


source







All Articles