galaaz 0.4.10 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2048 -531
  3. data/Rakefile +3 -2
  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.html +10 -195
  13. data/blogs/galaaz_ggplot/galaaz_ggplot.md +404 -0
  14. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
  15. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
  16. data/blogs/gknit/gknit.Rmd +5 -3
  17. data/blogs/gknit/gknit.pdf +0 -0
  18. data/blogs/gknit/lst.rds +0 -0
  19. data/blogs/manual/lst.rds +0 -0
  20. data/blogs/manual/manual.Rmd +826 -53
  21. data/blogs/manual/manual.html +2338 -695
  22. data/blogs/manual/manual.md +2032 -539
  23. data/blogs/manual/manual.pdf +0 -0
  24. data/blogs/manual/manual.tex +1804 -594
  25. data/blogs/manual/manual_files/figure-html/bubble-1.png +0 -0
  26. data/blogs/manual/manual_files/figure-html/diverging_bar.png +0 -0
  27. data/blogs/manual/manual_files/figure-latex/bubble-1.png +0 -0
  28. data/blogs/manual/manual_files/figure-latex/diverging_bar.pdf +0 -0
  29. data/blogs/manual/model.rb +41 -0
  30. data/blogs/nse_dplyr/nse_dplyr.Rmd +226 -73
  31. data/blogs/nse_dplyr/nse_dplyr.html +254 -336
  32. data/blogs/nse_dplyr/nse_dplyr.md +353 -158
  33. data/blogs/oh_my/oh_my.html +274 -386
  34. data/blogs/oh_my/oh_my.md +208 -205
  35. data/blogs/ruby_plot/ruby_plot.html +20 -205
  36. data/blogs/ruby_plot/ruby_plot.md +14 -15
  37. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
  38. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
  39. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
  40. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
  41. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
  42. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_decorations.png +0 -0
  43. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
  44. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
  45. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
  46. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
  48. data/examples/Bibliography/master.bib +50 -0
  49. data/examples/Bibliography/stats.bib +72 -0
  50. data/examples/islr/x_y_rnorm.jpg +0 -0
  51. data/examples/latex_templates/Test-acm_article/Makefile +16 -0
  52. data/examples/latex_templates/Test-acm_article/Test-acm_article.Rmd +65 -0
  53. data/examples/latex_templates/Test-acm_article/acm_proc_article-sp.cls +1670 -0
  54. data/examples/latex_templates/Test-acm_article/sensys-abstract.cls +703 -0
  55. data/examples/latex_templates/Test-acm_article/sigproc.bib +59 -0
  56. data/examples/latex_templates/Test-acs_article/Test-acs_article.Rmd +260 -0
  57. data/examples/latex_templates/Test-acs_article/Test-acs_article.pdf +0 -0
  58. data/examples/latex_templates/Test-acs_article/acs-Test-acs_article.bib +11 -0
  59. data/examples/latex_templates/Test-acs_article/acs-my_output.bib +11 -0
  60. data/examples/latex_templates/Test-acs_article/acstest.bib +17 -0
  61. data/examples/latex_templates/Test-aea_article/AEA.cls +1414 -0
  62. data/{blogs/gknit/marshal.dump → examples/latex_templates/Test-aea_article/BibFile.bib} +0 -0
  63. data/examples/latex_templates/Test-aea_article/Test-aea_article.Rmd +108 -0
  64. data/examples/latex_templates/Test-aea_article/Test-aea_article.pdf +0 -0
  65. data/examples/latex_templates/Test-aea_article/aea.bst +1269 -0
  66. data/examples/latex_templates/Test-aea_article/multicol.sty +853 -0
  67. data/examples/latex_templates/Test-aea_article/references.bib +0 -0
  68. data/examples/latex_templates/Test-aea_article/setspace.sty +546 -0
  69. data/examples/latex_templates/Test-amq_article/Test-amq_article.Rmd +256 -0
  70. data/examples/latex_templates/Test-amq_article/Test-amq_article.pdf +0 -0
  71. data/examples/latex_templates/Test-amq_article/Test-amq_article.pdfsync +3397 -0
  72. data/examples/latex_templates/Test-amq_article/pics/Figure2.pdf +0 -0
  73. data/examples/latex_templates/Test-ams_article/Test-ams_article.Rmd +215 -0
  74. data/examples/latex_templates/Test-ams_article/amstest.bib +436 -0
  75. data/examples/latex_templates/Test-asa_article/Test-asa_article.Rmd +153 -0
  76. data/examples/latex_templates/Test-asa_article/Test-asa_article.pdf +0 -0
  77. data/examples/latex_templates/Test-asa_article/agsm.bst +1353 -0
  78. data/examples/latex_templates/Test-asa_article/bibliography.bib +233 -0
  79. data/examples/latex_templates/Test-ieee_article/IEEEtran.bst +2409 -0
  80. data/examples/latex_templates/Test-ieee_article/IEEEtran.cls +6346 -0
  81. data/examples/latex_templates/Test-ieee_article/Test-ieee_article.Rmd +175 -0
  82. data/examples/latex_templates/Test-ieee_article/Test-ieee_article.pdf +0 -0
  83. data/examples/latex_templates/Test-ieee_article/mybibfile.bib +20 -0
  84. data/examples/latex_templates/Test-rjournal_article/RJournal.sty +335 -0
  85. data/examples/latex_templates/Test-rjournal_article/RJreferences.bib +18 -0
  86. data/examples/latex_templates/Test-rjournal_article/RJwrapper.pdf +0 -0
  87. data/examples/latex_templates/Test-rjournal_article/Test-rjournal_article.Rmd +52 -0
  88. data/examples/latex_templates/Test-springer_article/Test-springer_article.Rmd +65 -0
  89. data/examples/latex_templates/Test-springer_article/Test-springer_article.pdf +0 -0
  90. data/examples/latex_templates/Test-springer_article/bibliography.bib +26 -0
  91. data/examples/latex_templates/Test-springer_article/spbasic.bst +1658 -0
  92. data/examples/latex_templates/Test-springer_article/spmpsci.bst +1512 -0
  93. data/examples/latex_templates/Test-springer_article/spphys.bst +1443 -0
  94. data/examples/latex_templates/Test-springer_article/svglov3.clo +113 -0
  95. data/examples/latex_templates/Test-springer_article/svjour3.cls +1431 -0
  96. data/examples/rmarkdown/svm-rmarkdown-anon-ms-example/svm-rmarkdown-anon-ms-example.Rmd +73 -0
  97. data/examples/rmarkdown/svm-rmarkdown-anon-ms-example/svm-rmarkdown-anon-ms-example.pdf +0 -0
  98. data/examples/rmarkdown/svm-rmarkdown-article-example/svm-rmarkdown-article-example.Rmd +382 -0
  99. data/examples/rmarkdown/svm-rmarkdown-article-example/svm-rmarkdown-article-example.pdf +0 -0
  100. data/examples/rmarkdown/svm-rmarkdown-beamer-example/svm-rmarkdown-beamer-example.Rmd +164 -0
  101. data/examples/rmarkdown/svm-rmarkdown-beamer-example/svm-rmarkdown-beamer-example.pdf +0 -0
  102. data/examples/rmarkdown/svm-rmarkdown-cv/svm-rmarkdown-cv.Rmd +92 -0
  103. data/examples/rmarkdown/svm-rmarkdown-cv/svm-rmarkdown-cv.pdf +0 -0
  104. data/examples/rmarkdown/svm-rmarkdown-syllabus-example/attend-grade-relationships.csv +482 -0
  105. data/examples/rmarkdown/svm-rmarkdown-syllabus-example/svm-rmarkdown-syllabus-example.Rmd +280 -0
  106. data/examples/rmarkdown/svm-rmarkdown-syllabus-example/svm-rmarkdown-syllabus-example.pdf +0 -0
  107. data/examples/rmarkdown/svm-xaringan-example/svm-xaringan-example.Rmd +386 -0
  108. data/lib/R_interface/r.rb +1 -1
  109. data/lib/R_interface/r_libs.R +1 -1
  110. data/lib/R_interface/r_methods.rb +10 -0
  111. data/lib/R_interface/rpkg.rb +1 -0
  112. data/lib/R_interface/rsupport.rb +4 -6
  113. data/lib/gknit.rb +2 -0
  114. data/lib/gknit/draft.rb +105 -0
  115. data/lib/gknit/knitr_engine.rb +0 -33
  116. data/lib/util/exec_ruby.rb +1 -27
  117. data/specs/figures/bg.jpeg +0 -0
  118. data/specs/figures/bg.png +0 -0
  119. data/specs/figures/dose_len.png +0 -0
  120. data/specs/figures/no_args.jpeg +0 -0
  121. data/specs/figures/no_args.png +0 -0
  122. data/specs/figures/width_height.jpeg +0 -0
  123. data/specs/figures/width_height.png +0 -0
  124. data/specs/figures/width_height_units1.jpeg +0 -0
  125. data/specs/figures/width_height_units1.png +0 -0
  126. data/specs/figures/width_height_units2.jpeg +0 -0
  127. data/specs/figures/width_height_units2.png +0 -0
  128. data/specs/r_dataframe.spec.rb +11 -11
  129. data/specs/ruby_expression.spec.rb +1 -0
  130. data/specs/tmp.rb +41 -20
  131. data/version.rb +1 -1
  132. metadata +73 -35
  133. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +0 -41
  134. data/blogs/galaaz_ggplot/galaaz_ggplot.out +0 -10
  135. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/midwest_rb.pdf +0 -0
  136. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/scatter_plot_rb.pdf +0 -0
  137. data/blogs/gknit/gknit.md +0 -1430
  138. data/blogs/gknit/gknit.tex +0 -1358
  139. data/blogs/manual/graph.rb +0 -29
  140. data/blogs/nse_dplyr/nse_dplyr.tex +0 -1373
  141. data/blogs/ruby_plot/ruby_plot.Rmd_external_figs +0 -662
  142. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.svg +0 -57
  143. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.svg +0 -106
  144. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.svg +0 -110
  145. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.svg +0 -174
  146. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.svg +0 -236
  147. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.svg +0 -296
  148. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.svg +0 -236
  149. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.svg +0 -218
  150. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.svg +0 -128
  151. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.svg +0 -150
  152. data/blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png +0 -0
  153. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  154. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  155. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  156. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  157. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  158. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  159. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  160. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  161. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  162. data/blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  163. data/examples/paper/paper.rb +0 -36
