class Daru::IO::Exporters::RDS
RDS
Exporter Class, that extends `to_rds_string` and `write_rds` methods to `Daru::DataFrame` instance variables
Public Class Methods
new(dataframe, r_variable)
click to toggle source
Initializes a RDS
Exporter instance.
@param dataframe [Daru::DataFrame] A dataframe to export @param r_variable [String] Name of the R `data.frame` variable name to be saved in the RDS
file
@example Initializing an RData
Exporter
df = Daru::DataFrame.new([[1,2],[3,4]], order: [:a, :b]) #=> #<Daru::DataFrame(2x2)> # a b # 0 1 3 # 1 2 4 instance = Daru::IO::Exporters::RDS.new(df, "sample.dataframe")
Calls superclass method
Daru::IO::Exporters::Base::new
# File lib/daru/io/exporters/rds.rb, line 26 def initialize(dataframe, r_variable) optional_gem 'rsruby' super(dataframe) @r_variable = r_variable end
Public Instance Methods
to_s()
click to toggle source
Exports a RDS
Exporter instance to a file-writable String.
@return [String] A file-writable string
@example Getting a file-writable string from RDS
Exporter instance
instance.to_s #! same as df.to_rds_string("sample.dataframe") #=> "\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\x8B\xE0b```b..."
Calls superclass method
Daru::IO::Exporters::Base#to_s
# File lib/daru/io/exporters/rds.rb, line 41 def to_s super end
write(path)
click to toggle source
Exports a RDS
Exporter instance to a rds file.
@param path [String] Path of RDS
file where the dataframe is to be saved
@example Writing an RDS
Exporter instance to a rds file
instance.write("daru_dataframe.rds")
# File lib/daru/io/exporters/rds.rb, line 51 def write(path) @instance = RSRuby.instance @statements = process_statements(@r_variable, @dataframe) @statements << "saveRDS(#{@r_variable}, file='#{path}')" @statements.each { |statement| @instance.eval_R(statement) } end
Private Instance Methods
convert_datatype(value)
click to toggle source
# File lib/daru/io/exporters/rds.rb, line 69 def convert_datatype(value) case value when nil then 'NA' when String then "'#{value}'" else value end end
process_statements(r_variable, dataframe)
click to toggle source
# File lib/daru/io/exporters/rds.rb, line 60 def process_statements(r_variable, dataframe) [ *dataframe.map_vectors_with_index do |vector, i| "#{i} = c(#{vector.to_a.map { |val| convert_datatype(val) }.join(', ')})" end, "#{r_variable} = data.frame(#{dataframe.vectors.to_a.map(&:to_s).join(', ')})" ] end