class R
R
server wrapper
The purpose of this class is to allow us to easily send R
commands to the R
server, process and return the results.
R.exec
is the key entry point
Public Class Methods
exec(args={})
click to toggle source
Execute a block of R
commands, using optional syntactic sugar, returning the results
Creates a new connection to the RServe after every connection.
Example:
require 'r'
R.exec
do
a 'x', 2 a 'y', 4 ve 'x = -x' xy = e "x + y" xdivy = e "x / y" [xy, xdivy]
end
> [2, -0.5]¶ ↑
# File lib/r.rb, line 29 def self.exec args={}, &block raise "Expected hash, keys will be defined as R variable names, example: {xvals: [1,2,3], yvals: [3,2,1]}" unless args.is_a?(Hash) begin conn = Rserve::Connection.new dsl = R::RserveConnectionDsl.new conn, args res = dsl.instance_eval &block dsl.clear # since connection is recycled, make an attempt to clean up all defined vars return res rescue => e conn = dsl = nil # ensure cleanup raise e end end