Calling Ruby, Perl or Python from R

[This article was first published on R-Chart, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you want to interact with other programming languages from R, there are various packages and bindings available. These packages provide a pretty high degree of integration between the langages and allow you to pass objects back and forth seemlessly. The downside is that you need to have the time, security rights and correct installation sequences available to complete the compilation and installation.
A quick and easy way to return a string from an external program or scripting langage is to call the system function. As you can see from the xamples, you must set intern=TRUE to pass the value back into the R environment. In addition, the value being passed into R is being redirected from STDOUT, so you have to use an appropriate command in your script to effectively return the result.
x=system(‘ruby -e “puts 1+1″‘, intern=TRUE)
x=system(‘perl -e “print 2 + 4″‘, intern=TRUE)
x=system(‘C:\jython2.2.1\jython -c “print 1+3″‘, intern=TRUE)
Obviously not a great approach for a full blown application, but a good “quick and dirty” solution to pass a bit of data between programming language environments.

Oh and by the way you can execute a source file (don’t be confused by the fact that all of the examples above are simple interactive evaluation of expressions). Just make sure that the last line is outputting the value’s you want to assign to your variable
x=system(‘ruby calc.rb’, intern=TRUE)

To leave a comment for the author, please follow the link and comment on their blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)