Using R in Ruby

January 10, 2012
By

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

Integrating R into more traditional programming languages can be incredibly rewarding due to R’s powerful built-in statistical tools, but it can also be extremely frustrating at times. Thankfully, like much else to do with Ruby, integrating R and Ruby is quite a simple process. To begin, install the gem rinruby and require it in your script.

gem install rinruby

require 'rubygems'
require 'rinruby'

There is no further installation or configuration required. To evaluate an R expression, use the R.eval command.

 R.eval "test=1*1"

To get a value from R, use the R.pull command.

 test=R.pull "test" 

If you are running large code blocks, it is easy to use the R.eval command with the R source function. This will evaluate the R instructions in a given text file.

R.eval "source('C:/randomscript.R')"

If you are receiving the below error when attempting to load packages, your R library path is not properly set in your rinruby created instance of R.

Error in library(x) : there is no package called 'x'

You can correct this using the R .libPaths() function.

R.eval('.libPaths("C:/Users/R library path")')

To find out what your R library paths are, you should use the Sys.getenv function in R.

Sys.getenv(c("R_LIBS", "R_LIBS_USER"))

You should make sure that you set your library paths in the R instance created by rinruby to the same paths that appear when you run the Sys.getenv command in your R GUI. A list of other useful environment variables can be found here. Another useful function to use in conjunction with rinruby is the setwd command.

R.eval('setwd("C:/Users/Documents")')

If you replace the file path above with your R working directory path, rinruby will save files in the right spot.

Notes:

When typing file paths into Ruby for use in R, one should always use the forward slash(/), or four backslashes(\\\\), as single and double backslashes will not escape properly.

Note that using the R.pull command with multiple R instructions(or using it with the source() function) will often cause long runtimes, or will not complete. Using the R.eval command to evaluate scripts, and then using the R.pull command to pull variables out after the script has finished executing will solve this issue.

You can find more information on this at the rinruby site.

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

R-bloggers.com 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...

Comments are closed.

Sponsors

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)