galaaz 0.4.6 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +3575 -118
  3. data/Rakefile +21 -4
  4. data/bin/gknit +152 -6
  5. data/bin/gknit-draft +105 -0
  6. data/bin/gknit-draft.rb +28 -0
  7. data/bin/gknit_Rscript +127 -0
  8. data/bin/grun +27 -1
  9. data/bin/gstudio +47 -4
  10. data/bin/{gstudio.rb → gstudio_irb.rb} +0 -0
  11. data/bin/gstudio_pry.rb +7 -0
  12. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +3 -12
  13. data/blogs/galaaz_ggplot/galaaz_ggplot.html +77 -222
  14. data/blogs/galaaz_ggplot/galaaz_ggplot.md +4 -31
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  16. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
  18. data/blogs/galaaz_ggplot/midwest.Rmd +1 -9
  19. data/blogs/gknit/gknit.Rmd +232 -123
  20. data/blogs/{dev/dev.html → gknit/gknit.html} +1897 -33
  21. data/blogs/gknit/gknit.pdf +0 -0
  22. data/blogs/gknit/lst.rds +0 -0
  23. data/blogs/gknit/stats.bib +27 -0
  24. data/blogs/manual/lst.rds +0 -0
  25. data/blogs/manual/manual.Rmd +1893 -47
  26. data/blogs/manual/manual.html +3153 -347
  27. data/blogs/manual/manual.md +3575 -118
  28. data/blogs/manual/manual.pdf +0 -0
  29. data/blogs/manual/manual.tex +4026 -0
  30. data/blogs/manual/manual_files/figure-html/bubble-1.png +0 -0
  31. data/blogs/manual/manual_files/figure-html/diverging_bar.png +0 -0
  32. data/blogs/manual/manual_files/figure-latex/bubble-1.png +0 -0
  33. data/blogs/manual/manual_files/figure-latex/diverging_bar.pdf +0 -0
  34. data/blogs/{dev → manual}/model.rb +0 -0
  35. data/blogs/nse_dplyr/nse_dplyr.Rmd +849 -0
  36. data/blogs/nse_dplyr/nse_dplyr.html +878 -0
  37. data/blogs/nse_dplyr/nse_dplyr.md +1198 -0
  38. data/blogs/nse_dplyr/nse_dplyr.pdf +0 -0
  39. data/blogs/oh_my/oh_my.html +274 -386
  40. data/blogs/oh_my/oh_my.md +208 -205
  41. data/blogs/ruby_plot/ruby_plot.Rmd +64 -84
  42. data/blogs/ruby_plot/ruby_plot.html +235 -208
  43. data/blogs/ruby_plot/ruby_plot.md +239 -34
  44. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  45. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
  46. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
  48. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
  49. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
  50. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_decorations.png +0 -0
  51. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
  52. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
  53. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
  54. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
  55. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
  56. data/examples/Bibliography/master.bib +50 -0
  57. data/examples/Bibliography/stats.bib +72 -0
  58. data/examples/islr/ch2.spec.rb +1 -1
  59. data/examples/islr/ch3_boston.rb +4 -4
  60. data/examples/islr/x_y_rnorm.jpg +0 -0
  61. data/examples/latex_templates/Test-acm_article/Makefile +16 -0
  62. data/examples/latex_templates/Test-acm_article/Test-acm_article.Rmd +65 -0
  63. data/examples/latex_templates/Test-acm_article/acm_proc_article-sp.cls +1670 -0
  64. data/examples/latex_templates/Test-acm_article/sensys-abstract.cls +703 -0
  65. data/examples/latex_templates/Test-acm_article/sigproc.bib +59 -0
  66. data/examples/latex_templates/Test-acs_article/Test-acs_article.Rmd +260 -0
  67. data/examples/latex_templates/Test-acs_article/Test-acs_article.pdf +0 -0
  68. data/examples/latex_templates/Test-acs_article/acs-Test-acs_article.bib +11 -0
  69. data/examples/latex_templates/Test-acs_article/acs-my_output.bib +11 -0
  70. data/examples/latex_templates/Test-acs_article/acstest.bib +17 -0
  71. data/examples/latex_templates/Test-aea_article/AEA.cls +1414 -0
  72. data/examples/latex_templates/Test-aea_article/BibFile.bib +0 -0
  73. data/examples/latex_templates/Test-aea_article/Test-aea_article.Rmd +108 -0
  74. data/examples/latex_templates/Test-aea_article/Test-aea_article.pdf +0 -0
  75. data/examples/latex_templates/Test-aea_article/aea.bst +1269 -0
  76. data/examples/latex_templates/Test-aea_article/multicol.sty +853 -0
  77. data/examples/latex_templates/Test-aea_article/references.bib +0 -0
  78. data/examples/latex_templates/Test-aea_article/setspace.sty +546 -0
  79. data/examples/latex_templates/Test-amq_article/Test-amq_article.Rmd +256 -0
  80. data/examples/latex_templates/Test-amq_article/Test-amq_article.pdf +0 -0
  81. data/examples/latex_templates/Test-amq_article/Test-amq_article.pdfsync +3397 -0
  82. data/examples/latex_templates/Test-amq_article/pics/Figure2.pdf +0 -0
  83. data/examples/latex_templates/Test-ams_article/Test-ams_article.Rmd +215 -0
  84. data/examples/latex_templates/Test-ams_article/amstest.bib +436 -0
  85. data/examples/latex_templates/Test-asa_article/Test-asa_article.Rmd +153 -0
  86. data/examples/latex_templates/Test-asa_article/Test-asa_article.pdf +0 -0
  87. data/examples/latex_templates/Test-asa_article/agsm.bst +1353 -0
  88. data/examples/latex_templates/Test-asa_article/bibliography.bib +233 -0
  89. data/examples/latex_templates/Test-ieee_article/IEEEtran.bst +2409 -0
  90. data/examples/latex_templates/Test-ieee_article/IEEEtran.cls +6346 -0
  91. data/examples/latex_templates/Test-ieee_article/Test-ieee_article.Rmd +175 -0
  92. data/examples/latex_templates/Test-ieee_article/Test-ieee_article.pdf +0 -0
  93. data/examples/latex_templates/Test-ieee_article/mybibfile.bib +20 -0
  94. data/examples/latex_templates/Test-rjournal_article/RJournal.sty +335 -0
  95. data/examples/latex_templates/Test-rjournal_article/RJreferences.bib +18 -0
  96. data/examples/latex_templates/Test-rjournal_article/RJwrapper.pdf +0 -0
  97. data/examples/latex_templates/Test-rjournal_article/Test-rjournal_article.Rmd +52 -0
  98. data/examples/latex_templates/Test-springer_article/Test-springer_article.Rmd +65 -0
  99. data/examples/latex_templates/Test-springer_article/Test-springer_article.pdf +0 -0
  100. data/examples/latex_templates/Test-springer_article/bibliography.bib +26 -0
  101. data/examples/latex_templates/Test-springer_article/spbasic.bst +1658 -0
  102. data/examples/latex_templates/Test-springer_article/spmpsci.bst +1512 -0
  103. data/examples/latex_templates/Test-springer_article/spphys.bst +1443 -0
  104. data/examples/latex_templates/Test-springer_article/svglov3.clo +113 -0
  105. data/examples/latex_templates/Test-springer_article/svjour3.cls +1431 -0
  106. data/examples/misc/moneyball.rb +1 -1
  107. data/examples/misc/subsetting.rb +37 -37
  108. data/examples/rmarkdown/svm-rmarkdown-anon-ms-example/svm-rmarkdown-anon-ms-example.Rmd +73 -0
  109. data/examples/rmarkdown/svm-rmarkdown-anon-ms-example/svm-rmarkdown-anon-ms-example.pdf +0 -0
  110. data/examples/rmarkdown/svm-rmarkdown-article-example/svm-rmarkdown-article-example.Rmd +382 -0
  111. data/examples/rmarkdown/svm-rmarkdown-article-example/svm-rmarkdown-article-example.pdf +0 -0
  112. data/examples/rmarkdown/svm-rmarkdown-beamer-example/svm-rmarkdown-beamer-example.Rmd +164 -0
  113. data/examples/rmarkdown/svm-rmarkdown-beamer-example/svm-rmarkdown-beamer-example.pdf +0 -0
  114. data/examples/rmarkdown/svm-rmarkdown-cv/svm-rmarkdown-cv.Rmd +92 -0
  115. data/examples/rmarkdown/svm-rmarkdown-cv/svm-rmarkdown-cv.pdf +0 -0
  116. data/examples/rmarkdown/svm-rmarkdown-syllabus-example/attend-grade-relationships.csv +482 -0
  117. data/examples/rmarkdown/svm-rmarkdown-syllabus-example/svm-rmarkdown-syllabus-example.Rmd +280 -0
  118. data/examples/rmarkdown/svm-rmarkdown-syllabus-example/svm-rmarkdown-syllabus-example.pdf +0 -0
  119. data/examples/rmarkdown/svm-xaringan-example/svm-xaringan-example.Rmd +386 -0
  120. data/lib/R_interface/r.rb +2 -2
  121. data/lib/R_interface/r_libs.R +6 -1
  122. data/lib/R_interface/r_methods.rb +12 -2
  123. data/lib/R_interface/rdata_frame.rb +8 -17
  124. data/lib/R_interface/rindexed_object.rb +1 -2
  125. data/lib/R_interface/rlist.rb +1 -0
  126. data/lib/R_interface/robject.rb +20 -23
  127. data/lib/R_interface/rpkg.rb +15 -6
  128. data/lib/R_interface/rsupport.rb +13 -19
  129. data/lib/R_interface/ruby_extensions.rb +14 -18
  130. data/lib/R_interface/rvector.rb +0 -12
  131. data/lib/gknit.rb +2 -0
  132. data/lib/gknit/draft.rb +105 -0
  133. data/lib/gknit/knitr_engine.rb +6 -37
  134. data/lib/util/exec_ruby.rb +22 -84
  135. data/lib/util/inline_file.rb +7 -3
  136. data/specs/figures/bg.jpeg +0 -0
  137. data/specs/figures/bg.png +0 -0
  138. data/specs/figures/bg.svg +2 -2
  139. data/specs/figures/dose_len.png +0 -0
  140. data/specs/figures/no_args.jpeg +0 -0
  141. data/specs/figures/no_args.png +0 -0
  142. data/specs/figures/no_args.svg +2 -2
  143. data/specs/figures/width_height.jpeg +0 -0
  144. data/specs/figures/width_height.png +0 -0
  145. data/specs/figures/width_height_units1.jpeg +0 -0
  146. data/specs/figures/width_height_units1.png +0 -0
  147. data/specs/figures/width_height_units2.jpeg +0 -0
  148. data/specs/figures/width_height_units2.png +0 -0
  149. data/specs/r_dataframe.spec.rb +184 -11
  150. data/specs/r_list.spec.rb +4 -4
  151. data/specs/r_list_apply.spec.rb +11 -10
  152. data/specs/ruby_expression.spec.rb +3 -11
  153. data/specs/tmp.rb +106 -34
  154. data/version.rb +1 -1
  155. metadata +96 -33
  156. data/bin/gknit_old_r +0 -236
  157. data/blogs/dev/dev.Rmd +0 -77
  158. data/blogs/dev/dev.md +0 -87
  159. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  160. data/blogs/dev/dev_files/figure-html/diverging_bar. +0 -0
  161. data/blogs/dev/dev_files/figure-html/diverging_bar.png +0 -0
  162. data/blogs/dplyr/dplyr.rb +0 -63
  163. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +0 -43
  164. data/blogs/galaaz_ggplot/galaaz_ggplot.log +0 -640
  165. data/blogs/galaaz_ggplot/galaaz_ggplot.out +0 -10
  166. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +0 -481
  167. data/blogs/galaaz_ggplot/midwest.png +0 -0
  168. data/blogs/galaaz_ggplot/scatter_plot.png +0 -0
  169. data/blogs/ruby_plot/ruby_plot.Rmd_external_figs +0 -662
  170. data/blogs/ruby_plot/ruby_plot.tex +0 -1077
  171. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.svg +0 -57
  172. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.svg +0 -106
  173. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.svg +0 -110
  174. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.svg +0 -174
  175. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.svg +0 -236
  176. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.svg +0 -296
  177. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.svg +0 -236
  178. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.svg +0 -218
  179. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.svg +0 -128
  180. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.svg +0 -150
  181. data/examples/paper/paper.rb +0 -36
