~showard314/ubuntu/karmic/r-base/remove_start_comments

« back to all changes in this revision

Viewing changes to share/perl/massage-Examples.pl

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2009-04-17 06:56:48 UTC
  • mfrom: (1.3.1 upstream) (6.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090417065648-9mgm2udb2eanluot
* New upstream version released this morning

* debian/rules: Turn optimisation back to -O3 on alpha (as elsewhere) 
  with thanks to Kurt Roeckx for applying a fix to gcc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#-*- perl -*-
2
 
 
3
 
# Copyright (C) 2001--2007 R Development Core Team
4
 
# Copyright (C) 2003-4, 2006 The R Foundation
5
 
#
6
 
# This program is free software; you can redistribute it and/or modify
7
 
# it under the terms of the GNU General Public License as published by
8
 
# the Free Software Foundation; either version 2, or (at your option)
9
 
# any later version.
10
 
#
11
 
# This program is distributed in the hope that it will be useful, but
12
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
# General Public License for more details.
15
 
#
16
 
# A copy of the GNU General Public License is available at
17
 
# http://www.r-project.org/Licenses/
18
 
#
19
 
# Send any bug reports to r-bugs@r-project.org
20
 
 
21
 
## Usage: perl massage-Examples.pl pkgname files
22
 
 
23
 
## Given a list of files of the form .../.../<name>.R, produce one large
24
 
## file, i.e., write to stdout, concatenating the files together with
25
 
## 1) Putting a HEADER in front
26
 
## 2) Wrapping every file in order to be more order independent
27
 
## 3) appending a FOOTER ...
28
 
 
29
 
use File::Basename;
30
 
 
31
 
my $PKG = shift @ARGV;
32
 
my @Rfiles;
33
 
if(-d $ARGV[0]) {
34
 
    my $dir = $ARGV[0];
35
 
    opendir(DIR, $dir) or die "cannot opendir $dir: $!";
36
 
    my @files = sort grep { /\.R$/ } readdir(DIR);
37
 
    closedir(DIR);
38
 
    foreach my $file (@files) {
39
 
        push(@Rfiles, "$dir/$file");
40
 
    }
41
 
} else {
42
 
    @Rfiles = @ARGV;
43
 
}
44
 
 
45
 
### * Header
46
 
print <<_EOF_;
47
 
### * <HEADER>
48
 
###
49
 
attach(NULL, name = "CheckExEnv")
50
 
assign("nameEx", 
51
 
       local({
52
 
           s <- "__{must remake R-ex/*.R}__"
53
 
           function(new) {
54
 
               if(!missing(new)) s <<- new else s
55
 
           }
56
 
       }),
57
 
       pos = "CheckExEnv")
58
 
## Add some hooks to label plot pages for base and grid graphics
59
 
assign("base_plot_hook",
60
 
       function() {
61
 
           pp <- par(c("mfg","mfcol","oma","mar"))
62
 
           if(all(pp\$mfg[1:2] == c(1, pp\$mfcol[2]))) {
63
 
               outer <- (oma4 <- pp\$oma[4]) > 0; mar4 <- pp\$mar[4]
64
 
               mtext(sprintf("help(\\"%s\\")", nameEx()), side = 4,
65
 
                     line = if(outer)max(1, oma4 - 1) else min(1, mar4 - 1),
66
 
               outer = outer, adj = 1, cex = .8, col = "orchid", las=3)
67
 
           }
68
 
       },
69
 
       pos = "CheckExEnv")
70
 
assign("grid_plot_hook",
71
 
       function() {
72
 
           grid::pushViewport(grid::viewport(width=grid::unit(1, "npc") - 
73
 
                              grid::unit(1, "lines"), x=0, just="left"))
74
 
           grid::grid.text(sprintf("help(\\"%s\\")", nameEx()),
75
 
                           x=grid::unit(1, "npc") + grid::unit(0.5, "lines"),
76
 
                           y=grid::unit(0.8, "npc"), rot=90,
77
 
                           gp=grid::gpar(col="orchid"))
78
 
       },
79
 
       pos = "CheckExEnv")
80
 
setHook("plot.new",     get("base_plot_hook", pos = "CheckExEnv"))
81
 
setHook("persp",        get("base_plot_hook", pos = "CheckExEnv"))
82
 
setHook("grid.newpage", get("grid_plot_hook", pos = "CheckExEnv"))
83
 
assign("cleanEx",
84
 
       function(env = .GlobalEnv) {
85
 
           rm(list = ls(envir = env, all.names = TRUE), envir = env)
86
 
           RNGkind("default", "default")
87
 
           set.seed(1)
88
 
           options(warn = 1)
89
 
           .CheckExEnv <- as.environment("CheckExEnv")
90
 
           delayedAssign("T", stop("T used instead of TRUE"),
91
 
                  assign.env = .CheckExEnv)
92
 
           delayedAssign("F", stop("F used instead of FALSE"),
93
 
                  assign.env = .CheckExEnv)
94
 
           sch <- search()
95
 
           newitems <- sch[! sch %in% .oldSearch]
96
 
           for(item in rev(newitems))
97
 
               eval(substitute(detach(item), list(item=item)))
98
 
           missitems <- .oldSearch[! .oldSearch %in% sch]
99
 
           if(length(missitems))
100
 
               warning("items ", paste(missitems, collapse=", "),
101
 
                       " have been removed from the search path")
102
 
       },
103
 
       pos = "CheckExEnv")
104
 
assign("ptime", proc.time(), pos = "CheckExEnv")
105
 
## at least one package changes these via ps.options(), so do this
106
 
## before loading the package.
107
 
## Use postscript as incomplete files may be viewable, unlike PDF.
108
 
## Choose a size that is close to on-screen devices, fix paper
109
 
ps.options(width = 7, height = 7, paper = "a4", reset = TRUE)
110
 
grDevices::postscript("$PKG-Ex.ps")
111
 
                      
112
 
assign("par.postscript", graphics::par(no.readonly = TRUE), pos = "CheckExEnv")
113
 
options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly"))
114
 
options(warn = 1)    
115
 
_EOF_
116
 
 
117
 
if ($PKG eq "tcltk") {
118
 
    print "require('tcltk') || q()\n\n";
119
 
} elsif ($PKG ne "base") {
120
 
    print "library('$PKG')\n\n";
121
 
}
122
 
print "assign(\".oldSearch\", search(), pos = 'CheckExEnv')\n";
123
 
print "assign(\".oldNS\", loadedNamespaces(), pos = 'CheckExEnv')\n";
124
 
 
125
 
### * Loop over all R files, and edit a few of them ...
126
 
foreach my $file (@Rfiles) {
127
 
    my $have_examples = 0;
128
 
    my $have_par = 0;
129
 
    my $have_contrasts = 0;
130
 
    my $nm;
131
 
 
132
 
    $nm = basename $file, (".R");
133
 
    $nm =~ s/[^- .a-zA-Z0-9]/./g;
134
 
 
135
 
    if ($PKG eq "graphics" && $file =~ /[^m]text\.R$/) { next; }
136
 
    open(FILE, "< $file") or die "file $file cannot be opened";
137
 
    while (<FILE>) {
138
 
        $have_examples = 1
139
 
            if ((/_ Examples _/o) || (/### \*+ Examples/));
140
 
        next if /^#/; # need to skip comment lines
141
 
        $have_par = 1 if (/[^a-zA-Z0-9.]par\(/o || /^par\(/o);
142
 
        $have_contrasts = 1 if /options\(contrasts/o;
143
 
    }
144
 
    close(FILE);
145
 
    if ($have_examples) {
146
 
        print "cleanEx(); nameEx(\"$nm\")\n";
147
 
    }
148
 
 
149
 
    print "### * $nm\n\n";
150
 
    print "flush(stderr()); flush(stdout())\n\n";
151
 
    open(FILE, "< $file") or die "file $file cannot be opened";
152
 
    my $dont_test = 0;  
153
 
    while (<FILE>) {
154
 
        ## process \donttest
155
 
        $dont_test = 1 if /^## No test:/;
156
 
        print $_ unless $dont_test;
157
 
        $dont_test = 0 if /^## End\(No test\)/;
158
 
    }
159
 
    close(FILE);
160
 
 
161
 
    if($have_par) {
162
 
        ## if there were 'par()' calls, now reset them:
163
 
        print "graphics::par(get(\"par.postscript\", pos = 'CheckExEnv'))\n";
164
 
    }
165
 
    if($have_contrasts) {
166
 
        ## if contrasts were set, now reset them:
167
 
        print "options(contrasts = c(unordered = \"contr.treatment\"," .
168
 
            "ordered = \"contr.poly\"))\n";
169
 
    }
170
 
 
171
 
}
172
 
 
173
 
### * Footer
174
 
print <<_EOF_;
175
 
### * <FOOTER>
176
 
###
177
 
cat("Time elapsed: ", proc.time() - get("ptime", pos = 'CheckExEnv'),"\\n")
178
 
grDevices::dev.off()
179
 
###
180
 
### Local variables: ***
181
 
### mode: outline-minor ***
182
 
### outline-regexp: "\\\\(> \\\\)?### [*]+" ***
183
 
### End: ***
184
 
quit('no')
185
 
_EOF_