galaaz 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +29 -0
  3. data/bin/gknit +208 -10
  4. data/bin/gknit2 +14 -0
  5. data/bin/gknit2~ +6 -0
  6. data/bin/prepareR.rb +3 -0
  7. data/bin/prepareR.rb~ +1 -0
  8. data/bin/tmp.py +51 -0
  9. data/blogs/dev/dev.Rmd +70 -0
  10. data/blogs/dev/dev.Rmd~ +104 -0
  11. data/blogs/dev/dev.html +209 -0
  12. data/blogs/dev/dev.md +72 -0
  13. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  14. data/blogs/dev/model.rb +41 -0
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +55 -27
  16. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +44 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot.dvi +0 -0
  18. data/blogs/galaaz_ggplot/galaaz_ggplot.html +17 -4
  19. data/blogs/galaaz_ggplot/galaaz_ggplot.out +10 -0
  20. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  21. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +630 -0
  22. data/blogs/galaaz_ggplot/midwest.Rmd +1 -1
  23. data/blogs/galaaz_ggplot/midwest_external_png +13 -0
  24. data/blogs/galaaz_ggplot/midwest_external_png~ +1 -0
  25. data/blogs/gknit/gknit.Rmd +500 -0
  26. data/blogs/gknit/gknit.Rmd~ +184 -0
  27. data/blogs/gknit/gknit.Rnd~ +17 -0
  28. data/blogs/gknit/gknit.html +528 -0
  29. data/blogs/gknit/gknit.md +628 -0
  30. data/blogs/gknit/gknit.pdf +0 -0
  31. data/blogs/gknit/gknit.tex +745 -0
  32. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  33. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  34. data/blogs/gknit/model.rb +41 -0
  35. data/blogs/gknit/model.rb~ +46 -0
  36. data/blogs/ruby_plot/figures/dose_len.png +0 -0
  37. data/blogs/ruby_plot/figures/facet_by_delivery.png +0 -0
  38. data/blogs/ruby_plot/figures/facet_by_dose.png +0 -0
  39. data/blogs/ruby_plot/figures/facets_by_delivery_color.png +0 -0
  40. data/blogs/ruby_plot/figures/facets_by_delivery_color2.png +0 -0
  41. data/blogs/ruby_plot/figures/facets_with_decorations.png +0 -0
  42. data/blogs/ruby_plot/figures/facets_with_jitter.png +0 -0
  43. data/blogs/ruby_plot/figures/facets_with_points.png +0 -0
  44. data/blogs/ruby_plot/figures/final_box_plot.png +0 -0
  45. data/blogs/ruby_plot/figures/final_violin_plot.png +0 -0
  46. data/blogs/ruby_plot/figures/violin_with_jitter.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot.Rmd +680 -0
  48. data/blogs/ruby_plot/ruby_plot.Rmd~ +215 -0
  49. data/blogs/ruby_plot/ruby_plot.html +563 -0
  50. data/blogs/ruby_plot/ruby_plot.md +731 -0
  51. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  52. data/blogs/ruby_plot/ruby_plot.tex +458 -0
  53. data/examples/sthda_ggplot/all.rb +0 -6
  54. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +1 -1
  55. data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +1 -1
  56. data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +2 -2
  57. data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +0 -1
  58. data/lib/R/eng_ruby.R +62 -0
  59. data/lib/R/eng_ruby.R~ +63 -0
  60. data/lib/R_interface/capture_plot.rb~ +23 -0
  61. data/lib/{R → R_interface}/expression.rb +0 -0
  62. data/lib/{R → R_interface}/r.rb +10 -1
  63. data/lib/{R → R_interface}/r.rb~ +0 -0
  64. data/lib/{R → R_interface}/r_methods.rb +21 -5
  65. data/lib/{R → R_interface}/rbinary_operators.rb +6 -1
  66. data/lib/R_interface/rclosure.rb +38 -0
  67. data/lib/{R → R_interface}/rdata_frame.rb +0 -0
  68. data/lib/R_interface/rdevices.R +31 -0
  69. data/lib/R_interface/rdevices.rb +225 -0
  70. data/lib/{R/rclosure.rb → R_interface/rdevices.rb~} +3 -10
  71. data/lib/{R → R_interface}/renvironment.rb +0 -0
  72. data/lib/{R → R_interface}/rexpression.rb +0 -0
  73. data/lib/{R → R_interface}/rindexed_object.rb +0 -0
  74. data/lib/{R → R_interface}/rlanguage.rb +0 -0
  75. data/lib/{R → R_interface}/rlist.rb +0 -0
  76. data/lib/{R → R_interface}/rmatrix.rb +0 -0
  77. data/lib/{R → R_interface}/rmd_indexed_object.rb +0 -0
  78. data/lib/{R → R_interface}/robject.rb +5 -0
  79. data/lib/{R → R_interface}/rpkg.rb +0 -0
  80. data/lib/{R → R_interface}/rsupport.rb +49 -13
  81. data/lib/{R → R_interface}/rsupport_scope.rb +0 -0
  82. data/lib/{R → R_interface}/rsymbol.rb +1 -0
  83. data/lib/{R → R_interface}/ruby_callback.rb +0 -0
  84. data/lib/{R → R_interface}/ruby_extensions.rb +2 -1
  85. data/lib/{R → R_interface}/runary_operators.rb +0 -0
  86. data/lib/{R → R_interface}/rvector.rb +0 -0
  87. data/lib/galaaz.rb +4 -2
  88. data/lib/gknit.rb +27 -0
  89. data/lib/gknit.rb~ +26 -0
  90. data/lib/gknit/knitr_engine.rb +120 -0
  91. data/lib/gknit/knitr_engine.rb~ +102 -0
  92. data/lib/gknit/ruby_engine.rb +70 -0
  93. data/lib/gknit/ruby_engine.rb~ +72 -0
  94. data/lib/util/exec_ruby.rb +8 -7
  95. data/lib/util/inline_file.rb +70 -0
  96. data/lib/util/inline_file.rb~ +23 -0
  97. data/r_requires/ggplot.rb +1 -8
  98. data/r_requires/knitr.rb +27 -0
  99. data/r_requires/knitr.rb~ +4 -0
  100. data/specs/r_language.spec.rb +22 -0
  101. data/specs/r_plots.spec.rb +72 -0
  102. data/specs/r_plots.spec.rb~ +37 -0
  103. data/specs/tmp.rb +255 -1
  104. data/version.rb +1 -1
  105. metadata +89 -39
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64681d29dd707019a0461ac473194ade6accfb56
4
- data.tar.gz: 34f0d93c32e4c3f2deb6a15ff8d02ba684a28396
3
+ metadata.gz: 96f1e6bcd6650786b9e33356b100bec04177400c
4
+ data.tar.gz: e66eed26b93a6cb4eae16212027319a269af8ccb
5
5
  SHA512:
