galaaz 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,136 @@
1
+ ---
2
+ title: "Non Standard Evaluation in dplyr with Galaaz"
3
+ author:
4
+ - "Rodrigo Botafogo"
5
+ - "Daniel Mossé - University of Pittsburgh"
6
+ tags: [Tech, Data Science, Ruby, R, GraalVM]
7
+ date: "20/02/2019"
8
+ output:
9
+ html_document:
10
+ self_contained: true
11
+ keep_md: true
12
+ pdf_document:
13
+ includes:
14
+ in_header: ["../../sty/galaaz.sty"]
15
+ number_sections: yes
16
+ ---
17
+
18
+
19
+
20
+ # Introduction
21
+
22
+
23
+ ```r
24
+ df <- data.frame(x = 1:3, y = 3:1)
25
+ print(df)
26
+ ```
27
+
28
+ ```
29
+ ## x y
30
+ ## 1 1 3
31
+ ## 2 2 2
32
+ ## 3 3 1
33
+ ```
34
+
35
+ ```r
36
+ print(filter(df, x == 1))
37
+ ```
38
+
39
+ ```
40
+ ## x y
41
+ ## 1 1 3
42
+ ```
43
+
44
+ ```r
45
+ #> # A tibble: 1 x 2
46
+ #> x y
47
+ #> <int> <int>
48
+ #> 1 1 3
49
+ ```
50
+
51
+
52
+ ```r
53
+ my_var <- x
54
+ #> Error in eval(expr, envir, enclos): object 'x' not found
55
+ filter(df, my_var == 1)
56
+ #> Error: object 'my_var' not found
57
+ ```
58
+
59
+
60
+ ```ruby
61
+ @df = R.data__frame(x: (1..3), y: (3..1))
62
+ puts @df
63
+
64
+ puts @df.filter(:x.eq 1)
65
+ ```
66
+
67
+ ```
68
+ ## x y
69
+ ## 1 1 3
70
+ ## 2 2 2
71
+ ## 3 3 1
72
+ ## x y
73
+ ## 1 1 3
74
+ ```
75
+
76
+
77
+ ```ruby
78
+ my_var = :x
79
+ puts @df.filter(my_var.eq 1)
80
+ ```
81
+
82
+ ```
83
+ ## x y
84
+ ## 1 1 3
85
+ ```
86
+
87
+ >> dplyr code is ambiguous. Depending on what variables are defined where,
88
+ >> filter(df, x == y) could be equivalent to any of:
89
+
90
+ ```
91
+ df[df$x == df$y, ]
92
+ df[df$x == y, ]
93
+ df[x == df$y, ]
94
+ df[x == y, ]
95
+ ```
96
+
97
+ In galaaz this ambiguity does not exist
98
+
99
+
100
+ ```ruby
101
+ y = 2
102
+ x = 2
103
+
104
+ @df[:x.eq :y, :all]
105
+ @df[:x.eq y, :all]
106
+ # @df[x.eq :y, :all]
107
+ # @df[x == y, :all]
108
+ ```
109
+
110
+ ```
111
+ ## Message:
112
+ ## wrong number of arguments (given 2, expected 1)
113
+ ```
114
+
115
+ ```
116
+ ## Message:
117
+ ## /home/rbotafogo/desenv/galaaz/lib/R_interface/rbinary_operators.rb:134:in `eq'
118
+ ## (eval):4:in `exec_ruby'
119
+ ## /home/rbotafogo/desenv/galaaz/lib/util/exec_ruby.rb:138:in `instance_eval'
120
+ ## /home/rbotafogo/desenv/galaaz/lib/util/exec_ruby.rb:138:in `exec_ruby'
121
+ ## /home/rbotafogo/desenv/galaaz/lib/gknit/knitr_engine.rb:650:in `block in initialize'
122
+ ## /home/rbotafogo/desenv/galaaz/lib/R_interface/ruby_callback.rb:77:in `call'
123
+ ## /home/rbotafogo/desenv/galaaz/lib/R_interface/ruby_callback.rb:77:in `callback'
124
+ ## (eval):3:in `function(...) {\n rb_method(...)'
125
+ ## unknown.r:1:in `in_dir'
126
+ ## unknown.r:1:in `block_exec'
127
+ ## /home/rbotafogo/lib/graalvm-ce-1.0.0-rc12/jre/languages/R/library/knitr/R/block.R:91:in `call_block'
128
+ ## /home/rbotafogo/lib/graalvm-ce-1.0.0-rc12/jre/languages/R/library/knitr/R/block.R:6:in `process_group.block'
129
+ ## /home/rbotafogo/lib/graalvm-ce-1.0.0-rc12/jre/languages/R/library/knitr/R/block.R:3:in `<no source>'
130
+ ## unknown.r:1:in `withCallingHandlers'
131
+ ## unknown.r:1:in `process_file'
132
+ ## unknown.r:1:in `<no source>'
133
+ ## unknown.r:1:in `<no source>'
134
+ ## <REPL>:5:in `<repl wrapper>'
135
+ ## <REPL>:1
136
+ ```
@@ -3,7 +3,12 @@ list.of.packages <- c('rlang', 'purrr', 'formula.tools', 'lobstr')
3
3
  new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
4
4
  if(length(new.packages)) install.packages(new.packages)
5
5
 
6
+ if (!('data.table' %in% installed.packages()[, "Package"])) {
7
+ install.fastr.packages('data.table')
8
+ }
9
+
6
10
  library('formula.tools')
7
11
  library('lobstr')
8
12
  library('rlang')
9
13
  library('purrr')
14
+
@@ -331,6 +331,14 @@ module R
331
331
  #
332
332
  #--------------------------------------------------------------------------------------
333
333
 
334
+ def pretty_print(obj)
335
+ puts self
336
+ end
337
+
338
+ #--------------------------------------------------------------------------------------
339
+ #
340
+ #--------------------------------------------------------------------------------------
341
+
334
342
  def to_s
335
343
 
336
344
  begin
@@ -244,18 +244,16 @@ module R
244
244
  # of the output is greater than 0
245
245
  if (@@exec_counter == 0)
246
246
  R::Support.stop_capture.call(@@con)
247
- =begin
248
247
  if (R::Support.eval("length(r_capture) > 0")[0])
249
248
  cap = R::Object.build(R::Support.eval("r_capture"))
250
249
  (0...cap.size).each do |i|
251
- puts cap << i
250
+ puts cap >> i
252
251
  end
253
252
  end
254
- =end
255
253
  end
256
254
 
257
255
  rescue StandardError => e
258
- R::Support.stop_capture.call(@@con)
256
+ R::Support.stop_capture.call(@@con) if (@@exec_counter == 0)
259
257
  raise e
260
258
  end
261
259
 
@@ -21,6 +21,15 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
+ module Kernel
25
+
26
+ def puts(*args)
27
+ print(*args)
28
+ print "\n"
29
+ end
30
+
31
+ end
32
+
24
33
  #==========================================================================================
25
34
  #
26
35
  #==========================================================================================
@@ -31,6 +31,10 @@ class StringIO
31
31
 
32
32
  def puts(*args)
33
33
 
34
+ print "========================\n"
35
+ print "Inside puts\n"
36
+ print "========================\n"
37
+
34
38
  if args.empty?
35
39
  write(DEFAULT_RECORD_SEPARATOR)
36
40
  else
@@ -68,7 +72,6 @@ class StringIO
68
72
 
69
73
  end
70
74
 
71
-
72
75
  #----------------------------------------------------------------------------------------
73
76
  # Class RubyChunk is used only as a context for all ruby chunks in the rmarkdown file.
74
77
  # This allows for chunks to access instance_variables (@)
@@ -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>
@@ -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>
@@ -62,20 +62,20 @@ describe R::List do
62
62
  expect(quant.a[1]).to eq R.c('0%': 1.0)
63
63
  expect(quant.a[[1]]).to eq 1.0
64
64
 
65
- expect(quant.a[["50%"]]).to eq 5.50
66
- expect(quant.a['50%']).to eq R.c('50%': 5.50)
67
- expect(R.all__equal(quant.beta["100%"],
68
- R.c('100%': 20.08553692),
65
+ expect(quant.a[['50,00000%']]).to eq 5.50
66
+ expect(quant.a['50,00000%']).to eq R.c('50,00000%': 5.50)
67
+ expect(R.all__equal(quant.beta['100,0000%'],
68
+ R.c('100,0000%': 20.08553692),
69
69
  tolerance: (~:".Machine").double__eps ** 0.5)).to eq true
70
70
  expect(quant.logic[[2]]).to eq 0.0
71
71
  end
72
-
72
+
73
73
  it "should allow using method lapply with quantile" do
74
74
  x = R.lapply(@x, @q, R.c(0.25, 0.50, 0.75))
75
75
  expect(x.a[[1]]).to eq 3.25
76
- expect(x.a[["50%"]]).to eq 5.50
77
- expect(R.all__equal(x.beta['75%'],
78
- R.c('75%': 5.0536690),
76
+ expect(x.a[['50,00000%']]).to eq 5.50
77
+ expect(R.all__equal(x.beta['75,00000%'],
78
+ R.c('75,00000%': 5.0536690),
79
79
  tolerance: (~:".Machine").double__eps ** 0.5)).to eq true
80
80
  expect(x.logic[[2]]).to eq 0.5
81
81
  end
@@ -84,8 +84,9 @@ describe R::List do
84
84
  # sapply will return a matrix
85
85
  quant = R.sapply(@x, @q)
86
86
  expect(quant.rclass).to eq 'matrix'
87
-
88
- expect(quant[:all, 'a']).to eq R.c('0%': 1, '25%': 3.25, '50%': 5.50, '75%': 7.75, '100%': 10)
87
+ expect(quant[:all, 'a']).to eq R.c('0%': 1, '25,00000%': 3.25,
88
+ '50,00000%': 5.50, '75,00000%': 7.75,
89
+ '100,0000%': 10)
89
90
 
90
91
  expect(quant[3, 'beta']).to eq 1.0
91
92
  end
@@ -22,51 +22,55 @@
22
22
  ##########################################################################################
23
23
 
24
24
  require 'galaaz'
25
- require 'ggplot'
25
+ R.require 'stats'
26
+
27
+ # require 'ggplot'
26
28
 
27
29
  # Need to fix function 'str'... not printing anything
28
30
  # anymore
29
31
  # puts R.str(mtcars)
30
32
  # shouls allow mtcars[['car name']] = mtcars.rownames
31
33
 
32
- # Data Prep
33
- mtcars = ~:mtcars
34
+ # (~:mtcars).str
34
35
 
35
- mtcars.car_name = mtcars.rownames # create new column for car names
36
- mtcars.mpg_z = ((mtcars.mpg - mtcars.mpg.mean) / mtcars.mpg.sd).round 2
37
- mtcars.mpg_type = (mtcars.mpg_z < 0).ifelse('below', 'above')
38
- mtcars = mtcars[mtcars.mpg_z.order, :all]
39
- mtcars.car_name = R.factor(mtcars.car_name, levels: mtcars.car_name)
36
+ describe R::List do
37
+
38
+ context "The apply family of functions with lists" do
40
39
 
41
- puts mtcars.car_name
42
40
 
43
- R.awt
41
+ x = R.list(a: (1..10), beta: R.exp(-3..3), logic: R.c(true, false, false, true))
42
+ #puts x
44
43
 
45
- puts mtcars.ggplot(E.aes(x: :car_name, y: :mpg_z, label: :mpg_z)) +
46
- R.geom_bar(E.aes(fill: :mpg_type), stat: 'identity', width: 0.5) +
47
- R.scale_fill_manual(name: 'Mileage',
48
- labels: R.c('Above Average', 'Below Average'),
49
- values: R.c('above': '#00ba38', 'below': '#f8766d')) +
50
- R.labs(subtitle: "Normalised mileage from 'mtcars'",
51
- title: "Diverging Bars") +
52
- R.coord_flip
44
+ quant = R.lapply(x, ~:quantile)
45
+ #puts quant
46
+ #puts quant.a[['50,00000%']]
53
47
 
54
- sleep(3)
48
+ # puts quant.beta['100%']
55
49
 
50
+ it "ss" do
51
+
52
+ ret = R.all__equal(quant.beta["100%"],
53
+ R.c('100%': 20.08553692),
54
+ tolerance: (~:".Machine").double__eps ** 0.5)
55
+ expect(R.c(1, 2, 3)).to eq false
56
+ # expect(5 == 10).to eq true
57
+ end
58
+ end
59
+ end
56
60
 
57
61
  =begin
62
+ # Make @q the function R quantile
63
+
64
+ #puts quant.a
58
65
 
59
- mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`) # convert to factor to retain sorted order in plot.
66
+ # puts quant.a[["50%"]]
67
+ =end
60
68
 
