13

Is there a way to call functions defined in a file say myfunc.r

---------------myfunc.r --------------
myfunc = function(){
  return(c(1,2,3,4,5,6,7,8,9,10))
}

getname = function(){
  return("chart title")
}

---- Python 
   How to call getname() here ?

Any help would be greatly appreciated ?

user2171582
  • 131
  • 1
  • 1
  • 6

3 Answers3

21

There are features in rpy2 that should help making this cleaner than dumping objects into the global workspace.

from rpy2.robjects.packages import STAP
# if rpy2 < 2.6.1 do:
# from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage
# STAP = SignatureTranslatedAnonymousPackage
with open('myfunc.r', 'r') as f:
    string = f.read()
myfunc = STAP(string, "myfunc")

The objects in the R file can now be accessed with myfunc.myfunc and myfunc.getname.

Check the documention about importing arbitrary R code as a package.

lgautier
  • 11,363
  • 29
  • 42
  • `SignatureTranslatedAnonymousPackage` The name is a little bit long I think..for R and python develoopers:) – agstudy Mar 15 '13 at 14:12
  • Yes, may be on the Java extravaganza end of the naming spectrum but still under 140 characters ;-). A shorter class name while keeping it clear would mean introducing a subpackage. Suggestions for names are also welcome. – lgautier Mar 15 '13 at 14:25
  • Of course I came from the .net world and I know what you mean. rpy2 is a great work. I appreciate. +1.. long name but clean solution! I think you can just add `import SignatureTranslatedAnonymousPackage as STAP` for example... – agstudy Mar 15 '13 at 14:28
  • 1
    @agstudy: I edited the code snippet; now the real class name is written only once – lgautier Mar 15 '13 at 21:15
  • string = ''.join(f.readlines()) will give errors. What you should do for R version 3.2.0 (at the time or writing) is like this string = f.read().replace('\n', '') – momokjaaaaa Jun 09 '15 at 09:21
  • @momokjaaaaa Yes. I fixed the answer. Thanks. – lgautier Jul 07 '15 at 19:10
  • I tried this except that my function is called `calc_num` so I ran `myfunc=STAP(string, "calc_num")` and I get a `rpy2.rinterface.RParsingError` – KillerSnail Oct 10 '16 at 05:58
  • @KillerSnail : an `RParsingError` indicates that when parsing the R source code contained you the Python string `string` R stopped with a parsing error. In other words, whatever is in `string` did not seem valid code to R itself. – lgautier Oct 10 '16 at 16:02
10

You can do something like this ( python code here)

import rpy2.robjects as robjects
robjects.r('''
       source('myfunc.r')
''')

 r_getname = robjects.globalenv['getname']

then you call it

r_getname()
agstudy
  • 119,832
  • 17
  • 199
  • 261
2

I'd suggest to use what user3282437 suggested here:

import rpy2.robjects as robjects
r_source = robjects.r['source']
r_source('/path_to_file/myfunc.R')
r_getname = robjects.globalenv['getname']

I'm not sure that it's a global issue, but on my Windows machine direct call like agstudy advised:

import rpy2.robjects as robjects
robjects.r('source("some_file.R")')

leads to python crash.

Community
  • 1
  • 1
Dmitry Deryabin
  • 1,518
  • 2
  • 14
  • 27