galaaz 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +32 -0
- data/Rakefile +177 -0
- data/bin/galaaz +8 -0
- data/examples/50Plots_MasterList/scatter_plot.rb +51 -0
- data/examples/baseball.csv +1 -0
- data/examples/baseball.rb +16 -0
- data/examples/ggplot.rb +178 -0
- data/examples/islr/Figure.jpg +0 -0
- data/examples/islr/all.rb +32 -0
- data/examples/islr/ch2.spec.rb +148 -0
- data/examples/islr/ch3.spec.rb +28 -0
- data/examples/islr/ch3_boston.rb +77 -0
- data/examples/islr/ch3_multiple_regression.rb +36 -0
- data/examples/islr/ch6.spec.rb +64 -0
- data/examples/paper/paper.rb +36 -0
- data/examples/sthda_ggplot/README.md +38 -0
- data/examples/sthda_ggplot/all.rb +68 -0
- data/examples/sthda_ggplot/one_variable_continuous/density_gg.rb +52 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_area.rb +61 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_density.rb +77 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_dotplot.rb +69 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_freqpoly.rb +69 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_histogram.rb +62 -0
- data/examples/sthda_ggplot/one_variable_continuous/histogram_density.rb +55 -0
- data/examples/sthda_ggplot/one_variable_continuous/stat.rb +62 -0
- data/examples/sthda_ggplot/one_variable_discrete/bar.rb +54 -0
- data/examples/sthda_ggplot/qplots/box_violin_dot.rb +57 -0
- data/examples/sthda_ggplot/qplots/scatter_plots.rb +67 -0
- data/examples/sthda_ggplot/scatter_gg.rb +60 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_bin2d.rb +49 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_density2d.rb +64 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +52 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/geom_point.rb +65 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/geom_smooth.rb +66 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +83 -0
- data/examples/sthda_ggplot/two_variables_cont_function/geom_area.rb +63 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +85 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_boxplot.rb +62 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_dotplot.rb +75 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_jitter.rb +74 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_line.rb +55 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_violin.rb +70 -0
- data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +40 -0
- data/examples/sthda_ggplot/two_variables_error/geom_crossbar.rb +108 -0
- data/examples/subsetting.rb +372 -0
- data/lib/expression.rb +45 -0
- data/lib/galaaz.rb +27 -0
- data/lib/r.rb +118 -0
- data/lib/r_methods.rb +89 -0
- data/lib/rbinary_operators.rb +226 -0
- data/lib/rclosure.rb +34 -0
- data/lib/rdata_frame.rb +63 -0
- data/lib/renvironment.rb +34 -0
- data/lib/rexpression.rb +34 -0
- data/lib/rindexed_object.rb +68 -0
- data/lib/rlanguage.rb +64 -0
- data/lib/rlist.rb +72 -0
- data/lib/rmatrix.rb +38 -0
- data/lib/rmd_indexed_object.rb +43 -0
- data/lib/robject.rb +297 -0
- data/lib/rpkg.rb +53 -0
- data/lib/rsupport.rb +292 -0
- data/lib/rsupport_scope.rb +77 -0
- data/lib/rsymbol.rb +57 -0
- data/lib/ruby_callback.rb +83 -0
- data/lib/ruby_extensions.rb +74 -0
- data/lib/runary_operators.rb +58 -0
- data/lib/rvector.rb +117 -0
- data/r_requires/ggplot.rb +31 -0
- data/specs/all.rb +45 -0
- data/specs/r_dataframe.spec.rb +181 -0
- data/specs/r_eval.spec.rb +164 -0
- data/specs/r_function.spec.rb +105 -0
- data/specs/r_language.spec.rb +135 -0
- data/specs/r_list.spec.rb +129 -0
- data/specs/r_list_apply.spec.rb +99 -0
- data/specs/r_matrix.spec.rb +83 -0
- data/specs/r_vector_creation.spec.rb +99 -0
- data/specs/r_vector_functions.spec.rb +59 -0
- data/specs/r_vector_object.spec.rb +94 -0
- data/specs/r_vector_operators.spec.rb +174 -0
- data/specs/r_vector_subsetting.spec.rb +136 -0
- data/specs/tmp.rb +134 -0
- data/version.rb +2 -0
- metadata +198 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
##########################################################################################
|
|
4
|
+
# @author Rodrigo Botafogo
|
|
5
|
+
#
|
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
|
10
|
+
# distributions.
|
|
11
|
+
#
|
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
#
|
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
|
21
|
+
# OR MODIFICATIONS.
|
|
22
|
+
##########################################################################################
|
|
23
|
+
|
|
24
|
+
module R
|
|
25
|
+
|
|
26
|
+
module Scope
|
|
27
|
+
|
|
28
|
+
def self.with(symbol, *args)
|
|
29
|
+
attrs = []
|
|
30
|
+
dataframe = args[0]
|
|
31
|
+
|
|
32
|
+
args.each_with_index do |arg, index|
|
|
33
|
+
arg.names.each { |n| attrs << n.to_sym }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Class.new do
|
|
37
|
+
# create accessor functions for every variable name
|
|
38
|
+
attrs.each do |name|
|
|
39
|
+
define_method (name) do
|
|
40
|
+
dataframe.method_missing(name)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
#----------------------------------------------------------------------------------------
|
|
45
|
+
#
|
|
46
|
+
#----------------------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
define_method (:subset) do |*missing_args|
|
|
49
|
+
R::Support.exec_function(R.subset_method, dataframe, *missing_args)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
#----------------------------------------------------------------------------------------
|
|
53
|
+
#
|
|
54
|
+
#----------------------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
define_method (:method_missing) do |missing_symbol, *missing_args|
|
|
57
|
+
R::Support.process_missing(missing_symbol, false, dataframe, *missing_args)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
module Support
|
|
67
|
+
|
|
68
|
+
def self.new_scope(symbol, *args, &block)
|
|
69
|
+
executionScope = Scope.with(symbol, *args)
|
|
70
|
+
scope = executionScope.new
|
|
71
|
+
scope.instance_eval(&block)
|
|
72
|
+
# scope
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
data/lib/rsymbol.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
##########################################################################################
|
|
4
|
+
# @author Rodrigo Botafogo
|
|
5
|
+
#
|
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
|
10
|
+
# distributions.
|
|
11
|
+
#
|
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
#
|
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
|
21
|
+
# OR MODIFICATIONS.
|
|
22
|
+
##########################################################################################
|
|
23
|
+
|
|
24
|
+
module R
|
|
25
|
+
|
|
26
|
+
#--------------------------------------------------------------------------------------
|
|
27
|
+
#
|
|
28
|
+
#--------------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
class RSymbol < R::Object
|
|
31
|
+
include BinaryOperators
|
|
32
|
+
include FormulaBinOp
|
|
33
|
+
|
|
34
|
+
#--------------------------------------------------------------------------------------
|
|
35
|
+
#
|
|
36
|
+
#--------------------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
def =~(other_object)
|
|
39
|
+
exec_oper("`~`", other_object, true)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
#--------------------------------------------------------------------------------------
|
|
43
|
+
#
|
|
44
|
+
#--------------------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
def ^(other_object)
|
|
47
|
+
exec_oper("`:`", other_object)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
#--------------------------------------------------------------------------------------
|
|
51
|
+
#
|
|
52
|
+
#--------------------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
##########################################################################################
|
|
4
|
+
# @author Rodrigo Botafogo
|
|
5
|
+
#
|
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
|
10
|
+
# distributions.
|
|
11
|
+
#
|
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
#
|
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
|
21
|
+
# OR MODIFICATIONS.
|
|
22
|
+
##########################################################################################
|
|
23
|
+
|
|
24
|
+
module R
|
|
25
|
+
|
|
26
|
+
class RubyCallback
|
|
27
|
+
|
|
28
|
+
# The Ruby Proc, Method (or Object?) to be called back
|
|
29
|
+
attr_reader :object
|
|
30
|
+
# The R function that will call back on the object
|
|
31
|
+
attr_reader :r_function
|
|
32
|
+
|
|
33
|
+
#--------------------------------------------------------------------------------------
|
|
34
|
+
#
|
|
35
|
+
#--------------------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
def self.build(object)
|
|
38
|
+
RubyCallback.new(object).r_function
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
#--------------------------------------------------------------------------------------
|
|
42
|
+
# Initializes a callback object and constructs the R function that calls back the
|
|
43
|
+
# object. RubyCallback class will act as proxy to the actual Ruby Object since it
|
|
44
|
+
# needs to deal with R parameters and Return values boxing and unboxing when needed
|
|
45
|
+
# @param object [Object] Ruby Object
|
|
46
|
+
#--------------------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
def initialize(object)
|
|
49
|
+
@object = object
|
|
50
|
+
|
|
51
|
+
# ruby_callback_method is a method that returns an R function that returns an R
|
|
52
|
+
# function that calls back this object callback method (look at callback bellow)
|
|
53
|
+
@r_function = R::Support.ruby_callback_method.call(method(:callback))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
#--------------------------------------------------------------------------------------
|
|
57
|
+
#
|
|
58
|
+
#--------------------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
def method_missing(symbol, *args)
|
|
61
|
+
p "in ruby_callback.rb method missing with #{symbol} #{args}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
#--------------------------------------------------------------------------------------
|
|
65
|
+
#
|
|
66
|
+
#--------------------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
def callback(*args)
|
|
69
|
+
|
|
70
|
+
# converts every arg into a R::Object (Ruby object that wraps an R Interop)
|
|
71
|
+
args.map! { |arg| R::Object.build(arg) }
|
|
72
|
+
|
|
73
|
+
# calls the callback method and convert the result back to an R object
|
|
74
|
+
# method parse_arg was developed to parse the arguments to an R function
|
|
75
|
+
# but in a callback the return value needs to be converted. In this case
|
|
76
|
+
# the name parse_arg is misleading
|
|
77
|
+
R::Support.parse_arg(@object.call(*args))
|
|
78
|
+
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
##########################################################################################
|
|
4
|
+
# @author Rodrigo Botafogo
|
|
5
|
+
#
|
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
|
10
|
+
# distributions.
|
|
11
|
+
#
|
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
#
|
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
|
21
|
+
# OR MODIFICATIONS.
|
|
22
|
+
##########################################################################################
|
|
23
|
+
|
|
24
|
+
#==========================================================================================
|
|
25
|
+
#
|
|
26
|
+
#==========================================================================================
|
|
27
|
+
|
|
28
|
+
class Range
|
|
29
|
+
|
|
30
|
+
#----------------------------------------------------------------------------------------
|
|
31
|
+
# Defines unary minus operation for ranges
|
|
32
|
+
#----------------------------------------------------------------------------------------
|
|
33
|
+
|
|
34
|
+
def -@
|
|
35
|
+
NegRange.new(self.begin, self.end)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
#==========================================================================================
|
|
41
|
+
# Class NegRange exists to represent a negative range, e.g., -(1...10). Such a range is
|
|
42
|
+
# used to index vectors and means all elements but the ones in the given range. Class
|
|
43
|
+
# NegRange is parsed to become "-(1:10)" in R.
|
|
44
|
+
#==========================================================================================
|
|
45
|
+
|
|
46
|
+
class NegRange < Range
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
#==========================================================================================
|
|
51
|
+
#
|
|
52
|
+
#==========================================================================================
|
|
53
|
+
|
|
54
|
+
class Symbol
|
|
55
|
+
include R::BinaryOperators
|
|
56
|
+
include R::CallBinOp
|
|
57
|
+
|
|
58
|
+
#--------------------------------------------------------------------------------------
|
|
59
|
+
# Unary '+' converts a Ruby Symbol into an R Symbol
|
|
60
|
+
#--------------------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
def +@
|
|
63
|
+
R::Object.build(R::Support.eval("as.name").call(to_s))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
#--------------------------------------------------------------------------------------
|
|
67
|
+
# Unary '~' retrieves the values of the R symbol
|
|
68
|
+
#--------------------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
def ~@
|
|
71
|
+
R::Object.build(R::Support.eval(to_s))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
##########################################################################################
|
|
4
|
+
# @author Rodrigo Botafogo
|
|
5
|
+
#
|
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
|
10
|
+
# distributions.
|
|
11
|
+
#
|
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
#
|
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
|
21
|
+
# OR MODIFICATIONS.
|
|
22
|
+
##########################################################################################
|
|
23
|
+
|
|
24
|
+
module R
|
|
25
|
+
|
|
26
|
+
#--------------------------------------------------------------------------------------
|
|
27
|
+
#
|
|
28
|
+
#--------------------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
module UnaryOperators
|
|
31
|
+
|
|
32
|
+
#--------------------------------------------------------------------------------------
|
|
33
|
+
#
|
|
34
|
+
#--------------------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
def !
|
|
37
|
+
R::Support.exec_function_name("`!`", @r_interop)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
#--------------------------------------------------------------------------------------
|
|
41
|
+
#
|
|
42
|
+
#--------------------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
def -@
|
|
45
|
+
R::Support.exec_function_name("`-`", @r_interop)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
#--------------------------------------------------------------------------------------
|
|
49
|
+
#
|
|
50
|
+
#--------------------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
def +@
|
|
53
|
+
R::Support.exec_function_name("`+`", @r_interop)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
data/lib/rvector.rb
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
##########################################################################################
|
|
4
|
+
# @author Rodrigo Botafogo
|
|
5
|
+
#
|
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
|
10
|
+
# distributions.
|
|
11
|
+
#
|
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
16
|
+
#
|
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
|
21
|
+
# OR MODIFICATIONS.
|
|
22
|
+
##########################################################################################
|
|
23
|
+
|
|
24
|
+
module R
|
|
25
|
+
|
|
26
|
+
class Vector < Object
|
|
27
|
+
include IndexedObject
|
|
28
|
+
include BinaryOperators
|
|
29
|
+
include ExecBinOp
|
|
30
|
+
include UnaryOperators
|
|
31
|
+
include Enumerable
|
|
32
|
+
|
|
33
|
+
#--------------------------------------------------------------------------------------
|
|
34
|
+
#
|
|
35
|
+
#--------------------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
def initialize(r_interop)
|
|
38
|
+
super(r_interop)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
#--------------------------------------------------------------------------------------
|
|
42
|
+
# When indexing with '[' or '[[' an R object is returned. Sometimes we need to have
|
|
43
|
+
# access to an umboxed Ruby element, for instance, in an numeric array, we might want
|
|
44
|
+
# to receive the actual number that can be used in a Ruby method. In this case, we
|
|
45
|
+
# use the '<<' operator.
|
|
46
|
+
# @return the Ruby element at the given index in the vector
|
|
47
|
+
#--------------------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
def <<(index)
|
|
50
|
+
@r_interop[index]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
#--------------------------------------------------------------------------------------
|
|
54
|
+
#
|
|
55
|
+
#--------------------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
def pop
|
|
58
|
+
self << 0
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
#--------------------------------------------------------------------------------------
|
|
62
|
+
# @bug
|
|
63
|
+
# Each cannot return a Enumerator because R is single threaded. When this restriction
|
|
64
|
+
# is removed, make each return self.to_enum
|
|
65
|
+
#--------------------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
def each(result = :vec)
|
|
68
|
+
|
|
69
|
+
case result
|
|
70
|
+
when :vec
|
|
71
|
+
# length is a R::Vector, in order to extract its size as a Numeric we need to
|
|
72
|
+
# use the << operator
|
|
73
|
+
(1..length << 0).each do |i|
|
|
74
|
+
yield self[i]
|
|
75
|
+
end
|
|
76
|
+
when :native
|
|
77
|
+
(0...length << 0).each do |i|
|
|
78
|
+
yield self << i
|
|
79
|
+
end
|
|
80
|
+
else
|
|
81
|
+
raise "Type #{result} is unknown for method :each"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
#--------------------------------------------------------------------------------------
|
|
87
|
+
# Need to override each_with_index, as R indexing starts at 1
|
|
88
|
+
#--------------------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
def each_with_index(result = :vec)
|
|
91
|
+
case result
|
|
92
|
+
when :vec
|
|
93
|
+
(1..length << 0).each do |i|
|
|
94
|
+
yield self[i], i
|
|
95
|
+
end
|
|
96
|
+
when :native
|
|
97
|
+
(0...length << 0).each do |i|
|
|
98
|
+
yield self << i, i
|
|
99
|
+
end
|
|
100
|
+
else
|
|
101
|
+
raise "Type #{result} is unknown for method :each"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
#--------------------------------------------------------------------------------------
|
|
107
|
+
# SHOULD DEFINE COMPARISON BETWEEN TWO VECTORS
|
|
108
|
+
#--------------------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
def <=>(other_vector)
|
|
111
|
+
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
end
|
|
117
|
+
|