Class: Deplate::Regions::R
Constant Summary
collapse
- @@RAutoN =
0
Class Method Summary
collapse
Instance Method Summary
collapse
check_file, clean_strings, #deprecated_regnote, deprecated_regnote, #finish_accum, #format_compound, regions, register_as, set_line_cont, #setup
Methods inherited from Element
#join_lines, #join_lines_re_zh_cn
Class Method Details
.run(container, r, out) ⇒ Object
1098
1099
1100
|
# File 'lib/deplate/regions.rb', line 1098
def run(container, r, out)
Deplate::External.r(container, r, out)
end
|
Instance Method Details
#do_drop(r, out) ⇒ Object
954
955
956
957
|
# File 'lib/deplate/regions.rb', line 954
def do_drop(r, out)
send_to_R(r, out)
return nil
end
|
#do_normal(r, out) ⇒ Object
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
# File 'lib/deplate/regions.rb', line 968
def do_normal(r, out)
if send_to_R(r, out)
scn = table_scanner(@accum)
@accum.collect! do |r|
if r =~ /^\s*$/
"| |"
elsif @args['guess'] or @args['scanTable'] or @deplate.variables['rScanTable']
cells = scn.match(r)
if cells
cells = cells.captures.collect do |c|
v = c.strip
v.empty? ? @deplate.formatter.table_empty_cell : v
end
['|', cells.join(' | '), '|'].join(' ')
else
puts @accum.join("\n")
raise 'DBG: Error in table scanner (please report)'
end
else
"| %s |" % Deplate::Particle::Code.markup(r.gsub(/ /, "\\\\ "))
end
end
accum = @deplate.parsed_array_from_strings(@accum, 1 + @source.begin, @source.file)
if accum[0].class != Deplate::Element::Table
log(["Expected exactly one element of type Table", accum.collect {|e| e.class}.join(" ")], :error)
end
rOut = accum[0]
if rOut
rOut.collapse = false
else
log(["R yielded no output", out], :error)
end
return rOut
end
end
|
#do_R ⇒ Object
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
|
# File 'lib/deplate/regions.rb', line 861
def do_R
pre = @deplate.variables['rPrelude']
if pre
@accum = [pre, @accum].flatten.compact
end
id = @args["id"]
unless id
@@RAutoN += 1
id = @deplate.auxiliary_auto_filename('r', @@RAutoN, @accum)
end
r = "#{id}.R"
out = "#{id}.Rout"
case @regNote.strip
when "xtable"
rOut = do_xtable(r, out)
when "drop"
rOut = do_drop(r, out)
when "verb", "verbatim"
rOut = do_verbatim(r, out)
else
rOut = do_normal(r, out)
end
if rOut
rOut.args.update(@args)
return rOut
end
end
|
#do_verbatim(r, out) ⇒ Object
959
960
961
962
963
964
965
966
|
# File 'lib/deplate/regions.rb', line 959
def do_verbatim(r, out)
if send_to_R(r, out)
@elt = @accum.join("\n")
rOut = Deplate::Regions::Verbatim.new(@deplate, @source, @text, @match, self)
rOut.finish
return rOut
end
end
|
#do_xtable(r, out) ⇒ Object
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
|
# File 'lib/deplate/regions.rb', line 889
def do_xtable(r, out)
@accum.unshift(%{library(xtable)})
if send_to_R(r, out)
table = []
pre = []
post = []
mode = :pre
for l in @accum
xtable_postprocess_text(l)
m = /^\s*\<([^> ]+).*?\>(.*)?(\<\/(\S+)\>)?\s*$/.match(l)
if m
case m[1]
when "!--"
next
when "TABLE"
mode = :table
when "/TABLE"
mode = :post
when "TR"
row = l.scan(/\<TH.*?\>\s*(.*?)\s*<\/TH\>/).flatten
unless row.empty?
table << "|| #{row.join(" || ")} ||"
else
row = l.scan(/\<TD.*?\>\s*(.*?)\s*\<\/TD\>/).flatten
table << "| #{row.join(" | ")} |" unless row.empty?
end
log(["R xtable: row", row], :debug)
else
log(["Skip", m[1]])
end
else
case mode
when :pre
pre << l
when :post
post << l
end
end
end
accum = @deplate.parsed_array_from_strings(table, 1 + @source.begin, @source.file)
if accum.size != 1
log(["Unexpected R output (too many elements)",
out, accum.collect {|e| e.class}], :error)
end
until accum.empty? or accum[0].kind_of?(Deplate::Element::Table)
e = accum.shift
log(["Unexpected R output (Please check the output for errors!)",
out, e.class], :error)
end
rOut = accum[0]
if rOut
rOut.collapse = false
rOut.preNote = pre.join("\n")
rOut.postNote = post.join("\n")
log(["R xtable: pre", rOut.preNote], :debug)
log(["R xtable: post", rOut.postNote], :debug)
return rOut
end
end
end
|
#finish ⇒ Object
851
852
853
854
855
856
857
858
859
|
# File 'lib/deplate/regions.rb', line 851
def finish
finish_accum
begin
return do_R
rescue StandardError => e
log(["Program call failed", e, e.backtrace[0..10]], :error)
return nil
end
end
|
#run(r, out) ⇒ Object
1093
1094
1095
|
# File 'lib/deplate/regions.rb', line 1093
def run(r, out)
return Deplate::Regions::R.run(self, r, out)
end
|
#send_to_R(r, out) ⇒ Object
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
|
# File 'lib/deplate/regions.rb', line 1057
def send_to_R(r, out)
pwd = Dir.pwd
d = File.dirname(@deplate.dest)
rv = false
@deplate.in_working_dir do
log(["Running R", d, r, out])
begin
@accum.unshift(%{deplate.fmt <- "#{@deplate.formatter.formatter_name}"})
@accum << %{q(runLast=FALSE)}
if Deplate::Region.check_file(self, out, r, @accum)
log(["Files exist! Using", out], :anyway)
else
unless @deplate.options.force
for f in [r, out]
if File.exist?(f)
raise "Please delete '#{f}' or change the id before proceeding\nUse the --force option to avoid this message."
end
end
end
Deplate::External.write_file(self, r) {|io| io.puts(@accum.join("\n"))}
run(r, out)
end
@accum = File.open(out) {|io| io.read}.split(/[\n\r]+/)
skip = @args["skip"]
if skip
head, tail = Deplate::Core.split_list(skip, nil, nil, @source).collect {|n| n.to_i}
@accum = @accum[(head || 0) .. (@accum.size - tail - 1 || -1)] || []
end
rv = true
rescue StandardError => e
log("#R: #{e}", :error)
end
end
return rv
end
|
#table_scanner(strings) ⇒ Object
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
|
# File 'lib/deplate/regions.rb', line 1005
def table_scanner(strings)
max = 0
for l in strings
ls = l.size
if ls > max
max = ls
end
end
blanks = [true] * max
for l in strings
for i in 0..(l.size - 1)
if l[i] != 32
blanks[i] &&= false
end
end
end
lst = true
blanks.collect! do |c|
if c and lst
rv = false
else
rv = c
end
lst = c
rv
end
acc = []
lst = -1
blanks.each_with_index do |c, i|
if c
d = i - lst
lst = i
acc << ' ' unless acc.empty?
acc << '(' << '.' * (d - 1) << ')'
end
end
d = max - 1 - lst
if d > 1
acc << ' ' unless acc.empty?
acc << '(' << '.' * (d - 1) << ')'
end
Regexp.new(acc.join)
end
|
#xtable_postprocess_text(text) ⇒ Object
1049
1050
1051
1052
1053
1054
1055
|
# File 'lib/deplate/regions.rb', line 1049
def xtable_postprocess_text(text)
text.gsub!(/&?/, %{&})
text.gsub!(/<?/, %{<})
text.gsub!(/>?/, %{>})
text.gsub!(/"?/, %{"})
text
end
|