Class: Flukso::RRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/flukso/R.rb

Instance Method Summary collapse

Constructor Details

#initialize(workingdir) ⇒ RRunner

Returns a new instance of RRunner.



28
29
30
31
32
# File 'lib/flukso/R.rb', line 28

def initialize(workingdir)
  # TODO: Check whether R command is available.
  @workingdir = workingdir
  puts "Using working directory #{@workingdir}" if $verbose
end

Instance Method Details

#createClosingObject

returns a closing string - close file, terminate R



45
46
47
48
49
50
# File 'lib/flukso/R.rb', line 45

def createClosing()
  close=<<-END_OF_CMD
  q()
END_OF_CMD
  return close
end

#createPreamble(outfilename) ⇒ Object

returns a preamble string: which file to use etc.



35
36
37
38
39
40
41
42
# File 'lib/flukso/R.rb', line 35

def createPreamble(outfilename)
  absOutFilename = File.expand_path(File.join(@workingdir, outfilename))
  cmd= <<-END_OF_CMD
  pdf("#{absOutFilename}");
END_OF_CMD
  puts "using filename #{absOutFilename} for output." if $verbose
  return cmd
end

#execute(outfilename, commands) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flukso/R.rb', line 68

def execute(outfilename, commands)
  puts commands.class
  if commands.class != Array
    raise "Please provide an array of commands to the execute call."
  end
  cmdString=""
  commands.each{|command|
    cmdString << command.cmd();
    cmdString << "# command separator\n"
  }
  executeCommandString(outfilename, cmdString);
end

#executeCommandString(outfilename, commands) ⇒ Object

Executes the given command. Expects a string that contains the commands to execute.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/flukso/R.rb', line 54

def executeCommandString(outfilename, commands) 
  cmdSet = createPreamble(outfilename)
  cmdSet << commands << createClosing
  # The Tempfile will get deleted automagically when ruby terminates.
  cmdfile=Tempfile.new("r-cmd", @workingdir)
  cmdfile.print(cmdSet)
  cmdfile.close()
  puts "executing commands:\n#{cmdSet}" if $verbose
  commandline="#{R_CMD} #{cmdfile.path}"
  puts "using commandline: #{commandline}" if $verbose
  stdout = %x[#{commandline}]
  puts "R (Exitcode: #{$?}) said: #{stdout}" if $verbose
end