Class: Rust::Formula

Inherits:
RustDatatype show all
Defined in:
lib/rust/core/types/language.rb

Overview

Mirror of the formula type in R.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RustDatatype

pull_priority, #r_hash, #r_mirror, #r_mirror_to

Constructor Details

#initialize(left_part, right_part) ⇒ Formula

Creates a new formula with a given left_part (optional) and right_part (as strings).

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/rust/core/types/language.rb', line 34

def initialize(left_part, right_part)
    raise ArgumentError, "Expected string" if left_part && !left_part.is_a?(String)
    raise ArgumentError, "Expected string" if !right_part.is_a?(String)
    
    @left_part  = left_part || ""
    @right_part = right_part
end

Instance Attribute Details

#left_partObject (readonly)

Returns the value of attribute left_part.



28
29
30
# File 'lib/rust/core/types/language.rb', line 28

def left_part
  @left_part
end

#right_partObject (readonly)

Returns the value of attribute right_part.



29
30
31
# File 'lib/rust/core/types/language.rb', line 29

def right_part
  @right_part
end

Class Method Details

.can_pull?(type, klass) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/rust/core/types/language.rb', line 9

def self.can_pull?(type, klass)
    return klass == "formula" || (klass.is_a?(Array) && klass.include?("formula"))
end

.pull_variable(variable, type, klass) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rust/core/types/language.rb', line 13

def self.pull_variable(variable, type, klass)
    formula_elements = Rust._pull("as.character(#{variable})")

    assert("The number of elements of a formula must be 2 or 3: #{formula_elements} given") { formula_elements.size > 1 && formula_elements.size < 4 }
    if formula_elements.size == 2
       return Formula.new(nil, formula_elements[1]) 
    elsif formula_elements.size == 3
        return Formula.new(formula_elements[2], formula_elements[1])
    end
end

Instance Method Details

#==(oth) ⇒ Object



42
43
44
45
46
# File 'lib/rust/core/types/language.rb', line 42

def ==(oth)
    return false unless oth.is_a?(Formula)
    
    return @left_part == oth.left_part && @right_part == oth.right_part
end

#inspectObject



52
53
54
# File 'lib/rust/core/types/language.rb', line 52

def inspect
    return self.to_R.strip
end

#load_in_r_as(variable_name) ⇒ Object



24
25
26
# File 'lib/rust/core/types/language.rb', line 24

def load_in_r_as(variable_name)
    Rust._eval("#{variable_name} <- #{self.left_part} ~ #{self.right_part}")
end

#to_RObject



48
49
50
# File 'lib/rust/core/types/language.rb', line 48

def to_R
    return "#@left_part ~ #@right_part"
end