user308827
user308827

Reputation: 22011

Error reading csv file using rpy2 in python

I cannot seem to be able 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 if I replace r.base_dir by base_dir. The code is essentially reading in 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=',')")

Upvotes: 3

Views: 366

Answers (1)

lgautier
lgautier

Reputation: 11565

The Python symbols are not magically visible in the R namespace.

While at it you may consider calling 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=',')

Upvotes: 1

Related Questions