6
- metadata.gz: 1651b248d716539fcb2892b0398de6190db001fc02656cca187422b9f9bf10b6ac93486033058874cd87e910f71abe9350816563a4e762d0ee4f5a8887eccc68
7
- data.tar.gz: d0599bd0afb5602fd3be61b97ed1a46a0b72c87179a1a8a9b081ce135bc60896387e9ad9287bfbf133d9a14a6e4fdb3478aaa2f583c94ee4f478882cfea29404
6
+ metadata.gz: db82f43785fd763ff93d0f494d284536c3041fa0b68a69295192d3074b328fe6098b484e940d2c2de3a5eef2e63ef72b435b9442bf7685f527a565d0c1fcbe16
7
+ data.tar.gz: 63943f74c540a1dd96f48dbd874c20849f0a7fc92bb1f8eea643f8973c3920e19f061a11792f74c6054e8f3617ccb90409cbab6faf8a784f0ab9b4c49d6c4367
data/Rakefile CHANGED
@@ -79,6 +79,7 @@ specs = FileList['specs/**/*.rb']
79
79
  master_list = FileList['examples/50Plots_MasterList/**/*.rb']
80
80
  islr = FileList['examples/islr/**/*.rb']
81
81
  misc = FileList['examples/misc/**/*.rb']
82
+ blogs = Dir.entries("blogs")
82
83
 
83
84
  #===========================================================================================
84
85
  # Creates tasks for all specs.
@@ -147,6 +148,34 @@ end
147
148
 
148
149
  task :default => "sthda:all"
149
150
 