@@ -572,8 +572,8 @@ class KnitrEngine
572
572
  plot = R.evaluate_plot_snapshot
573
573
 
574
574
  if (!(plot.is__null >> 0))
575
-
576
575
  # create directory for the graphics files if does not already exists
576
+ # unless (R.dir__exists(@fig__path) >> 0)
577
577
  unless File.directory?(@fig__path)
578
578
  FileUtils.mkdir_p(@fig__path)
579
579
  end
@@ -652,11 +652,13 @@ class KnitrEngine
652
652
  # function engine_output will format whatever is in out inside a white box
653
653
  out = R.engine_output(@options, out: res) if @echo
654
654
 
655
- # ouputs the data in RubyChunk '@@outputs' variable. Everything that should
655
+ # ouputs the data in RubyChunk '@outputs' variable. Everything that should
656
656
  # be processed by 'pandoc' and not appear in the output block from
657
657
  # engine_output, should be outputed with the 'outputs' function and will be
658
- # stored in the @@outputs variable
659
- out = R.c(out, RubyChunk.get_outputs)
658
+ # stored in the @outputs variable
659
+ # out = R.c(out, RubyChunk.get_outputs)
660
+ out = R.c(out, RChunk.out_list)
661
+ RChunk.reset_outputs
660
662
 
