Class: Daru::IO::Importers::RData

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

Overview

RData Importer Class, that extends read_rdata method to Daru::DataFrame

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#optional_gem

Constructor Details

#initializeRData

Checks for required gem dependencies of RData Importer



13
14
15
# File 'lib/daru/io/importers/r_data.rb', line 13

def initialize
  super
end

Class Method Details

.read(path) ⇒ Daru::IO::Importers::RData

Reads data from a Rdata file

Examples:

Reading from rdata file

instance = Daru::IO::Importers::RData.read('ACScounty.RData')

Parameters:

  • path (String)

    Path to RData file, where the dataframe is to be imported from.

Returns:



27
28
29
30
31
# File 'lib/daru/io/importers/r_data.rb', line 27

def read(path)
  @instance = RSRuby.instance
  @instance.eval_R("load('#{path}')")
  self
end

Instance Method Details

#call(variable) ⇒ Daru::DataFrame

Imports a Daru::DataFrame from a RData Importer instance and rdata file

Examples:

Importing a particular variable

df = instance.call("ACS3")

#=>   #<Daru::DataFrame(1629x30)>
#           Abbreviati       FIPS     Non.US      State       cnty females.di  ...
#         0         AL       1001       14.7    alabama    autauga       13.8  ...
#         1         AL       1003       13.5    alabama    baldwin       14.1  ...
#         2         AL       1005       20.1    alabama    barbour       16.1  ...
#         3         AL       1009       18.0    alabama     blount       13.7  ...
#         4         AL       1015       18.6    alabama    calhoun       12.9  ...
#       ...        ...        ...        ...        ...        ...        ...  ...

Parameters:

  • variable (String)

    The variable to be imported from the variables stored in the RData file. Please note that the R variable to be imported from the RData file should be a data.frame

Returns:



53
54
55
56
57
58
59
# File 'lib/daru/io/importers/r_data.rb', line 53

def call(variable)
  @variable = variable.to_s

  validate_params

  process_dataframe(@instance.send(@variable.to_sym))
end

#read(path) ⇒ Daru::IO::Importers::RData

Reads data from a Rdata file

Examples:

Reading from rdata file

instance = Daru::IO::Importers::RData.read('ACScounty.RData')

Parameters:

  • path (String)

    Path to RData file, where the dataframe is to be imported from.

Returns:



27
28
29
30
31
# File 'lib/daru/io/importers/r_data.rb', line 27

def read(path)
  @instance = RSRuby.instance
  @instance.eval_R("load('#{path}')")
  self
end