Calling Ruby, Perl or Python from R

June 16, 2010

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.

Search R-bloggers


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)