Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating R package for python source code

Tags:

python

r

I have written a small program in python and I want to create an R interface/R package to run this program.

I see that Rpy has a lot of functions to call R from python and rPython can call python functions from R. I also see some tutorials about how to create an R package with C++ source code. However, it isn't clear to me how to create an R package from python source code.

Since everything is written in python, my goal is to create a R package that can call the python program like:

pythonProgram(input1, input2, input3)

Any suggestions of tutorials/ways to get started would be highly appreciated!

like image 290
user2154589 Avatar asked Aug 27 '26 16:08

user2154589


1 Answers

If your python script doesn't need to return anything to R, you can use base R, and it can be as simple as something like this:

pythonProgram <- function(input1, input2, input3) {

  command <- paste("pyprog.py -i1",input1,"-i2",input2,"-i3",input3)
  shell(command)

}

Have a look at the help for shell (and system too). Both allow you to run arbitrary system commands.

If the python script needs to return data to R, you can either get your python script to write to a log file, and then have R read it. Or you can get your python script to write to stdout and have shell or system read that output.

like image 101
Tommy O'Dell Avatar answered Aug 30 '26 06:08

Tommy O'Dell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!