Module: GalaazUtil

Defined in:
lib/util/exec_ruby.rb,
lib/util/inline_file.rb

Overview

Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation, without fee and without a signed licensing agreement, is hereby granted, provided that the above copyright notice, this paragraph and the following two paragraphs appear in all copies, modifications, and distributions.

IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED “AS IS”. RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

Author:

  • Rodrigo Botafogo

Class Method Summary collapse

Class Method Details

.exec_ruby(options) ⇒ R::List


Executes the ruby code with the given options. The options are:

  • options.code: the ruby code

  • options[]: evaluate if true

  • options.echo: if true, show the source code of the chunk in the output

  • options.message: if true, show error message if any exception in the ruby code

  • options.warning: if true, show stack trace from the ruby code

  • options.include: if true, include the code output

Note that we need to access the ‘eval’ element of the list by indexing as options[], this is because eval is a Ruby function and doing options.eval will call the eval method on options, which is not what we want


Parameters:

  • options (R::List)

    An R list of options

Returns:

  • (R::List)

    an R list with everything that needs to be outputed.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/util/exec_ruby.rb', line 78

def self.exec_ruby(options)

  # RubyChunk.init
  
  # read the chunk code
  code = R.paste(options.code, collapse: "\n") >> 0
  
  # the output should be a list with the proper structure to pass to
  # function engine_output.  We first add the souce code from the block to
  # the list
  out_list = R.list(R.structure(R.list(src: code), class: 'source')) if
    options.echo >> 0

  begin

    # set $stdout to a new StringIO object so that everything that is
    # output from instance_eval is captured and can be sent to the
    # report
    $stdout = StringIO.new

    # Execute the Ruby code in the scope of class RubyChunk. This is done
    # so that instance variables created in one chunk can be used again on
    # another chunk
    # RChunk.instance_eval(code) if (options[["eval"]] >> 0)
    eval(code, RCbinding, __FILE__, __LINE__ + 1) if (options[["eval"]] >> 0)
    
    # add the returned value to the list
    # this should have captured everything in the evaluation code
    # it is not working since at least RC10.
    out = $stdout.string
    
    out_list = R.c(out_list, out)
    
  rescue StandardError => e

    # print the error message
    if (options.message >> 0)
      message = R.list(R.structure(R.list(message: e.message), class: 'message'))
      out_list = R.c(out_list, message)
    end

    # Print the backtrace of the error message
    if (options.warning >> 0)
      bt = ""
      e.backtrace.each { |line| bt << line + "\n"}
      warning = R.list(R.structure(R.list(message: bt), class: 'message'))
      out_list = R.c(out_list, warning)
    end

  rescue SyntaxError => e
    STDERR.puts "A syntax error occured in ruby block '#{options.label >> 0}'"
    raise SyntaxError.new(e)
    
  ensure
    # return $stdout to standard output
    $stdout = STDOUT
  end
  
  (options.include >> 0)? out_list : R.list
  
end

.inline_file(filename, relative, pwd = Dir.pwd) ⇒ Object

Opens the given filename for reading and returns the file content so that it can be printed as a code chunk in rmarkdown

Parameters:

  • filename (String)

    the name of the file to be found. If the file has no extension then ‘.rb’ is assumed

  • relative (String)

    containing either ‘require_relative ’ or ‘require ’. In the first case looks on the current directory, on the latter, searches the $LOAD_PATH

  • pwd (String) (defaults to: Dir.pwd)

    Directory to look at when ‘require_relative ’, by default, its the pwd directory



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/util/inline_file.rb', line 37

def self.inline_file(filename, relative, pwd = Dir.pwd)

  filename << ".rb" if File.extname(filename) == ""
  file = "#{pwd}/#{filename}"

  if (relative == false)
    $LOAD_PATH.each do |path|
      begin
        files = Dir.entries(path)
      rescue Errno::ENOENT
        next
      end

      if files != nil
        file = "#{path}/#{filename}"
        # break if File.exist?(file)
        break if (R.file__exists(file) >> 0)
      end
    end
  end

  # There is a bug(?) in > RC15 that when the bellow command
  # is called, there is a call to R Polyglot eval passing to_i 
  # if File.exist?(file)
  if (R.file__exists(file) >> 0)
    code = ""
    File.open(file, "r") do |fileObj|
      while (line = fileObj.gets)
        code << line
      end
    end
    
  else
    raise Errno::ENOENT, "file #{filename} not found in #{$LOAD_PATH}"
  end
  
  code
end