661
663
  # @TODO: allow capturing many plots in the block. For now, only the last
662
664
  # plot will be captured. Not a very serious problem for now.
@@ -685,36 +687,3 @@ class KnitrEngine
685
687
  end
686
688
 
687
689
  end
688
-
689
- =begin
690
- module R
691
-
692
- class Object
693
-
694
- #--------------------------------------------------------------------------------------
695
- # Redefine to_s in order to capture plots when in knitr
696
- #--------------------------------------------------------------------------------------
697
-
698
- def to_s
699
-
700
- STDERR.puts "+++++++++++++++++++++++++++"
701
- STDERR.puts "in to_s"
702
- STDERR.puts "+++++++++++++++++++++++++++"
703
-
704
- cap = nil
705
- cap = R::Support.capture.call(r_interop)
706
- str = String.new
707
- (0...(cap.size - 1)).each do |i|
708
- str << cap[i] << "\n"
709
- end
710
- str << cap[cap.size - 1] if cap.size >= 1
711
- re = RubyEngine.instance
712
- re.capture_plot
713
- str
714
- end
715
-
716
- end
717
-
718
- end
719
-
720
- =end
@@ -24,72 +24,35 @@
24
24
  require 'stringio'
25
25
 
26
26
  #----------------------------------------------------------------------------------------
