Class: Daru::IO::Exporters::RDS

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/exporters/rds.rb

Overview

RDS Exporter Class, that extends to_rds_string and write_rds methods to Daru::DataFrame instance variables

Direct Known Subclasses

RData

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initialize(dataframe, r_variable) ⇒ RDS

Initializes a RDS Exporter instance.

Examples:

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")

Parameters:

  • dataframe (Daru::DataFrame)

    A dataframe to export

  • r_variable (String)

    Name of the R data.frame variable name to be saved in the RDS file



26
27
28
29
30
31
# File 'lib/daru/io/exporters/rds.rb', line 26

def initialize(dataframe, r_variable)
  optional_gem 'rsruby'

  super(dataframe)
  @r_variable = r_variable
end

Instance Method Details

#to_sString

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

Examples:

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..."

Returns:

  • (String)

    A file-writable string



41
42
43
# File 'lib/daru/io/exporters/rds.rb', line 41

def to_s
  super
end

#write(path) ⇒ Object

Exports a RDS Exporter instance to a rds file.

Examples:

Writing an RDS Exporter instance to a rds file

instance.write("daru_dataframe.rds")

Parameters:

  • path (String)

    Path of RDS file where the dataframe is to be saved



51
52
53
54
55
56
# 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