Class: Rust::Plots::GGPlot::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/rust/external/ggplot2/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, aes = nil) ⇒ Plot

Returns a new instance of Plot.



58
59
60
61
62
63
64
# File 'lib/rust/external/ggplot2/core.rb', line 58

def initialize(data, aes = nil)
    @layers = []
    
    @data = data
    @aes = aes
    @theme = Rust::Plots::GGPlot.default_theme
end

Instance Attribute Details

#aesObject

Returns the value of attribute aes.



56
57
58
# File 'lib/rust/external/ggplot2/core.rb', line 56

def aes
  @aes
end

#themeObject

Returns the value of attribute theme.



55
56
57
# File 'lib/rust/external/ggplot2/core.rb', line 55

def theme
  @theme
end

Instance Method Details

#+(others) ⇒ Object



125
126
127
128
129
# File 'lib/rust/external/ggplot2/core.rb', line 125

def +(others)
    copy = self.deep_clone
    copy << others
    return copy
end

#<<(others) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rust/external/ggplot2/core.rb', line 113

def <<(others)
    if others.is_a?(Array) && others.all? { |o| o.is_a?(Layer) }
        @layers += others
    elsif others.is_a?(Layer)
        @layers << others
    else
        raise ArgumentError, "Expected Layer or Array of Layers"
    end
    
    return self
end

#inspect(show = true) ⇒ Object



131
132
133
134
# File 'lib/rust/external/ggplot2/core.rb', line 131

def inspect(show = true)
    self.show if show
    return super()
end

#layer(layer) ⇒ Object



66
67
68
# File 'lib/rust/external/ggplot2/core.rb', line 66

def layer(layer)
    @layers << layer
end

#save(filename, **options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rust/external/ggplot2/core.rb', line 82

def save(filename, **options)
    Rust.exclusive do 
        dataset_name = nil
        if @data
            dataset_name = "ggplotter.data"
            @data.load_in_r_as(dataset_name)
        end
        r = self.to_R(dataset_name)
        Rust._eval("ggplot.latest <- #{r}")
        save = Rust::Function.new("ggsave")
        save.arguments = Rust::Arguments.new([Rust::Variable.new("ggplot.latest")])
        save.options = Rust::Options.from_hash({file: filename}.merge(options))
        save.call
    end
end

#showObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rust/external/ggplot2/core.rb', line 70

def show()
    Rust.exclusive do
        dataset_name = nil
        if @data
            dataset_name = "ggplotter.data"
            @data.load_in_r_as(dataset_name)
        end
        r = self.to_R(dataset_name)
        Rust._eval(r)
    end
end

#to_R(data_set_name = "ggplotter.data") ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rust/external/ggplot2/core.rb', line 98

def to_R(data_set_name="ggplotter.data")
    function = Rust::Function.new("ggplot")
    function.arguments = Rust::Arguments.new
    function.arguments << (data_set_name ? Rust::Variable.new(data_set_name) : nil)
    function.arguments << @aes if @aes
    
    result = function.to_R
    result += " + " + @theme.to_R
    @layers.each do |layer|
        result += " + " + layer.to_R
    end
    
    return result
end