# [ESS] Code contribution or suggestion

Jim_Garrett at bd.com Jim_Garrett at bd.com
Fri Jun 24 17:10:26 CEST 2011

   Hi all,
I have just joined this list.  I've been using Emacs and ESS for years but
only recently have I begun learning e-lisp and customizing Emacs.  I've also
just begun using Sweave consistently.  I manage several other statisticians,
and I'm training them on Sweave so that we may use it for all critical
reports.  Along the way, I've taken some steps to automate the generation of
boilerplate text in standard, common cases.  This has made it much easier to
use Sweave, partly by reducing typing and partly to keep me from having to
remember options.  This may be a form of dumbing-down but it does help one
to get started quickly.
I'd like to contribute some e-lisp functions I've written for possible
inclusion  in  ESS.   They would be trivial for any experienced e-lisp
programmer to write, so I guess I'm actually asking that something along
these lines be included to help beginners set up Sweave documents.  (Or is
something equivalent already there?)  I'm using ESS 5.10-1, Emacs 23.1.1,
all from Ubuntu's repositories.
Specifically, here are the things I've automated:
1. Generating a skeletal LaTeX document with a standard preamble..
2. Creating a skeleton for an Sweave code chunk that creates a figure.
3. Similarly, for a table (using xtable).
4. Similarly, for a code chunk that returns nothing (i.e., carries out
necessary data processing but does not directly add to a report)

Below are my functions.  Feel free to make whatever modifications you feel
necessary, or to achieve these ends by other means.
Best regards,
Jim Garrett
Baltimore, Maryland, USA
1. To generate a template for a LaTeX file:
(defun create-sweave-file ()
"Insert contents of template file into buffer."
(interactive nil)
(insert-file-contents "~/.sweave-template.rnw"))
where .sweave-template.rnw is
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\begin{document}
\title{}
\author{}
\date{}
\maketitle
\end{document}
I actually include more in the skeleton because I encourage a particular
report format for my group, but I don't want to inflict those on everyone.
2. To create a template for a code chunk that creates a figure:
(defun insert-sweave-figure (label)
"Insert standard figure stuff and R tags."
(interactive "MEnter label: ")
(insert
(concat
"\\begin{figure}
\\begin{center}
<<" label ", fig = T, echo = F>>=
@
\\end{center}
\\caption{}
\\label{fig:" label "}
\\end{figure}")))
(I like to leave 3 blank lines in the code block so that I can edit the
middle one, leaving one above and below so it doesn't look crowded.)
3. To create a template for a code chunk creating a table, using xtable:
(defun insert-sweave-table (label)
"Insert code that creates a LaTeX table through the xtable function."
(interactive "MEnter label: ")
(insert
(concat
"<<"
label
", echo = F, results = tex>>=
require(xtable)
print(xtable(,
caption = \"\",
label = \"tab:"
label
"\"),
include.rownames = F)
@")))
4. To create a template for a code chunk that returns no output:
(defun insert-sweave-code (label)
"Insert code block that produces no output."
(interactive "MEnter label: ")
(insert
(concat
"<<"
label
", echo = F, include = F>>=
@")))
-----------------------------------------
*******************************************************************
IMPORTANT MESSAGE FOR RECIPIENTS IN THE U.S.A.: This message may constitute
an advertisement of a BD group's products or services or a solicitation of
interest in them. If this is such a message and you would like to opt out of
forward this e-mail to optoutbygroup at bd.com.
*******************************************************************This
message (which includes any attachments) is intended only for the designated
recipient(s). It may contain confidential or proprietary information and may
be  subject  to the attorney-client privilege or other confidentiality
protections. If you are not a designated recipient, you may not review, use,
copy or distribute this message. If you received this in error, please
notify  the sender by reply e-mail and delete this message. Thank you.
*******************************************************************
Becton Drive Franklin Lakes, NJ 07417 U.S.A.