27
- # Path StringIO puts... Already opened an issue in RC12
27
+ # Class RC is used only as a context for all ruby chunks in the rmarkdown file.
28
+ # This allows for chunks to access local variables defined in other chunks.
28
29
  #----------------------------------------------------------------------------------------
29
30
 
30
- class StringIO
31
-
32
- def puts(*args)
33
-
34
- if args.empty?
35
- write(DEFAULT_RECORD_SEPARATOR)
36
- else
37
- args.each do |arg|
38
-
39
- # method to_s is not being called when the output is diverted
40
- # need to fix it
41
- if (arg.is_a? R::Object)
42
- arg = arg.to_s
43
- end
44
-
45
- if arg.nil?
46
- line = ''
47
- elsif Thread.guarding? arg
48
- line = '[...]'
49
- else
50
- begin
51
- arg = Truffle::Type.coerce_to(arg, Array, :to_ary)
52
- Thread.recursion_guard arg do
53
- arg.each { |a| puts a }
54
- end
55
- next
56
- rescue
57
- line = arg.to_s
58
- end
59
- end
60
-
61
- write(line)
62
- write(DEFAULT_RECORD_SEPARATOR) # unless line[-1] == ?\n
63
- end
64
- end
65
-
66
- nil
67
- end
68
-
69
- end
31
+ class RC
70
32
 
33
+ attr_reader :out_list
71
34
 
72
- #----------------------------------------------------------------------------------------
73
- # Class RubyChunk is used only as a context for all ruby chunks in the rmarkdown file.
74
- # This allows for chunks to access instance_variables (@)
75
- #----------------------------------------------------------------------------------------
76
-
77
- class RubyChunk
35
+ def initialize
36
+ @out_list = R.list
37
+ end
78
38
 
79
- def self.init
80
- @@outputs = R.list
39
+ def outputs(obj)
40
+ @out_list = R.c(@out_list, obj)
81
41
  end