61
- # Diverging Barcharts
62
- R.theme_set(R.theme_bw)
63
69
 
64
- ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) +
65
- geom_bar(stat='identity', aes(fill=mpg_type), width=.5) +
66
- scale_fill_manual(name="Mileage",
67
- labels = c("Above Average", "Below Average"),
68
- values = c("above"="#00ba38", "below"="#f8766d")) +
69
- labs(subtitle="Normalised mileage from 'mtcars'",
70
- title= "Diverging Bars") +
71
- coord_flip()
70
+ =begin
71
+ R::Support.eval(<<R)
72
+ x = list(a = (1:10), beta = exp(-3:3), logic = c(TRUE, FALSE, FALSE, TRUE))
73
+ quant = lapply(x, quantile)
74
+ print(quant)
75
+ R
72
76
  =end
data/version.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  $gem_name = "galaaz"
2
- $version="0.4.6"
2
+ $version="0.4.7"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galaaz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Botafogo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-21 00:00:00.000000000 Z
11
+ date: 2019-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -106,11 +106,18 @@ files:
106
106
  - blogs/galaaz_ggplot/midwest_external_png
107
107
  - blogs/galaaz_ggplot/scatter_plot.png
108
108
  - blogs/gknit/gknit.Rmd
109
+ - blogs/gknit/gknit.html
110
+ - blogs/gknit/gknit.md
111
+ - blogs/gknit/gknit_files/figure-html/bubble-1.png
112
+ - blogs/gknit/gknit_files/figure-html/diverging_bar.png
109
113
  - blogs/gknit/model.rb
110
114
  - blogs/manual/manual.Rmd
111
115
  - blogs/manual/manual.html
112
116
  - blogs/manual/manual.md
113
117
  - blogs/manual/manual_files/figure-html/diverging_bar.png
118
+ - blogs/nse_dplyr/nse_dplyr.Rmd
119
+ - blogs/nse_dplyr/nse_dplyr.html
120
+ - blogs/nse_dplyr/nse_dplyr.md
114
121
  - blogs/oh_my/not_so.rb
115
122
  - blogs/oh_my/oh_my.Rmd
116
123
  - blogs/oh_my/oh_my.html
@@ -269,8 +276,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
269
276
  - !ruby/object:Gem::Version
270
277
  version: '0'
271
278
  requirements: []
272
- rubyforge_project:
273
- rubygems_version: 2.6.14.1
279
+ rubygems_version: 3.0.3
274
280
  signing_key:
275
281
  specification_version: 4
276
282
  summary: Tightly coupling Ruby and R