Dirk Eddelbuettel
2001-Jan-27 04:37 UTC
[Rd] Off-topic: Pretty-printing R (or S) code with GNU a2ps
--oP8mQFO4PT
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit
GNU a2ps is a fairly versatile text-to-anything processor. It is useful for
typsetting source code from a wide variety of programming languages. As R
(ie S) wasn't supported yet, I quickly cobbled a style sheet for the S
language together. Stylesheets are documented in the texinfo docs for a2ps,
and examples can be found in /usr/share/a2ps/sheets (on Debian at least,
others distros or unices probably have similar locations). IIRC a2ps exists
for NT but might require Cygwin. I typeset on Linux and only print on NT :)
Well, as I still don't really know what I'm doing with R, I would
appreciate
comments / feedback / input from anyone could spend a moment or two on this.
To activate the stylesheet, simple patch
/usr/share/a2ps/sheets/sheets.map
as per the short diff below which declares that the stylesheet s.ssh shall be
used for filenames matching /*.[RS]/. The sheet itself is attached, just
place it in the same directory, ie /usr/share/a2ps/sheets/.
Usage then simply becomes
a2ps -Pdisplay foo.R
or
a2ps foo.R -o foo.ps; gv foo.ps
in case the "display" device isn't defined.
A few points / questions:
* content: a2ps uses tricks to use TeX symbols. It works well to typset
<- with \leftarrow (uncomment that in s.ssh to see) but I couldn't
tell
it to parse
a<-2
it only finds <- with whitespace, eg
a <- 2
What could be done about <<- ? Is there a matching TeX symbol?
* more content: Is the list of keywords really that short? I couldn't find
much else...
* scope: Are other file extensions in use? Are there conventions for S or
Splus files? Any clever way to make a2ps understand the files in
$R_HOME/library/$PACKAGE/R are also R? Could file(1) be taught what
an R file is?
* editorial: shall this be the s.ssh or r.ssh or rs.ssh?
Thanks, Dirk
edd@sonny:/tmp/a2ps> cat sheets.map.diff
*** /usr/share/a2ps/sheets/sheets.map Fri Jan 26 22:13:55 2001
--- sheets.map Fri Jan 26 22:17:42 2001
***************
*** 373,378 ****
--- 373,381 ----
<*python script*>
/*.py/
+ # GNU R files, as well as S language files
+ s: /*.[RS]/
+
# REXX command files
rexx: /*.cmd/i
--oP8mQFO4PT
Content-Type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GZ1bmN0
aW9uLCBpZiwgImluIiwgcmVwZWF0LCBzd2l0Y2gsIHdoaWxlCmVuZCBrZXl3b3JkcwoKI29w
dGlvbmFsIG9wZXJhdG9ycyBhcmUKb3BlcmF0b3JzIGFyZQogICA8PiBcbmVxLAogICAhPSBc
bmVxLAogICA8PSBcbGVxLAogICA+PSBcZ2VxLAogICA9ICBcZXF1aXYsCiMgICA8LSBcbGVm
dGFycm93LAogICA8LSwKICAgPDwtLAogICAtPiwKICAgXwplbmQgb3BlcmF0b3JzCgpzZXF1
ZW5jZXMgYXJlCiAgICAiIyIgQ29tbWVudCwKICAgIEMtc3RyaW5nLAogICAgQy1jaGFyCmVu
ZCBzZXF1ZW5jZXMKCmVuZCBzdHlsZQo
--oP8mQFO4PT
Content-Type: text/plain; charset=us-ascii
Content-Description: .signature
Content-Transfer-Encoding: 7bit
--
According to the latest figures, 43% of all statistics are totally worthless.
--oP8mQFO4PT--
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To:
r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kurt Hornik
2001-Feb-12 15:10 UTC
[Rd] Off-topic: Pretty-printing R (or S) code with GNU a2ps
[Manually approved by MM -- because it contains binary attachment ] --QcBiZYhiHg Content-Type: text/plain; charset=us-ascii Content-Description: message body text Content-Transfer-Encoding: 7bit>>>>> Dirk Eddelbuettel writes:> GNU a2ps is a fairly versatile text-to-anything processor. It is > useful for typsetting source code from a wide variety of programming > languages. As R (ie S) wasn't supported yet, I quickly cobbled a > style sheet for the S language together. Stylesheets are documented in > the texinfo docs for a2ps, and examples can be found in > /usr/share/a2ps/sheets (on Debian at least, others distros or unices > probably have similar locations). IIRC a2ps exists for NT but might > require Cygwin. I typeset on Linux and only print on NT :)> Well, as I still don't really know what I'm doing with R, I would > appreciate comments / feedback / input from anyone could spend a > moment or two on this. To activate the stylesheet, simple patch> /usr/share/a2ps/sheets/sheets.map> as per the short diff below which declares that the stylesheet s.ssh > shall be used for filenames matching /*.[RS]/. The sheet itself is > attached, just place it in the same directory, ie > /usr/share/a2ps/sheets/.> Usage then simply becomes > a2ps -Pdisplay foo.R > or > a2ps foo.R -o foo.ps; gv foo.ps > in case the "display" device isn't defined.Thanks for bringing this up. We have locally used style sheets S.ssh for S code and Rd.ssh for Rd documentation format, both written by T Hothorn. I attach these files below.> A few points / questions:> * content: a2ps uses tricks to use TeX symbols. It works well to typset > <- with \leftarrow (uncomment that in s.ssh to see) but I couldn't tell > it to parse > a<-2 > it only finds <- with whitespace, eg > a <- 2Torsten gets this right by using a different alphabet, I think.> What could be done about <<- ? Is there a matching TeX symbol?Dunno.> * more content: Is the list of keywords really that short? I couldn't find > much else...R-lang says: The following identifiers have a special meaning and should not be used for object names if else repeat while function for in next break TRUE FALSE NULL NA Inf NaN ... ..1 ..2 etc. so I think this is the list of keywords to use. (Yes switch() is not in there ...)> * scope: Are other file extensions in use? Are there conventions for S or > Splus files? Any clever way to make a2ps understand the files in > $R_HOME/library/$PACKAGE/R are also R? Could file(1) be taught what > an R file is?I think assuming that extensions `.R' and `.Rd' for R code and doc source files are pretty safe. The package installer will also allow for code extensions `.r', `.S', `.s', and `.q', but I think only the last of these may be specific enough to make it into the extension list. No R magic in general. ESS think .R/.r are safe for auto-mode-alist and .S/.s need enforcing. [One could try checking for Emacs-style mode magic but I think that would be a bad idea.]> * editorial: shall this be the s.ssh or r.ssh or rs.ssh?I think we want S.ssh, as this is a style sheet for code written in the S language. Best, -k --QcBiZYhiHg Content-Type: application/octet-stream Content-Disposition: attachment; filename="S.ssh" Content-Transfer-Encoding: base64 IyBUaGlzIGlzIGEgUiBzaGVldApzdHlsZSBSIGlzCndyaXR0ZW4gYnkgIlRvcnN0ZW4gSG90aG9y biA8aG90aG9ybkBjaS50dXdpZW4uYWMuYXQ+Igp2ZXJzaW9uIGlzIDAuMQpyZXF1aXJlcyBhMnBz IDQuOS41Cgpkb2N1bWVudGF0aW9uIGlzCiAgICJDcmVhdGVzIHByZXR0eSBSLWNvZGUgcG9zdHNj cmlwdCBmaWxlcy4iCmVuZCBkb2N1bWVudGF0aW9uCgphbHBoYWJldHMgYXJlCiAgICJBQkNERUZH SElKS0xNTk9QUVJTVFVWV1hZWmFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6XyMkJT0tJi8iCgpj YXNlIHNlbnNpdGl2ZQoKIyBhbmNlc3RvcnMgYXJlCiMgICBjCiMgZW5kIGFuY2VzdG9ycwoKc2Vx dWVuY2VzIGFyZQogICIjIiBDb21tZW50LAogICJcIiIgUGxhaW4gU3RyaW5nICJcIiIgUGxhaW4K IyAgIGV4Y2VwdGlvbnMgYXJlCiMgICAgL1xcXFwsLwojICAgICAgIGVuZCBleGNlcHRpb25zCmVu ZCBzZXF1ZW5jZXMKCmtleXdvcmRzIGluIEtleXdvcmRfc3Ryb25nIGFyZQogIGlmLCB0aGVuLCBl bHNlLCBicmVhaywgd2hpbGUsIGZvciwgZnVuY3Rpb24sIG5leHQsIHJldHVybiwgRkFMU0UsIFRS VUUKZW5kIGtleXdvcmRzCgpvcGVyYXRvcnMgYXJlCiAgIDwtIFxsZWZ0YXJyb3cKZW5kIG9wZXJh dG9ycwoKb3B0aW9uYWwgc2VxdWVuY2VzIGFyZQogICA8IFBsYWluIEtleXdvcmQgPiBQbGFpbgpl bmQgc2VxdWVuY2VzCmVuZCBzdHlsZQ=--QcBiZYhiHg Content-Type: application/octet-stream Content-Disposition: attachment; filename="Rd.ssh" Content-Transfer-Encoding: base64 IyBTdHlsZSBzaGVldCBmb3IgUyBvdXRwdXQKIyBDb3B5cmlnaHQgKGMpIDIwMDAgVG9yc3RlbiBI b3Rob3JuCgojCiMgCiMgVGhpcyBwcm9ncmFtIGl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KdmVyc2lvbiBpcyAwLjEKcmVxdWlyZXMgYTJwcyA0LjkuNQoKZG9jdW1lbnRh dGlvbiBpcwogICAJImEycHMgc3R5bGUgc2hlZXQgZm9yIFJkIgplbmQgZG9jdW1lbnRhdGlvbgoK YWxwaGFiZXRzIGFyZQogICAiQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xt bm9wcXJzdHV2d3h5el8jJCU9LSYvIgoKY2FzZSBzZW5zaXRpdmUKCiMgYW5jZXN0b3JzIGFyZQoj ICAgYwojIGVuZCBhbmNlc3RvcnMKCnNlcXVlbmNlcyBhcmUKICAiIyIgQ29tbWVudCwKICAiXCIi IFBsYWluIFN0cmluZyAiXCIiIFBsYWluLAogICJcXCIgS2V5d29yZF9zdHJvbmcgInsiIFBsYWlu CiMgICJ7IiBQbGFpbiBTdHJpbmcgIn0iIFBsYWluCiMJZXhjZXB0aW9ucyBhcmUKIwkvaXRlbXss Y29kZXssdXNhZ2V7LGtleXdvcmR7LwojCWVuZCBleGNlcHRpb25zCiMgICJ7IiBQbGFpbiBTdHJp bmcgIn17IiBQbGFpbgojCQojICAgZXhjZXB0aW9ucyBhcmUKIyAgIC9cXFthOnpdey8gS2V5d29y ZCAifSIgUGxhaW4KIyAgIGVuZCBleGNlcHRpb25zCmVuZCBzZXF1ZW5jZXMKCm9wZXJhdG9ycyBh cmUKICAgPC0gXGxlZnRhcnJvdwplbmQgb3BlcmF0b3JzCgpvcHRpb25hbCBzZXF1ZW5jZXMgYXJl CiAgIDwgUGxhaW4gS2V5d29yZCA+IFBsYWluCmVuZCBzZXF1ZW5jZXMKZW5kIHN0eWxl --QcBiZYhiHg-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._