82
-
83
- def self.get_outputs
84
- @@outputs
42
+
43
+ def reset_outputs
44
+ @out_list = nil
85
45
  end
86
-
87
- def self.outputs(obj)
88
- @@outputs = R.c(@@outputs, obj)
46
+
47
+ def get_binding
48
+ binding
89
49
  end
90
50
 
91
51
  end
92
52
 
53
+ RChunk = RC.new
54
+ RCbinding = RChunk.get_binding
55
+
93
56
  #----------------------------------------------------------------------------------------
94
57
  #
95
58
  #----------------------------------------------------------------------------------------
@@ -114,11 +77,11 @@ module GalaazUtil
114
77
 
115
78
  def self.exec_ruby(options)
116
79
 
117
- RubyChunk.init
80
+ # RubyChunk.init
118
81
 
119
82
  # read the chunk code
120
83
  code = R.paste(options.code, collapse: "\n") >> 0
121
-
84
+
122
85
  # the output should be a list with the proper structure to pass to
123
86
  # function engine_output. We first add the souce code from the block to
124
87
  # the list
@@ -135,7 +98,8 @@ module GalaazUtil
135
98
  # Execute the Ruby code in the scope of class RubyChunk. This is done
136
99
  # so that instance variables created in one chunk can be used again on
137
100
  # another chunk
138
- RubyChunk.instance_eval(code) if (options[["eval"]] >> 0)
101
+ # RChunk.instance_eval(code) if (options[["eval"]] >> 0)
102
+ eval(code, RCbinding, __FILE__, __LINE__ + 1) if (options[["eval"]] >> 0)
139
103
 
140
104
  # add the returned value to the list
141
105
  # this should have captured everything in the evaluation code
@@ -173,30 +137,4 @@ module GalaazUtil
173
137
 
174
138
  end
175
139
 
176
- #----------------------------------------------------------------------------------------
177
- # Used by old gknit. Will eventually be replaced by exe_ruby
178
- #----------------------------------------------------------------------------------------
179
-
180
- def self.exec_ruby_tor(code)
181
-
182
- # the output should be a list with the proper structure to pass to
183
- # function engine_output.
184
- out_list = R.list(R.structure(R.list(src: code), class: 'source'))
185
-
186
- # Set up standard output as a StringIO object.
187
- $stdout = StringIO.new
188
- RubyChunk.instance_eval(code)
189
-
190
- # this should have captured everything in the evaluation code
191
- # it is not working since at least RC10.
192
- out = $stdout.string
193
-
194
- out_list = R.c(out_list, out)
195
-
196
- # return $stdout to standard output
197
- $stdout = STDOUT
198
- R::Support.parse_arg(out_list)
199
-
200
- end
201
-
202
140
  end
@@ -49,12 +49,16 @@ module GalaazUtil
49
49
 
50
50
  if files != nil
51
51
  file = "#{path}/#{filename}"
52
- break if File.exist?(file)
52
+ # break if File.exist?(file)
53
+ break if (R.file__exists(file) >> 0)
53
54
  end
54
55
  end
55
56
  end
56
-
57
- if File.exist?(file)
57
+
58
+ # There is a bug(?) in > RC15 that when the bellow command
59
+ # is called, there is a call to R Polyglot eval passing to_i
60
+ # if File.exist?(file)
61
+ if (R.file__exists(file) >> 0)
58
62
  code = ""
59
63
  File.open(file, "r") do |fileObj|
60
64
  while (line = fileObj.gets)
Binary file
Binary file
@@ -3,7 +3,7 @@
3
3
  <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1' viewBox='0 0 506 506' style='fill:transparent'>
4
4
  <g style='stroke-width:1.4226377952755906;stroke-linejoin:round;stroke-linecap:round'>
5
5
  <rect x='0' y='0' width='506' height='506' style='stroke:#FFFFFF;fill:#FFFFFF'/>
6
- <rect x='31' y='6' width='469' height='473' style='stroke:rgb(0,0,0);stroke-opacity:0.000;fill:#EBEBEB'/>
6
+ <rect x='31' y='6' width='469' height='473' style='stroke:rgb(0,0,0);stroke-opacity:0,000;fill:#EBEBEB'/>
7
7
  </g><g style='stroke-width:0.7113188976377953;stroke-linejoin:round;stroke-linecap:butt'>
8
8
  <polyline points='31,446 500,446' style='stroke:#FFFFFF'/>
