Class: Daru::IO::Exporters::RData

Inherits:
RDS
  • Object
show all
Defined in:
lib/daru/io/exporters/r_data.rb

Overview

RData Exporter Class, that can be used to export multiple Daru::DataFrames to a RData file

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initialize(**options) ⇒ RData

Initializes a RData Exporter instance.

Examples:

Initializing RData Exporter instance

df1 = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b])

#=> #<Daru::DataFrame(2x2)>
#      a   b
#  0   1   3
#  1   2   4

df2 = Daru::DataFrame.new([[5,6],[7,8]], order: [:x, :y])

#=> #<Daru::DataFrame(2x2)>
#      x   y
#  0   5   7
#  1   6   8

instance = Daru::IO::Exporters::RData.new("first.df": df1, "second.df": df2)

Parameters:

  • options (Hash)

    A set of key-value pairs wherein the key depicts the name of the R data.frame variable name to be saved in the RData file, and the corresponding value depicts the Daru::DataFrame (or any Ruby variable in scope)



31
32
33
34
35
# File 'lib/daru/io/exporters/r_data.rb', line 31

def initialize(**options)
  optional_gem 'rsruby'

  @options = options
end

Instance Method Details

#to_sString

Exports a RData Exporter instance to a file-writable String.

Examples:

Writing to a RData file

instance.to_s

#=> "\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\vr\x890\xE2\x8A\xE0b```b..."

Returns:

  • (String)

    A file-writable string



45
46
47
# File 'lib/daru/io/exporters/r_data.rb', line 45

def to_s
  super
end

#write(path) ⇒ Object

Exports an RData Exporter instance to a rdata file.

Examples:

Writing to a RData file

instance.write("daru_dataframes.RData")

Parameters:

  • path (String)

    Path of RData file where the dataframe(s) is/are to be saved



55
56
57
58
59
60
61
62
# File 'lib/daru/io/exporters/r_data.rb', line 55

def write(path)
  @instance    = RSRuby.instance
  @statements  = @options.map do |r_variable, dataframe|
    process_statements(r_variable, dataframe)
  end.flatten
  @statements << "save(#{@options.keys.map(&:to_s).join(', ')}, file='#{path}')"
  @statements.each { |statement| @instance.eval_R(statement) }
end