-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRprep.pl
134 lines (112 loc) · 2.78 KB
/
Rprep.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
### Takes a bunch of filenames, usually generated by make
### Generates an R file that:
### ### loads all .RData files (and .RData files corresponding to .Rout files)
### ### makes .R files into a script (obeying START and END commands)
### ### Puts other file names an a variable called input_files
### ### saves image to a .RData file
### Make should pipe output to a .Rout file,
### and provide its name as first argument.
use strict;
use 5.10.0;
my $useCLArgs = "for (a in commandArgs(TRUE)){ eval(parse(text=a)) }";
my @env;
my @envir;
my @R;
my @input;
my $target = shift(@ARGV);
die "ERROR -- Rprep.pl: Illegal target $target (does not end with .Rout) \n" unless $target =~ s/.Rout$/.RData/;
die "ERROR -- Rprep.pl: No input files received, nothing to do. A rule, script or dependency is probably missing from the project directory \n" unless @ARGV>0;
my $rtarget = $target;
$rtarget =~ s/\.RData//;
my $savetext = "save.image(file=\"$target\")";
my $save = $savetext;
foreach(@ARGV){
s/Rout$/RData/;
if ((/\.RData$/) or (/\.rda$/) or (/.R.env$/) or (/.Rdata/)){
push @env, $_;
} elsif (/\.R$/){
push @R, $_;
} elsif (/\.envir$/){
s/.envir//;
s/Rout$/RData/;
push @envir, "\"$_\"";
} else {
push @input, "\"$_\"";
}
}
foreach(@env){
print "load('$_')\n";
}
if (@input){
print "input_files <- c(";
print join ", ", @input;
print ")\n";
}
if (@envir){
print "envir_list <- list(); ";
print "for (f in c(";
print join ", ", @envir;
say ")){envir_list[[f]] <- new.env(); load(f, envir=envir_list[[f]])}";
}
print "pdf(\"$rtarget.Rout.pdf\")\n# End RR preface\n";
if (@env){
print "# Global Environment: ";
print join " ", @env;
print "\n\n";
}
if (@input){
print "# Input: ";
print join " ", @input;
print "\n\n";
}
if (@R){
print "# Scripts: ";
print join " ", @R;
print "\n\n";
}
if (@envir){
print "# Environment list: ";
print join " ", @envir;
print "\n\n";
}
foreach my $f (@R){
my $text;
open (INF, $f);
while(<INF>){
if (/^END/){
print STDERR ("END at line $. in $f\n");
last;
}
if (/setpdf/){
s/^# *//;
s/setpdf\s*\(\)/dev.off(); pdf("RTARGET.Rout.pdf")/;
s/setpdf\s*\(/dev.off(); pdf("RTARGET.Rout.pdf", /;
}
if (/RTARGET/){
s/^# *//;
s/RTARGET/$rtarget/g;
}
if (/useCLArgs/){
s/^# *//;
s/useCLArgs/$useCLArgs/g;
}
$text .= $_;
if (/^START/){
print STDERR ("START at line $. in $f\n");
$text = "";
$save= $savetext;
}
if (/rdsave/){
$save = $_;
$save =~ s/^# *//;
if ($save =~/^#/) {$save=$savetext} else{
$save =~ s/rdsave\s*\(/save(file="$target", / or die("Problem with special statement $save");
}
}
if (/rdnosave/){
$save = "";
}
}
print $text;
}
print "\n# Begin RR postscript\n# If you see this in an R log, your R script did not close properly\n$save\n";