151
+ #===========================================================================================
152
+ # Creates task for running gknit
153
+ #===========================================================================================
154
+
155
+ blogs.each do |dir|
156
+ next if dir == '.' || dir == '..'
157
+ blog_rmd = "blogs/#{dir}/#{dir}.Rmd"
158
+ desc "run gknit to build #{dir} blog post"
159
+ task "blog:#{dir}" do
160
+ (sh %{ bin/gknit #{blog_rmd} })
161
+ end
162
+
163
+ desc "run gknit2 to build #{dir} blog post"
164
+ task "blog2:#{dir}" do
165
+ (sh %{ bin/gknit2 #{blog_rmd} })
166
+ end
167
+
168
+ end
169
+
170
+ #===========================================================================================
171
+ # Loads R and require libraries to run Galaaz
172
+ #===========================================================================================
173
+
174
+ desc 'Prepare R for running'
175
+ task :make_r do
176
+ (sh %{ gu install r })
177
+ end
178
+
150
179
 
151
180
  =begin
152
181
  name = "#{$gem_name}-#{$version}.gem"
data/bin/gknit CHANGED
@@ -1,26 +1,224 @@
1
1
  #!/usr/bin/env ruby
2
+ # coding: utf-8
2
3
 
3
4
  dir = Dir.pwd
4
5
  puts "Knitting #{dir}/#{ARGV[0]}"
5
6
 
6
- req_gal = "require '../lib/galaaz'"
7
-
8
7
  code = <<EOF
9
- library('knitr');
10
- library('rmarkdown');
8
+ eval.polyglot("ruby", "$LOAD_PATH.unshift %q(/home/rbotafogo/desenv/galaaz/lib)")
9
+ eval.polyglot("ruby", "require %q(galaaz)")
10
+ eval.polyglot("ruby", "R.install_and_loads %q(knitr)")
11
+ eval.polyglot("ruby", "R.install_and_loads %q(rmarkdown)")
12
+
13
+ # graphics devices in base R, plus those in Cairo, cairoDevice, tikzDevice
14
+ auto_exts = c(
15
+ bmp = "bmp", postscript = "eps", pdf = "pdf", png = "png", svg = "svg",
16
+ jpeg = "jpeg", pictex = "tex", tiff = "tiff", win.metafile = "wmf",
17
+ cairo_pdf = "pdf", cairo_ps = "eps",
18
+
19
+ quartz_pdf = "pdf", quartz_png = "png", quartz_jpeg = "jpeg",
20
+ quartz_tiff = "tiff", quartz_gif = "gif", quartz_psd = "psd",
21
+ quartz_bmp = "bmp",
22
+
23
+ CairoJPEG = "jpeg", CairoPNG = "png", CairoPS = "eps", CairoPDF = "pdf",
24
+ CairoSVG = "svg", CairoTIFF = "tiff",
25
+
26
+ Cairo_pdf = "pdf", Cairo_png = "png", Cairo_ps = "eps", Cairo_svg = "svg",
27
+
28
+ svglite = "svg",
29
+
30
+ tikz = "tex"
31
+ )
32
+
33
+ is_plot_output = function(x) {
34
+ evaluate::is.recordedplot(x) || inherits(x, "knit_image_paths")
35
+ }
36
+
37
+ # find recorded plots in the output of evaluate()
38
+ find_recordedplot = function(x) {
39
+ vapply(x, is_plot_output, logical(1))
40
+ }
41
+
42
+ dev2ext = function(x) {
43
+ res = auto_exts[x]
44
+ if (any(idx <- is.na(res))) {
45
+ for (i in x[idx]) check_dev(i)
46
+ stop2(
47
+ "cannot find appropriate filename extensions for device ", x[idx], "; ",
48
+ "please use chunk option 'fig.ext' (https://yihui.name/knitr/options)"
49
+ )
50
+ }
51
+ unname(res)
52
+ }
53
+
54
+ # filter the dev.args option
55
+ get_dargs = function(dargs, dev) {
56
+ if (length(dargs) == 0) return()
57
+ if (is.list(dargs) && all(sapply(dargs, is.list))) {
58
+ # dev.args is list(dev1 = list(arg1 = val1, ...), dev2 = list(arg2, ...))
59
+ dargs = dargs[[dev]]
60
+ }
61
+ dargs
62
+ }
63
+
64
+ # open a device for a chunk; depending on the option global.device, may or may
65
+ # not need to close the device on exit
66
+ chunk_device = function(
67
+ width, height, record = TRUE, dev, dev.args, dpi, options, tmp = tempfile()
68
+ ) {
69
+ dev_new = function() {
70
+ # actually I should adjust the recording device according to dev, but here I
71
+ # have only considered the png and tikz devices (because the measurement
72
+ # results can be very different especially with the latter, see #1066), and
73
+ # also the cairo_pdf device (#1235)
74
+ if (identical(dev, "png")) {
75
+ do.call(grDevices::png, c(list(
76
+ filename = tmp, width = width, height = height, units = "in", res = dpi
77
+ ), get_dargs(dev.args, "png")))
78
+ } else if (identical(dev, "tikz")) {
79
+ dargs = c(list(
80
+ file = tmp, width = width, height = height
81
+ ), get_dargs(dev.args, "tikz"))
82
+ dargs$sanitize = options$sanitize; dargs$standAlone = options$external
83
+ if (is.null(dargs$verbose)) dargs$verbose = FALSE
84
+ do.call(tikz_dev, dargs)
85
+ } else if (identical(dev, "cairo_pdf")) {
86
+ do.call(grDevices::cairo_pdf, c(list(
87
+ filename = tmp, width = width, height = height
88
+ ), get_dargs(dev.args, "cairo_pdf")))
89
+ } else if (identical(getOption("device"), "pdf_null")) {
90
+ if (!is.null(dev.args)) {
91
+ dev.args = get_dargs(dev.args, "pdf")
92
+ dev.args = dev.args[intersect(names(dev.args), c("pointsize", "bg"))]
93
+ }
94
+ do.call(pdf_null, c(list(width = width, height = height), dev.args))
95
+ } else dev.new(width = width, height = height)
96
+ }
97
+ if (!opts_knit$get("global.device")) {
98
+ dev_new()
99
+ dev.control(displaylist = if (record) "enable" else "inhibit") # enable recording
100
+ # if returns TRUE, we need to close this device after code is evaluated
101
+ return(TRUE)
102
+ } else if (is.null(dev.list())) {
103
+ # want to use a global device but not open yet
104
+ dev_new()
105
+ dev.control("enable")
106
+ }
107
+ FALSE
108
+ }
109
+
110
+ showtext = function(show) if (isTRUE(show)) showtext::showtext_begin()
11
111
 
12
- # define the galaaz engine for processing Ruby chunks in
112
+ # define the ruby engine for processing Ruby chunks in
13
113
  # rmarkdown
14
- eng_galaaz = function(options) {
114
+ eng_ruby = function(options) {
15
115
  block_code = paste(options$code, collapse = "\\n");
16
- code = paste0("GalaazUtil.exec_ruby(", shQuote(block_code), ")");
116
+ code = paste0("GalaazUtil.exec_ruby(",
117
+ shQuote(block_code),
118
+ ")
119
+ ");
120
+
121
+ # png(paste0("gknit_files/figure-html/", options$label, ".png"));
122
+
123
+ keep = options$fig.keep
124
+ keep.idx = NULL
125
+ if (is.numeric(keep)) {
126
+ keep.idx = keep
127
+ keep = "index"
128
+ }
129
+
130
+ tmp.fig = tempfile(); on.exit(unlink(tmp.fig), add = TRUE)
131
+
132
+ # open a device to record plots
133
+ if (chunk_device(options$fig.width[1L], options$fig.height[1L], keep != "none",
134
+ options$dev, options$dev.args, options$dpi, options, tmp.fig)) {
135
+ # preserve par() settings from the last code chunk
136
+ if (keep.pars <- opts_knit$get("global.par"))
137
+ par2(opts_knit$get("global.pars"))
138
+ showtext(options$fig.showtext) # showtext support
139
+ dv = dev.cur()
140
+ on.exit({
141
+ if (keep.pars) opts_knit$set(global.pars = par(no.readonly = TRUE))
142
+ dev.off(dv)
143
+ }, add = TRUE)
144
+ }
145
+
146
+ # guess plot file type if it is NULL
147
+ # if (keep != "none" && is.null(options$fig.ext))
148
+ # options$fig.ext = dev2ext(options$dev)
149
+
150
+ # out = eval.polyglot("ruby", code);
151
+ res = eval.polyglot("ruby", code);
152
+
153
+ # rearrange locations of figures
154
+ # figs = find_recordedplot(res)
155
+ # if (length(figs) && any(figs)) {
156
+ # if (keep == 'none') {
157
+ # res = res[!figs] # remove all
158
+ # } else {
159
+ # if (options$fig.show == 'hold') res = c(res[!figs], res[figs]) # move to the end
160
+ # figs = find_recordedplot(res)
161
+ # if (length(figs) && sum(figs) > 1) {
162
+ # if (keep %in% c('first', 'last')) {
163
+ # res = res[-(if (keep == 'last') head else tail)(which(figs), -1L)]
164
+ # } else {
165
+ # # keep only selected
166
+ # if (keep == 'index') res = res[which(figs)[keep.idx]]
167
+ # # merge low-level plotting changes
168
+ # if (keep == 'high') res = merge_low_plot(res, figs)
169
+ # }
170
+ # }
171
+ # }
172
+ #}
173
+ engine_output(options, block_code, res)
174
+ }
175
+
176
+ knit_engines$set(ruby = eng_ruby)
177
+
178
+ # define the rb engine for inline Ruby code
179
+ eng_rb = function(options) {
180
+ code = paste0("GalaazUtil.exec_ruby(",
181
+ shQuote(options$label),
182
+ ")
183
+ ");
184
+ out = eval.polyglot("ruby", code)
185
+ return(out)
186
+ }
187
+
188
+ knit_engines$set(rb = eng_rb)
189
+
190
+ # define the include engine for including Ruby files
191
+ # in place
192
+
193
+ eng_include = function(options) {
194
+ if (is.null(options$relative) | isTRUE(options$relative)) {
195
+ req = "require_relative "
196
+ } else {
197
+ req = "require "
198
+ }
199
+
200
+ # read the file and store it to output in the code block
201
+ code = paste0("GalaazUtil.inline_file(",
202
+ shQuote(options$label),
203
+ ", ",
204
+ shQuote(req),
205
+ ")
206
+ ");
207
+ include = eval.polyglot("ruby", code);
208
+
209
+ # require the file
210
+ require = paste0(req, shQuote(options$label));
211
+ code = paste0("GalaazUtil.exec_ruby(",
212
+ shQuote(require),
213
+ ")
214
+ ");
215
+
17
216
  out = eval.polyglot("ruby", code);
18
- engine_output(options, block_code, out)
217
+ engine_output(options, include, out)
19
218
  }
20
219
 
21
- knit_engines$set(galaaz = eng_galaaz)
220
+ knit_engines$set(include = eng_include)
22
221
 
23
- eval.polyglot("ruby", "require %q(galaaz)")
24
222
 
25
223
  rmarkdown::render("#{dir}/#{ARGV[0]}")
26
224
  EOF
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ dir = Dir.pwd
5
+ puts "Knitting #{dir}/#{ARGV[0]}"
6
+
7
+ code = <<EOF
8
+ eval.polyglot("ruby", "$LOAD_PATH.unshift %q(/home/rbotafogo/desenv/galaaz/lib)")
9
+ eval.polyglot("ruby", "require %q(gknit)")
10
+
11
+ rmarkdown::render("#{dir}/#{ARGV[0]}")
12
+ EOF
13
+
14
+ exec "Rscript --jvm --polyglot -e '#{code}'"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ dir = Dir.pwd
5
+ puts "Knitting #{dir}/#{ARGV[0]}"
6
+
@@ -0,0 +1,3 @@
1
+ # gu install r
2
+ # configure r
3
+ R.install_and_loads('knitr')
@@ -0,0 +1 @@
1
+ R.install_and_loads('knitr')
@@ -0,0 +1,51 @@
1
+ eval.polyglot("python", code);
2
+ code = paste0("out = sys.stdout.getvalue()",
3
+ "sys.stdout.close()",
4
+ "sys.stdout = backup",
5
+ "out"
6
+ )
7
+
8
+
9
+
10
+
11
+
12
+
13
+ import sys
14
+ from cStringIO import StringIO
15
+
16
+ # setup the environment
17
+ backup = sys.stdout
18
+
19
+ # ####
20
+ sys.stdout = StringIO() # capture output
21
+ write()
22
+ out = sys.stdout.getvalue() # release output
23
+ # ####
24
+
25
+ sys.stdout.close() # close the stream
26
+ sys.stdout = backup # restore original stdout
27
+
28
+ print out.upper() # post processing
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+ code = paste0("from contextlib import contextmanager",
37
+ "@contextmanager",
38
+ "def stdout_redirector(stream):",
39
+ " old_stdout = sys.stdout",
40
+ " sys.stdout = stream",
41
+ " try:",
42
+ " yield",
43
+ " finally:",
44
+ " sys.stdout = old_stdout",
45
+ "f = io.StringIO()",
46
+ "with stdout_redirector(f):",
47
+ " exec(",
48
+ shQuote(block_code),
49
+ ")",
50
+ "f.getvalue()"
51
+ ");
@@ -0,0 +1,70 @@
1
+ ---
2
+ title: "gKnit - Ruby and R Knitting with Galaaz in GraalVM"
3
+ author: "Rodrigo Botafogo"
4
+ tags: [Galaaz, Ruby, R, TruffleRuby, FastR, GraalVM, knitr]
5
+ date: "19 October 2018"
6
+ output:
7
+ html_document:
8
+ self_contained: true
9
+ keep_md: true
10
+ pdf_document:
11
+ includes:
12
+ in_header: ["../../sty/galaaz.sty"]
13
+ number_sections: yes
14
+ ---
15
+
16
+ ```{r setup, echo=FALSE}
17
+
18
+ ```
19
+
20
+ # Introduction
21
+
22
+
23
+ ```{r bubble}
24
+ # load package and data
25
+ library(ggplot2)
26
+ data(mpg, package="ggplot2")
27
+ # mpg <- read.csv("http://goo.gl/uEeRGu")
28
+
29
+ mpg_select <- mpg[mpg$manufacturer %in% c("audi", "ford", "honda", "hyundai"), ]
30
+
31
+ # Scatterplot
32
+ theme_set(theme_bw()) # pre-set the bw theme.
33
+ g <- ggplot(mpg_select, aes(displ, cty)) +
34
+ labs(subtitle="mpg: Displacement vs City Mileage",
35
+ title="Bubble chart")
36
+
37
+ g + geom_jitter(aes(col=manufacturer, size=hwy)) +
38
+ geom_smooth(aes(col=manufacturer), method="lm", se=F)
39
+ ```
40
+
41
+ ### Plotting
42
+
43
+ ```{ruby diverging_bar}
44
+ require 'ggplot'
45
+
46
+ R.theme_set R.theme_bw
47
+
48
+ # Data Prep
49
+ mtcars = ~:mtcars
50
+ mtcars.car_name = R.rownames(:mtcars)
51
+ # compute normalized mpg
52
+ mtcars.mpg_z = ((mtcars.mpg - mtcars.mpg.mean)/mtcars.mpg.sd).round 2
53
+ mtcars.mpg_type = mtcars.mpg_z < 0 ? "below" : "above"
54
+ mtcars = mtcars[mtcars.mpg_z.order, :all]
55
+ # convert to factor to retain sorted order in plot
56
+ mtcars.car_name = mtcars.car_name.factor levels: mtcars.car_name
57
+
58
+ # Diverging Barcharts
59
+ gg = mtcars.ggplot(E.aes(x: :car_name, y: :mpg_z, label: :mpg_z)) +
60
+ R.geom_bar(E.aes(fill: :mpg_type), stat: 'identity', width: 0.5) +
61
+ R.scale_fill_manual(name: "Mileage",
62
+ labels: R.c("Above Average", "Below Average"),
63
+ values: R.c("above": "#00ba38", "below": "#f8766d")) +
64
+ R.labs(subtitle: "Normalised mileage from 'mtcars'",
65
+ title: "Diverging Bars") +
66
+ R.coord_flip()
67
+
68
+ print gg
69
+ ```
70
+
@@ -0,0 +1,104 @@
1
+ ---
2
+ title: "gKnit - Ruby and R Knitting with Galaaz in GraalVM"
3
+ author: "Rodrigo Botafogo"
4
+ tags: [Galaaz, Ruby, R, TruffleRuby, FastR, GraalVM, knitr]
5
+ date: "19 October 2018"
6
+ output:
7
+ html_document:
8
+ self_contained: true
9
+ keep_md: true
10
+ pdf_document:
11
+ includes:
12
+ in_header: ["../../sty/galaaz.sty"]
13
+ number_sections: yes
14
+ ---
15
+
16
+ ```{r setup, echo=FALSE}
17
+
18
+ ```
19
+
20
+ # Introduction
21
+
22
+
23
+ ```{r bubble}
24
+ # load package and data
25
+ library(ggplot2)
26
+ data(mpg, package="ggplot2")
27
+ # mpg <- read.csv("http://goo.gl/uEeRGu")
28
+
29
+ mpg_select <- mpg[mpg$manufacturer %in% c("audi", "ford", "honda", "hyundai"), ]
30
+
31
+ # Scatterplot
32
+ theme_set(theme_bw()) # pre-set the bw theme.
33
+ g <- ggplot(mpg_select, aes(displ, cty)) +
34
+ labs(subtitle="mpg: Displacement vs City Mileage",
35
+ title="Bubble chart")
36
+
37
+ g + geom_jitter(aes(col=manufacturer, size=hwy)) +
38
+ geom_smooth(aes(col=manufacturer), method="lm", se=F)
39
+ ```
40
+
41
+ ### Plotting
42
+
43
+ ```{ruby diverging_bar}
44
+ require 'ggplot'
45
+
46
+ R.theme_set R.theme_bw
47
+
48
+ # Data Prep
49
+ mtcars = ~:mtcars
50
+ mtcars.car_name = R.rownames(:mtcars)
51
+ # compute normalized mpg
52
+ mtcars.mpg_z = ((mtcars.mpg - mtcars.mpg.mean)/mtcars.mpg.sd).round 2
53
+ mtcars.mpg_type = mtcars.mpg_z < 0 ? "below" : "above"
54
+ mtcars = mtcars[mtcars.mpg_z.order, :all]
55
+ # convert to factor to retain sorted order in plot
56
+ mtcars.car_name = mtcars.car_name.factor levels: mtcars.car_name
57
+
58
+ # Diverging Barcharts
59
+ gg = mtcars.ggplot(E.aes(x: :car_name, y: :mpg_z, label: :mpg_z)) +
60
+ R.geom_bar(E.aes(fill: :mpg_type), stat: 'identity', width: 0.5) +
61
+ R.scale_fill_manual(name: "Mileage",
62
+ labels: R.c("Above Average", "Below Average"),
63
+ values: R.c("above": "#00ba38", "below": "#f8766d")) +
64
+ R.labs(subtitle: "Normalised mileage from 'mtcars'",
65
+ title: "Diverging Bars") +
66
+ R.coord_flip()
67
+
68
+ print gg
69
+ ```
70
+
71
+ ### Plotting with keep value
72
+
73
+ ```{ruby diverging_bar2, fig.keep = 1, fig.showtext = TRUE}
74
+ require 'ggplot'
75
+
76
+ R.theme_set R.theme_bw
77
+
78
+ # Data Prep
79
+ mtcars = ~:mtcars
80
+ mtcars.car_name = R.rownames(:mtcars)
81
+ # compute normalized mpg
82
+ mtcars.mpg_z = ((mtcars.mpg - mtcars.mpg.mean)/mtcars.mpg.sd).round 2
83
+ mtcars.mpg_type = mtcars.mpg_z < 0 ? "below" : "above"
84
+ mtcars = mtcars[mtcars.mpg_z.order, :all]
85
+ # convert to factor to retain sorted order in plot
86
+ mtcars.car_name = mtcars.car_name.factor levels: mtcars.car_name
87
+
88
+ # Diverging Barcharts
89
+ # R.png
90
+ gg = mtcars.ggplot(E.aes(x: :car_name, y: :mpg_z, label: :mpg_z)) +
91
+ R.geom_bar(E.aes(fill: :mpg_type), stat: 'identity', width: 0.5) +
92
+ R.scale_fill_manual(name: "Mileage",
93
+ labels: R.c("Above Average", "Below Average"),
94
+ values: R.c("above": "#00ba38", "below": "#f8766d")) +
95
+ R.labs(subtitle: "Normalised mileage from 'mtcars'",
96
+ title: "Diverging Bars") +
97
+ R.coord_flip()
98
+
99
+ # print gg
100
+
101
+
102
+ # R.dev__off
103
+ # R.include_graphics("Rplot001.png")
104
+ ```