module GalaazUtil
@author Rodrigo Botafogo
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.
Public Class Methods
# File lib/util/exec_ruby.rb, line 77 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
¶ ↑
Opens the given filename for reading and returns the file content so that it can be printed as a code chunk in rmarkdown @param filename [String] the name of the file to be found. If the file has no extension then '.rb' is assumed @param relative [String] containing either 'require_relative ' or 'require '. In the first case looks on the current directory, on the latter, searches the $LOAD_PATH @param pwd [String] Directory to look at when 'require_relative ', by default, its the pwd directory
¶ ↑
# File lib/util/inline_file.rb, line 36 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