9
9
  <polyline points='31,301 500,301' style='stroke:#FFFFFF'/>
@@ -54,4 +54,4 @@
54
54
  <text x='410' y='490' lengthAdjust='spacingAndGlyphs' textLength='5px' style='font-size:8.8px;fill:#4D4D4D'>2</text>
55
55
  <text x='253' y='500' lengthAdjust='spacingAndGlyphs' textLength='26px' style='font-size:11.0px;fill:#000000'>dose</text>
56
56
  <text x='13' y='252' lengthAdjust='spacingAndGlyphs' textLength='20px' style='font-size:11.0px;fill:#000000' transform='rotate(-90,13,252)'>len</text>
57
- </svg>
57
+ </svg>
Binary file
Binary file
Binary file
@@ -3,7 +3,7 @@
3
3
  <svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1' viewBox='0 0 506 506' style='fill:transparent'>
4
4
  <g style='stroke-width:1.4226377952755906;stroke-linejoin:round;stroke-linecap:round'>
5
5
  <rect x='0' y='0' width='506' height='506' style='stroke:#FFFFFF;fill:#FFFFFF'/>
6
- <rect x='31' y='6' width='469' height='473' style='stroke:rgb(0,0,0);stroke-opacity:0.000;fill:#EBEBEB'/>
6
+ <rect x='31' y='6' width='469' height='473' style='stroke:rgb(0,0,0);stroke-opacity:0,000;fill:#EBEBEB'/>
7
7
  </g><g style='stroke-width:0.7113188976377953;stroke-linejoin:round;stroke-linecap:butt'>
8
8
  <polyline points='31,446 500,446' style='stroke:#FFFFFF'/>
9
9
  <polyline points='31,301 500,301' style='stroke:#FFFFFF'/>
@@ -54,4 +54,4 @@
54
54
  <text x='410' y='490' lengthAdjust='spacingAndGlyphs' textLength='5px' style='font-size:8.8px;fill:#4D4D4D'>2</text>
55
55
  <text x='253' y='500' lengthAdjust='spacingAndGlyphs' textLength='26px' style='font-size:11.0px;fill:#000000'>dose</text>
56
56
  <text x='13' y='252' lengthAdjust='spacingAndGlyphs' textLength='20px' style='font-size:11.0px;fill:#000000' transform='rotate(-90,13,252)'>len</text>
57
- </svg>
57
+ </svg>
@@ -72,6 +72,179 @@ describe R::DataFrame do
72
72
  end
73
73
 
74
74
  end
