-1

I have this function in R and I want to call it in python. I am planning to assign the data which has been manipulated in python to the R function to do further analysis on it. I also need to have the data back to python after the function did the analysis. I know about rpy2 and I used this command

robjects.r('''source('mycode.R')''')
r_test_function = robjects.globalenv['myFunction']

I get this error:

/usr/local/lib/python2.7/dist-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: Error in file(filename, "r", encoding = encoding) :
cannot open the connection

warnings.warn(x, RRuntimeWarning) /usr/local/lib/python2.7/dist-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: In addition: warnings.warn(x, RRuntimeWarning) /usr/local/lib/python2.7/dist-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: Warning message:

warnings.warn(x, RRuntimeWarning) /usr/local/lib/python2.7/dist-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: In file(filename, "r", encoding = encoding) :
warnings.warn(x, RRuntimeWarning) /usr/local/lib/python2.7/dist-packages/rpy2/rinterface/init.py:186: RRuntimeWarning:

warnings.warn(x, RRuntimeWarning) /usr/local/lib/python2.7/dist-packages/rpy2/rinterface/init.py:186: RRuntimeWarning: cannot open file 'mycode.R': No such file or directory

warnings.warn(x, RRuntimeWarning) Info | ____________________________________________________________________________________________________| Error | Termination with exception status| Error

I used the sys.path.append('thePath') for the directory containing the function. Can someone please help me about this? I have searched a lot about the topic but I could not find anything in my case. Also is what I explained above practical with rpy2?

Thank you,

K S
  • 3
  • 6
  • Does the error happen on your first line or your second line? What's in the file you are sourceing? What's `bfast.R`? Do you get errors doing basic rpy stuff? – Spacedman Aug 30 '17 at 08:18
  • I edited the error, the bfast.R is the same as mycode.R. There are multiple R functions in the file I am referring to. Yes, I tried other simple functions, still the same error. – K S Aug 30 '17 at 08:37
  • **Does the error happen on your first line or your second line?** What folder did Python start from? Where is the R file in your file system? What do you get if you run `getwd()` from Rpy2? That's R's working directory. What about `file.exists('mycode.R')`? That tells you if the file exists. – Spacedman Aug 30 '17 at 14:31

1 Answers1

0

Is your concern is getting the full directory path? I am currently working on Python 3.6 to browse through all files in directory and list them. Since you want to work with just one specific file, you could try the following:

import os

file = os.path.abspath(''F:\Training.xlsx'') so your file variable witll look like 'F:\Training.xlsx'

You could simply use the variable file instead of entering the whole directory path.

Hope it helps.

Sagar Dawda
  • 1,126
  • 9
  • 17
  • Hey Sagar, thank you for your reply. I am new to python. I tried your suggestion. Still getting the same error. – K S Aug 30 '17 at 08:12