Class: AlexR

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

Class Method Summary collapse

Class Method Details

.execute(output_file, input_file_path = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/alex_r.rb', line 4

def self.execute(output_file, input_file_path=nil)
  r_executable = "Rscript -"
  r_executable = r_executable.insert(0, "cd #{File.dirname(input_file_path)} && ") if input_file_path

  r_output = ""
  r_error = ""
  status = Open4::popen4(r_executable) do |pid, stdin, stdout, stderr|
    stdin << "pdf('#{output_file}')\n"
    if input_file_path
      File.open(input_file_path) do |file|
        until file.eof?
          stdin << "#{file.readline}\n"
        end
      end
    else
      yield(stdin) if block_given?
    end
    stdin.close
    r_output = stdout.read
    r_error = stderr.read
  end
  
  raise r_error unless status.success?
  r_output
end