75
+
76
+ #----------------------------------------------------------------------------------------
77
+ context "Subsetting with '['" do
78
+
79
+ before(:each) do
80
+ @mtcars = ~:mtcars
81
+ end
82
+
83
+ it "should subset with [ with only the column index" do
84
+ slice = @mtcars[1]
85
+ expect(slice.typeof).to eq "list"
86
+ expect(slice.rclass).to eq "data.frame"
87
+ expect(slice.length).to eq 1
88
+ expect(slice[[1]][1]).to eq 21.0
89
+ expect(slice[[1, 1]]).to eq 21.0
90
+ # undefined columns selected (RError)
91
+ expect { slice['Mazda RX4'] }.to raise_error(RuntimeError)
92
+ expect(slice[['Mazda RX4']]).to eq nil
93
+ end
94
+
95
+ it "should subset with [ with a column range" do
96
+ slice = @mtcars[(1..3)]
97
+ expect(slice.typeof).to eq "list"
98
+ expect(slice.rclass).to eq "data.frame"
99
+ expect(slice.length).to eq 3
100
+ expect(slice[[2]][10]).to eq 6.0
101
+ expect(slice[10, 2]).to eq 6.0
102
+ end
103
+
104
+ it "should subset with [ by row and column indices" do
105
+ expect(@mtcars[1, 1]).to eq 21.0
106
+ expect(@mtcars[6, 5]).to eq 2.76
107
+ end
108
+
109
+ it "do not use R style missing value idexing with [" do
110
+ # This array bellow is equivalent to [6]. This can bring some surprises for
111
+ # R people that are used to this indexing notation, as in this case the ','
112
+ # is ignored
113
+ arr = [6, ]
114
+ expect(arr).to eq [6]
115
+
116
+ # This will output the sixth column of the mtcars dataframe
117
+ slice = @mtcars[6, ]
118
+ expect(slice.typeof).to eq "list"
119
+ expect(slice.rclass).to eq "data.frame"
120
+ expect(slice.length).to eq 1
121
+ expect(slice[[1]][1]).to eq 2.62
122
+ end
123
+
124
+ it "should subset with [ by row index and :all" do
125
+ # Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
126
+ slice = @mtcars[5, :all]
127
+ expect(slice.rclass).to eq "data.frame"
128
+ expect(slice[1, 1]).to eq 18.7
129
+ expect(slice[1, 1]).not_to eq 360.0
130
+ expect(slice.names).to eq R.c("mpg", "cyl", "disp", "hp", "drat",
131
+ "wt", "qsec", "vs", "am", "gear", "carb")
132
+ end
133
+
134
+ it "should subset with [ by row index, :all and allowing for drop parameter" do
135
+ # will transform the data into a list
136
+ lst = @mtcars[1, :all, drop: true]
137
+ expect(lst.typeof).to eq "list"
138
+ expect(lst[[1]]).to eq 21.0
139
+ expect(lst[1]).to eq R.list(mpg: 21.0)
140
+ end
141
+
142
+ it "should subset with [ by :all and column index" do
143
+ # [1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52
144
+ # [20] 65 97 150 150 245 175 66 91 113 264 175 335 109
145
+ slice = @mtcars[:all, 4]
146
+ expect(slice.rclass).to eq "numeric"
147
+ expect(slice.typeof).to eq "double"
148
+ expect(slice[10]).to eq 123.0
149
+ end
150
+
151
+ it "should subset with [ by :all, column index and allowing for 'drop' parameter" do
152
+ expect(@mtcars[:all, 1]).to eq @mtcars[:all, 1, drop: true]
153
+ end
154
+
155
+ it "should subset with [ and column name" do
156
+ slice = @mtcars['drat']
157
+ expect(slice.typeof).to eq "list"
158
+ expect(slice.rclass).to eq "data.frame"
159
+ expect(slice.length).to eq 1
160
+ expect(slice[[1]][1]).to eq 3.90
161
+ expect(slice[[1, 1]]).to eq 3.90
162
+ # undefined columns selected (RError)
163
+ expect { slice['Mazda RX4'] }.to raise_error(RuntimeError)
164
+ expect(slice[['Mazda RX4']]).to eq nil
165
+ end
166
+
167
+ it "should subset with [ and row name" do
168
+ slice = @mtcars['Merc 450SE', :all]
169
+ expect(slice[[1]]).to eq 16.4
170
+ expect(slice[['hp']]).to eq 180.0
171
+ end
172
+
173
+ it "should subset with [ and row and column name" do
174
+ slice = @mtcars['Merc 450SL', 'drat']
175
+ expect(slice).to eq 3.07
176
+ end
177
+
178
+ end
179
+
180
+ #----------------------------------------------------------------------------------------
181
+ context "Subsetting with '[['" do
182
+
183
+ before(:each) do
184
+ @mtcars = ~:mtcars
185
+ end
186
+
187
+ it "should subset with [[ and a column index" do
188
+ slice = @mtcars[[1]]
189
+ expect(slice.typeof).to eq "double"
190
+ expect(slice.rclass).to eq "numeric"
191
+ expect(slice[1]).to eq 21.0
192
+ expect(slice[16]).to eq 10.4
193
+ end
194
+
195
+ it "should subset with [[ and a row index" do
196
+ slice = @mtcars
197
+ end
198
+
199
+ it "should subset with [[ and row and column indices" do
200
+ expect(@mtcars[[6, 1]]).to eq 18.1
201
+ expect(@mtcars[[6, 5]]).to eq 2.76
202
+ expect(@mtcars[[15, 1]]).to eq 10.4
203
+ expect(@mtcars[[15, 7]]).to eq 17.98
204
+ end
205
+
206
+ it "should subset with [[ and row and column names" do
207
+ expect(@mtcars[['Valiant', 'mpg']]).to eq 18.1
208
+ expect(@mtcars[['Valiant', 'drat']]).to eq 2.76
209
+ expect(@mtcars[['Cadillac Fleetwood', 'mpg']]).to eq 10.4
210
+ expect(@mtcars[['Cadillac Fleetwood', 'qsec']]).to eq 17.98
211
+ end
212
+
213
+ end
214
+
215
+ #----------------------------------------------------------------------------------------
216
+ context "Assigning with '[<-'" do
217
+
218
+ before(:each) do
219
+ @mtcars = ~:mtcars
220
+ end
221
+
222
+ it "should add a column to a dataframe indexing by column" do
223
+ @mtcars["New Column"] = R.c((1..32))
224
+ expect(@mtcars[[1, 12]]).to eq 1
225
+ expect(@mtcars[['Mazda RX4', 'New Column']]).to eq 1
226
+ end
227
+
228
+ it "should add a column to a dataframe indexing with :all in the row" do
229
+ @mtcars[:all, 'New Column'] = R.c((1..32))
230
+ expect(@mtcars[[1, 12]]).to eq 1
231
+ expect(@mtcars[['Mazda RX4', 'New Column']]).to eq 1
232
+ end
233
+
234
+ it "should assign to an element of a dataframe indexing with row and column by number" do
235
+ @mtcars[17, 6] = 1000
236
+ expect(@mtcars[[17, 6]]).to eq 1000.0
237
+ expect(@mtcars[['Chrysler Imperial', 'wt']]).to eq 1000.0
238
+ end
239
+
240
+ it "should assign to a range" do
241
+ @mtcars[(10..12)] = R.list((1..32), nil, "New Col": R.c(32..1))
242
+ expect(@mtcars[['Fiat 128', 'gear']]).to eq 18
243
+ expect(@mtcars[['Chrysler Imperial', 'New Col']]).to eq 16
244
+ end
245
+
246
+ end
247
+
75
248
  #----------------------------------------------------------------------------------------