data/Rakefile CHANGED
@@ -35,7 +35,8 @@ class MakeTask < Rake::TaskLib
35
35
  # Create class variables for the polyglot options and libs
36
36
  # not yet possible to run native with R it seems...
37
37
  # @@polyglot_options = "--polyglot --experimental-options --single_threaded"
38
- @@polyglot_options = "--polyglot --jvm --experimental-options --single-threaded"
38
+ # @@polyglot_options = "--polyglot --jvm --experimental-options --single-threaded"
39
+ @@polyglot_options = "--polyglot --jvm"
39
40
  @@libs = "-Ilib/" # -Ir_requires/"
40
41
 
41
42
  #----------------------------------------------------------------------------------------
@@ -173,7 +174,7 @@ blogs.each do |dir|
173
174
  blog_rmd = "blogs/#{dir}/#{dir}.Rmd"
174
175
  desc "run gknit to build #{dir} blog post"
175
176
  task "blog:#{dir}" do
176
- (sh %{ bin/gknit #{blog_rmd} })
177
+ (sh %{ bin/gknit #{blog_rmd} } )
177
178
  end
178
179
 
179
180
  end
data/bin/gknit CHANGED
@@ -1,15 +1,161 @@
1
1
  #!/usr/bin/env ruby
2
2
  # coding: utf-8
3
3
 
4
+ require 'optionparser'
5
+
6
+ options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: gknit [options]"
10
+ opts.on("-f", "--filename FILE", "The input file to be rendered.
11
+ This value can also be provided without the '-f' or '--filename'
12
+ flag by just giving the name of the file as an input parameter.") do |opt|
13
+ options[:input_file] = opt
14
+ end
15
+ opts.on("--output_format OUT_FORMAT",
16
+ "The R Markdown output format to convert to. The option 'all'
17
+ will render all formats defined within the file. The option
18
+ can be the name of a format (e.g. 'html_document') and that
19
+ will render the document to that single format. One can also
20
+ use a vector of format names to render to multiple formats.
21
+ Alternatively, you can pass an output format object (e.g.
22
+ html_document()). If using NULL then the output format is the
23
+ first one defined in the YAML frontmatter in the input file
24
+ (this defaults to HTML if no format is specified there).") do |opt|
25
+ options[:output_format] = opt
26
+ end
27
+ opts.on("-o", "--output_file OUT_FILE", "The name of the output file.
28
+ If using nil then the output filename will be based on filename for
29
+ the input file. If a filename is provided, a path to the output file
30
+ can also be provided. Note that the output_dir option allows for
31
+ specifying the output file path as well, however, if also specifying
32
+ the path, the directory must exist.") do |opt|
33
+ options[:output_file] = opt
34
+ end
35
+ opts.on("-d", "--output_dir OUT_DIR", "The output directory for
36
+ the rendered output_file. This allows for a choice of an alternate
37
+ directory to which the output file should be written (the default
38
+ output directory is that of the input file). If a path is provided
39
+ with a filename in output_file the directory specified here will
40
+ take precedence. Please note that any directory path provided will
41
+ create any necessary directories if they do not exist.") do |opt|
42
+ options[:output_dir] = opt
43
+ end
44
+ opts.on("-a", "--output_options OUT_OPT", "List of output options
45
+ that can override the options specified in metadata (e.g. could be
46
+ used to force self_contained or mathjax = 'local'). Note that this
47
+ is only valid when the output format is read from metadata (i.e.
48
+ not a custom format object passed to output_format).") do |opt|
49
+ options[:output_options] = opt
50
+ end
51
+ opts.on("-i", "--intermediates_dir INTER_DIR", "FEATURE NOT YET
52
+ IMPLEMENTED! Intermediate files directory. If a path is specified
53
+ then intermediate files will be written to that path. If nil,
54
+ intermediate files are written to the same directory as the input
55
+ file.") do |opt|
56
+ options[:intermediates_dir] = opt
57
+ end
58
+ opts.on("-n", "--no_clean", "Will not clean intermediate files
59
+ that are created during rendering.") do |opt|
60
+ options[:clean] = opt
61
+ end
62
+ end.parse!
63
+
64
+ # Sets the directory
4
65
  dir = Dir.pwd
5
- puts "Knitting #{dir}/#{ARGV[0]}"
6
66
 
67
+ #=========================================================================================
68
+ # Check if the name of the file to gKnit was given and that it exists. If not,
69
+ # issue error.
70
+ #=========================================================================================
71
+
72
+ options[:input_file] = ARGV[0] if !ARGV[0].nil?
73
+ raise "Input file name is required for gKnitting. Add -f FILE or --input_file FILE. For help, type gknit -h" if
74
+ options[:input_file].nil?
75
+ options[:input_file] = "#{dir}/#{options[:input_file]}"
76
+ raise "File #{options[:input_file]} does not exist!" if !File.exist?(options[:input_file])
77
+
78
+ #=========================================================================================
79
+ # Checks the output_format option. If none, pass NULL to the render function. Output
80
+ # format needs to be quoted.
81
+ #=========================================================================================
82
+
83
+ options[:output_format] =
84
+ (options[:output_format].nil?)? "nil" : "\"#{options[:output_format]}\""
85
+
86
+ #=========================================================================================
87
+ # Checks the output_file option. If none, pass NULL to the render function. Output
88
+ # file name needs to be quoted.
89
+ #=========================================================================================
90
+
91
+ options[:output_file] =
92
+ (options[:output_file].nil?)? "nil" : "\"#{options[:output_file]}\""
93
+
94
+ #=========================================================================================
95
+ # Checks if an output directory was given. If given and directory does not exist,
96
+ # create the directory. If no directory given, pass NULL to the render function.
97
+ #=========================================================================================
98
+
99
+ if (!options[:output_dir].nil?)
100
+ Dir.mkdir(options[:output_dir]) if !Dir.exist?(options[:output_dir])
101
+ options[:output_dir] = "\"#{options[:output_dir]}\""
102
+ else
103
+ # If not, pass NULL to the render function
104
+ options[:output_dir] = "nil"
105
+ end
106
+
107
+ #=========================================================================================
108
+ # Checks if any intermediate directory given. If not, pass NULL to the render function
109
+ # Calling render with options intermediates_dir is not working, requires further study
110
+ #=========================================================================================
111
+
112
+ if (!options[:intermediates_dir].nil?)
113
+ if !Dir.exist?(options[:intermediates_dir])
114
+ Dir.mkdir(options[:intermediates_dir])
115
+ options[:intermediates_dir] = "\"#{options[:intermediates_dir]}\""
116
+ else
117
+ options[:intermediates_dir] = "\"#{options[:intermediates_dir]}\""
118
+ end
119
+ else
120
+ options[:intermediates_dir] = "nil"
121
+ end
122
+
123
+ options[:clean] = (options[:clean].nil?)? "true" : "false"
124
+
125
+ #=========================================================================================
126
+ # Sends message to user that the gKniting process has started
127
+ #=========================================================================================
128
+ puts "================================"
129
+ puts "gKnitting #{options[:input_file]} with the following options:"
130
+ puts " * Output format: \t #{options[:output_format]}"
131
+ puts " * Output file: \t #{options[:output_file]}"
132
+ puts " * Output directory: \t #{options[:output_dir]}"
133
+ puts " * Clean: \t\t #{options[:clean]}"
134
+
135
+
136
+ # Code to be evaluated by the polyglot Ruby script
7
137
  code = <<EOF
8
- eval.polyglot("ruby", "$LOAD_PATH.unshift %q(/home/rbotafogo/desenv/galaaz/lib)")
9
- eval.polyglot("ruby", "require %q(gknit)")
138
+ require %q(galaaz)
139
+ require %q(gknit)
10
140
 
11
- gknit_file = "#{dir}/#{ARGV[0]}"
12
- rmarkdown::render("#{dir}/#{ARGV[0]}")
141
+ R::Rmarkdown.render(
142
+ %q(#{options[:input_file]}),
143
+ output_format: #{options[:output_format]},
144
+ output_file: #{options[:output_file]},
145
+ output_dir: #{options[:output_dir]},
146
+ clean: #{options[:clean]}
147
+ )
13
148
  EOF
14
149
 
15
- exec "Rscript --jvm --polyglot -e '#{code}'"
150
+ libx = File.expand_path('../../lib', __FILE__)
151
+ exec "ruby --polyglot --jvm --experimental-options --single-threaded -I#{libx} -e '#{code}'"
152
+
153
+ =begin
154
+ # how to call rmarkdown::render with a parameter list
155
+ # all params need to be defined in the YAML header
156
+ rmarkdown::render("#{options[:input_file]}", output_format = #{options[:output_format]},
157
+ output_file = #{options[:output_file]},
158
+ output_dir = #{options[:output_dir]},
159
+ clean = #{options[:clean]},
160
+ params = list(rake_clean = TRUE))
161
+ =end
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ ##########################################################################################
5
+ # @author Rodrigo Botafogo
6
+ #
7
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
8
+ # and distribute this software and its documentation, without fee and without a signed
9
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
10
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
11
+ # distributions.
12
+ #
13
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
14
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
15
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
16
+ # POSSIBILITY OF SUCH DAMAGE.
17
+ #
18
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
20
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
21
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
22
+ # OR MODIFICATIONS.
23
+ ##########################################################################################
24
+
25
+ require 'optionparser'
26
+
27
+ options = {}
28
+
29
+ OptionParser.new do |opts|
30
+ opts.banner = "Usage: gknit-draft [options] [path]"
31
+ opts.on("-f", "--filename FILE", "Directory and filename to create") do |opt|
32
+ options[:file] = opt
33
+ end
34
+ opts.on("-t", "--template TEMPLATE", "Latex template to use") do |opt|
35
+ options[:template] = opt
36
+ end
37
+ opts.on("-p", "--package PACKAGE", "R Package where the template is defined") do |opt|
38
+ options[:package] = opt
39
+ end
40
+ opts.on("-g", "--gem RubyGem", "Ruby Gem where the template is defined") do |opt|
41
+ options[:gem] = opt
42
+ end
43
+ opts.on("-n", "--no_dir", "If a new directory should not be created") do |opt|
44
+ options[:no_dir] = opt
45
+ end
46
+ opts.on("-e", "--edit EDITOR", "Editor to use to edit the file") do |opt|
47
+ options[:editor] = opt
48
+ end
49
+ end.parse!
50
+
51
+ #=========================================================================================
52
+ # Only one of 'package' or 'gem' can be given
53
+ #=========================================================================================
54
+
55
+ raise "You should either specify an R package or Ruby Gem where the template is defined" if
56
+ !options[:package].nil? && !options[:gem].nil?
57
+
58
+ #=========================================================================================
59
+ # If gem given, then set it as the package name
60
+ #=========================================================================================
61
+
62
+ is_r_package = (options[:package].nil?)? false : true
63
+ options[:package] =
64
+ (!options[:gem].nil?)? "%Q(#{options[:gem]})" : "%Q(#{options[:package]})"
65
+
66
+ #=========================================================================================
67
+ #
68
+ #=========================================================================================
69
+
70
+ options[:no_dir] = (options[:no_dir].nil?)? true : false
71
+
72
+ #=========================================================================================
73
+ #
74
+ #=========================================================================================
75
+
76
+ Dir.chdir(File.dirname(File.expand_path('.', __FILE__)))
77
+
78
+ options[:file] = "%Q(#{options[:file]}-#{options[:template]})"
79
+
80
+ code = <<-Ruby
81
+ require 'gknit'
82
+ R.install_and_loads 'xfun'
83
+
84
+ GKnit.draft(file: #{options[:file]}, template: %Q(#{options[:template]}),
85
+ package: #{options[:package]}, create_dir: #{options[:no_dir]},
86
+ is_package: #{is_r_package}, edit: true)
87
+ Ruby
88
+
89
+ libx = File.expand_path('../../lib', __FILE__)
90
+ exec %Q(ruby --polyglot --jvm -I#{libx} -e "#{code}")
91
+
92
+ # exec %Q(ruby --polyglot --jvm --experimental-options --single-threaded -I../lib -e "#{code}")
93
+
94
+ =begin
95
+ puts "=========="
96
+ full_filename = Dir.glob("gknit-templates*", base: "#{Gem.default_dir}/gems")
97
+ template_path = "#{Gem.default_dir}/gems/#{full_filename[0]/rmarkdown/templates/}"
98
+ puts template_path
99
+ puts "=========="
100
+ =end
101
+
102
+ #puts %Q(ruby --polyglot --jvm --experimental-options --single-threaded -I../lib -e "#{code}")
103
+
104
+ #exec "ruby --polyglot --jvm --experimental-options --single-threaded \
105
+ #-I../lib -S ./gknit-draft.rb #{options[:file]} #{options[:template]} #{options[:package]} #{options[:create_#dir]} #{is_r_package} #{options[:editor]}"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ ##########################################################################################
3
+ # @author Rodrigo Botafogo
4
+ #
5
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
6
+ # and distribute this software and its documentation, without fee and without a signed
7
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
8
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
9
+ # distributions.
10
+ #
11
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
12
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
13
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
14
+ # POSSIBILITY OF SUCH DAMAGE.
15
+ #
16
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
18
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
19
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
20
+ # OR MODIFICATIONS.
21
+ ##########################################################################################
22
+
23
+ require 'gknit'
24
+
25
+ R.install_and_loads 'xfun'
26
+
27
+ GKnit.draft(file: ARGV[0], template: ARGV[1], package: ARGV[2],
28
+ create_dir: ARGV[3], is_package: ARGV[4], edit: ARGV[5])
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require 'optionparser'
5
+
6
+ options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: gknit [options]"
10
+ opts.on("-f", "--filename FILE", "The input file to be rendered. This value can also be provided without the '-f' or '--filename' flag by just giving the name of the file as an input parameter.") do |opt|
11
+ options[:input_file] = opt
12
+ end
13
+ opts.on("--output_format OUT_FORMAT", "The R Markdown output format to convert to. The option 'all' will render all formats defined within the file. The option can be the name of a format (e.g. 'html_document') and that will render the document to that single format. One can also use a vector of format names to render to multiple formats. Alternatively, you can pass an output format object (e.g. html_document()). If using NULL then the output format is the first one defined in the YAML frontmatter in the input file (this defaults to HTML if no format is specified there).") do |opt|
14
+ options[:output_format] = opt
15
+ end
16
+ opts.on("-o", "--output_file OUT_FILE", "The name of the output file. If using nil then the output filename will be based on filename for the input file. If a filename is provided, a path to the output file can also be provided. Note that the output_dir option allows for specifying the output file path as well, however, if also specifying the path, the directory must exist.") do |opt|
17
+ options[:output_file] = opt
18
+ end
19
+ opts.on("-d", "--output_dir OUT_DIR", "The output directory for the rendered output_file. This allows for a choice of an alternate directory to which the output file should be written (the default output directory is that of the input file). If a path is provided with a filename in output_file the directory specified here will take precedence. Please note that any directory path provided will create any necessary directories if they do not exist.") do |opt|
20
+ options[:output_dir] = opt
21
+ end
22
+ opts.on("-a", "--output_options OUT_OPT", "List of output options that can override the options specified in metadata (e.g. could be used to force self_contained or mathjax = 'local'). Note that this is only valid when the output format is read from metadata (i.e. not a custom format object passed to output_format).") do |opt|
23
+ options[:output_options] = opt
24
+ end
25
+ opts.on("-i", "--intermediates_dir INTER_DIR", "FEATURE NOT YET IMPLEMENTED! Intermediate files directory. If a path is specified then intermediate files will be written to that path. If nil, intermediate files are written to the same directory as the input file.") do |opt|
26
+ options[:intermediates_dir] = opt
27
+ end
28
+ opts.on("-n", "--no_clean", "Will not clean intermediate files that are created during rendering.") do |opt|
29
+ options[:clean] = opt
30
+ end
31
+ end.parse!
32
+
33
+ # Sets the directory
34
+ dir = Dir.pwd
35
+
36
+ #=========================================================================================
37
+ # Check if the name of the file to gKnit was given and that it exists. If not,
38
+ # issue error.
39
+ #=========================================================================================
40
+
41
+ options[:input_file] = ARGV[0] if !ARGV[0].nil?
42
+ raise "Input file name is required for gKnitting. Add -f FILE or --input_file FILE. For help, type gknit -h" if
43
+ options[:input_file].nil?
44
+ options[:input_file] = "#{dir}/#{options[:input_file]}"
45
+ raise "File #{options[:input_file]} does not exist!" if !File.exist?(options[:input_file])
46
+
47
+ #=========================================================================================
48
+ # Checks the output_format option. If none, pass NULL to the render function. Output
49
+ # format needs to be quoted.
50
+ #=========================================================================================
51
+
52
+ options[:output_format] =
53
+ (options[:output_format].nil?)? "NULL" : "\"#{options[:output_format]}\""
54
+
55
+ #=========================================================================================
56
+ # Checks the output_file option. If none, pass NULL to the render function. Output
57
+ # file name needs to be quoted.
58
+ #=========================================================================================
59
+
60
+ options[:output_file] =
61
+ (options[:output_file].nil?)? "NULL" : "\"#{options[:output_file]}\""
62
+
63
+ #=========================================================================================
64
+ # Checks if an output directory was given. If given and directory does not exist,
65
+ # create the directory. If no directory given, pass NULL to the render function.
66
+ #=========================================================================================
67
+
68
+ if (!options[:output_dir].nil?)
69
+ Dir.mkdir(options[:output_dir]) if !Dir.exist?(options[:output_dir])
70
+ options[:output_dir] = "\"#{options[:output_dir]}\""
71
+ else
72
+ # If not, pass NULL to the render function
73
+ options[:output_dir] = "NULL"
74
+ end
75
+
76
+ #=========================================================================================
77
+ # Checks if any intermediate directory given. If not, pass NULL to the render function
78
+ # Calling render with options intermediates_dir is not working, requires further study
79
+ #=========================================================================================
80
+
81
+ if (!options[:intermediates_dir].nil?)
82
+ if !Dir.exist?(options[:intermediates_dir])
83
+ Dir.mkdir(options[:intermediates_dir])
84
+ options[:intermediates_dir] = "\"#{options[:intermediates_dir]}\""
85
+ else
86
+ options[:intermediates_dir] = "\"#{options[:intermediates_dir]}\""
87
+ end
88
+ else
89
+ options[:intermediates_dir] = "NULL"
90
+ end
91
+
92
+ options[:clean] = (options[:clean].nil?)? "TRUE" : "FALSE"
93
+
94
+ #=========================================================================================
95
+ # Sends message to user that the gKniting process has started
96
+ #=========================================================================================
97
+ puts "================================"
98
+ puts "gKnitting #{options[:input_file]} with the following options:"
99
+ puts " * Output format: \t #{options[:output_format]}"
100
+ puts " * Output file: \t #{options[:output_file]}"
101
+ puts " * Output directory: \t #{options[:output_dir]}"
102
+ puts " * Clean: \t\t #{options[:clean]}"
103
+
104
+
105
+ # Code to be evaluated by the R script
106
+ code = <<EOF
107
+ eval.polyglot("ruby", "$LOAD_PATH.unshift %q(/home/rbotafogo/desenv/galaaz/lib)")
108
+ eval.polyglot("ruby", "require %q(gknit)")
109
+
110
+ rmarkdown::render("#{options[:input_file]}", output_format = #{options[:output_format]},
111
+ output_file = #{options[:output_file]},
112
+ output_dir = #{options[:output_dir]},
113
+ clean = #{options[:clean]})
114
+ EOF
115
+
116
+
117
+ exec "Rscript --jvm --polyglot -e '#{code}'"
118
+
119
+ =begin
120
+ # how to call rmarkdown::render with a parameter list
121
+ # all params need to be defined in the YAML header
122
+ rmarkdown::render("#{options[:input_file]}", output_format = #{options[:output_format]},
123
+ output_file = #{options[:output_file]},
124
+ output_dir = #{options[:output_dir]},
125
+ clean = #{options[:clean]},
126
+ params = list(rake_clean = TRUE))
127
+ =end