76
249
  context "Ruby 'each'" do
77
250
 
@@ -148,7 +321,7 @@ describe R::DataFrame do
148
321
  end
149
322
 
150
323
  #----------------------------------------------------------------------------------------
151
- context "Boostraping" do
324
+ context "Bootsraping" do
152
325
 
153
326
  before(:each) do
154
327
  @df = R.data__frame(x: R.rep((1..3), each: 2), y: (6..1), z: (~:letters)[(1..6)])
@@ -158,8 +331,8 @@ describe R::DataFrame do
158
331
  it "should do reordering with integer subsetting" do
159
332
 
160
333
  table = @df[R.sample(@df.nrow), :all]
161
- expect(table.x[3]).to eq 3
162
- expect(table.y[2]).to eq 5
334
+ expect(table.x[3]).to eq 1
335
+ expect(table.y[2]).to eq 6
163
336
  expect(table.z.levels[6]).to eq "f"
164
337
 
165
338
  end
@@ -167,17 +340,17 @@ describe R::DataFrame do
167
340
  it "should select random rows with integer subsetting" do
168
341
  table = @df[R.sample(@df.nrow, 3), :all]
169
342
 
170
- expect(table.x[3]).to eq 3
171
- expect(table.y[1]).to eq 3
172
- expect(table.z.levels[table.z[2]]).to eq "b"
343
+ expect(table.x[3]).to eq 1
344
+ expect(table.y[1]).to eq 4
345
+ expect(table.z.levels[table.z[2]]).to eq "a"
173
346
  end
174
347
 
175
348
  it "should randomly reorder a dataframe" do
176
349
  df2 = @df[R.sample(@df.nrow), (3..1)]
177
350
 
178
- expect(df2.z.levels[df2.z[4]]).to eq "c"
179
- expect(df2.y[2]).to eq 5
180
- expect(df2.x[5]).to eq 1
351
+ expect(df2.z.levels[df2.z[4]]).to eq "f"
352
+ expect(df2.y[2]).to eq 6
353
+ expect(df2.x[5]).to eq 2
181
354
  end
182
355
 
183
356
  it "should reorder by a given variable" do
@@ -185,8 +358,8 @@ describe R::DataFrame do
185
358
  df2 = df2[df2.x.order, :all]
186
359
 
187
360
  expect(df2.x[1]).to eq 1
188
- expect(df2.y[1]).to eq 5
189
- expect(df2.z.levels[df2.z[1]]).to eq "b"
361
+ expect(df2.y[1]).to eq 6
362
+ expect(df2.z.levels[df2.z[1]]).to eq "a"
190
363
  end
191
364
 
192
365
  it "should reorder variables" do