Re: [R] Summary (Re: (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?)

2010-12-13 Thread Claudia Beleites

Dear Emmanuel and dear list,

Therefore, I let this problem to sleep. However, I Cc this answer (with
the original question below) to Max Kuhn and Friedrich Leisch, in the
(faint) hope that this feature, which does not seem to have been missed
by anybody in 8 years,
I've been missing it every once in a while, but till now I could always rephrase 
the problem with expand = FALSE or functions, and the chunk that does the actual 
calculation at the end.


Most often, however, I'm just lazy and use R comments. If math should go in 
there, I use listings instead of fancyvrb with the modified Sweave.sty that 
hopefully is attached (if not, see below).


Here's an example chunk:
keep.source=TRUE=
1 / 2 # $\frac{1}{x}$
4 + 4 # Here may come lots of explanations, that are in a \LaTeX\ 
paragraph\footnote{blabla}: even long lines are properly broken.\\ Though the 
new lines start at the beginning of the line. \\[6pt] And a line break in the 
chunk source will of course be interpreted as R again: so no new paragraphs 
inside the same comment.

# But there can be new commented lines.
3 + 6
# Note that comment only lines at the end of a code chunk seem to be lost.
# Not only one but all that aren't followed by R code
@
(the second line should be very long, I somehow can't keep thunderbird from 
inserting line breaks)



Hope that helps a bit,

Claudia

=== modified Sweave.sty ===
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{Sweave}{}

\RequirePackage{ifthen}
\newboolean{swe...@gin}
\setboolean{swe...@gin}{true}
\newboolean{swe...@ae}
\setboolean{swe...@ae}{true}

\declareoption{nogin}{\setboolean{swe...@gin}{false}}
\declareoption{noae}{\setboolean{swe...@ae}{false}}
\ProcessOptions

\RequirePackage{graphicx,listings}
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}

\ifthenelse{\boolean{swe...@gin}}{\setkeys{gin}{width=0.8\textwidth}}{}%
\ifthenelse{\boolean{swe...@ae}}{%
  \RequirePackage[T1]{fontenc}
  \RequirePackage{ae}
}{}%

\lstnewenvironment{Sinput}{\lstset{language=R,basicstyle=\sl,texcl, 
commentstyle=\upshape}}{}

\lstnewenvironment{Soutput}{\lstset{language=R}}{}
\lstnewenvironment{Scode}{\lstset{language=R,basicstyle=\sl}}{}

\newenvironment{Schunk}{}{}

\newcommand{\Sconcordance}[1]{%
  \ifx\pdfoutput\undefined%
  \csname newcount\endcsname\pdfoutput\fi%
  \ifcase\pdfoutput\special{#1}%
  \else\immediate\pdfobj{#1}\fi}


--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 0 40 5 58-37 68
email: cbelei...@units.it
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{Sweave}{}

\RequirePackage{ifthen}
\newboolean{swe...@gin}
\setboolean{swe...@gin}{true}
\newboolean{swe...@ae}
\setboolean{swe...@ae}{true}

\declareoption{nogin}{\setboolean{swe...@gin}{false}}
\declareoption{noae}{\setboolean{swe...@ae}{false}}
\ProcessOptions

\RequirePackage{graphicx,listings}
\IfFileExists{upquote.sty}{\RequirePackage{upquote}}{}

\ifthenelse{\boolean{swe...@gin}}{\setkeys{gin}{width=0.8\textwidth}}{}%
\ifthenelse{\boolean{swe...@ae}}{%
  \RequirePackage[T1]{fontenc}  
  \RequirePackage{ae}
}{}%

\lstnewenvironment{Sinput}{\lstset{language=R,basicstyle=\sl,texcl, commentstyle=\upshape}}{}
\lstnewenvironment{Soutput}{\lstset{language=R}}{}
\lstnewenvironment{Scode}{\lstset{language=R,basicstyle=\sl}}{}

\newenvironment{Schunk}{}{}

\newcommand{\Sconcordance}[1]{%
  \ifx\pdfoutput\undefined%
  \csname newcount\endcsname\pdfoutput\fi%
  \ifcase\pdfoutput\special{#1}%
  \else\immediate\pdfobj{#1}\fi}
\documentclass{article}
\begin{document}
keep.source=TRUE=
1 / 2 # $\frac{1}{x}$
4 + 4 # Here may come lots of explanations, that are in a \LaTeX\ 
paragraph\footnote{blabla}: even long lines are properly broken.\\ Though the 
new lines start at the beginning of the line. \\[6pt] And a line break in the 
chunk source will of course be interpreted as R again: so no new paragraphs 
inside the same comment.
# But there can be new commented lines.
3 + 6
# Note that comment only lines at the end of a code chunk seem to be lost.
# Not only one but all that aren't followed by R code
@
\end{document}

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Summary (Re: (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?)

2010-12-13 Thread Liviu Andronic
On Mon, Dec 13, 2010 at 12:30 AM, Emmanuel Charpentier
emm.charpent...@free.fr wrote:
 Therefore, I let this problem to sleep. However, I Cc this answer (with
 the original question below) to Max Kuhn and Friedrich Leisch, in the
 (faint) hope that this feature, which does not seem to have been missed
 by anybody in 8 years,

It has been, at least in a slightly different context. Sweave is not
very flexible when it comes to repetitive tasks: you cannot loop over
mixed R/LaTeX code. One way to work around is to define, in the
Preamble, a \newcommand containing most LaTeX code and calling it in
the R loop via a cat() statement. For a fully worked example see [1].

Unfortunately this is still very inflexible. I guess the right
answer to this is either 'brew' or 'markup' (or perhaps some other
packages out there), although I've never fully investigated the two
solutions since the workaround was good enough for my needs.

Regards
Liviu

[1] http://www.mail-archive.com/r-help@r-project.org/msg105487.html

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Summary (Re: (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?)

2010-12-12 Thread Emmanuel Charpentier
Dear list,

see comment at end.

On Sat, 11 Dec 2010 22:58:10 +, Emmanuel Charpentier wrote :

 Dear list,
 
 Inspired by the original Knuth tools, and for paedaogical reasons, I
 wish to produce a document presenting some source code with interspersed
 comments in the source (see Knuth's books rendering TeX and metafont
 sources to see what I mean).
 
 I seemed to remember that a code chunk could be defined piecewise, like
 in
 
 Comments...
 
 Chunk1, eval=FALSE, echo=TRUE=
 SomeCode
 @
 
 Some other comments...
 
 Chunk2, eval=FALSE, echo=TRUE=
 MoreCode
 @
 
 And finally,
 
 Chunk3, eval=TRUE, echo=TRUE=
 Chunk1
 Chunk2
 EndOfTheCode
 @
 
 That works ... as long as SomeCode, MoreCode and EndOfTheCode are self-
 standing pieces of R code, but *not* code fragments. You can *not*
 intersperse comments in, say, a function body, or  local() environment
 this way : when Sweaving, *R* complains of an incomplete source (makes
 noise about an unexpected end of input at the end of Chunk1, IIRC, and
 never sees Chunk2).
 
 I hoped that Sweave's alternative syntax could offer a way out : no
 such luck.
 
 There seems to be no way to delay R evaluation of a R chunk passed by
 Sweave ; at least, the eval=FALSE option of chunk declaration is not
 sufficient for that.
 
 Am I missing something in the Sweave nd odfWeve documentations (that I
 read till I grew green and moldy) ? Or does this require a fundamental
 change in the relevant Sweave drivers ?
 
 Can you suggest alternative ways of doing what I mean to do ? The only
 workaround I found is to paste a second copy of my code in a \verbatim
 environment (or, in the case of odfWeave, in the text part), and spice
 it with \end{verbatim} comments.. \begin{verbatim} chunks. This way, I
 lose any guarantee of consistency between commented text and effective
 code.
 
 Any other idea ?
 
   Emmanuel Charpentier


To summarize the answers I got so far, there seems to be no satisfactory 
solutions to my current problem with either Sweave or odfWeave :
- every (Sw|odfW)eave code chunk has to be parseable in itself. 
One cannot break it in unparseable pieces in home to paste it later ;
- the (hypothetical) parse=FALSE option, suggested by Duncan 
Murdoch, is, well, hypothetical ;
- the brew package is not integrated in either Sweave or 
odfWeave ;
- R-style comments will get you only so far (where is math markup 
when you need it ?) ;
- subdivising work in small units is not a practical solution, 
when your big piece of software is a collection of small parts and 
local variable assignments, embedded in a (perforce large) local 
environment to keep things clean...

Therefore, I let this problem to sleep. However, I Cc this answer (with 
the original question below) to Max Kuhn and Friedrich Leisch, in the 
(faint) hope that this feature, which does not seem to have been missed 
by anybody in 8 years, might be considered sufficiently useful to grant 
addition someday...

Sincerely yours,

Emmanuel Charpentier

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Summary (Re: (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?)

2010-12-12 Thread Duncan Murdoch

On 12/12/2010 6:30 PM, Emmanuel Charpentier wrote:

Dear list,

see comment at end.

On Sat, 11 Dec 2010 22:58:10 +, Emmanuel Charpentier wrote :


Dear list,

Inspired by the original Knuth tools, and for paedaogical reasons, I
wish to produce a document presenting some source code with interspersed
comments in the source (see Knuth's books rendering TeX and metafont
sources to see what I mean).

I seemed to remember that a code chunk could be defined piecewise, like
in

Comments...

Chunk1, eval=FALSE, echo=TRUE=
SomeCode
@

Some other comments...

Chunk2, eval=FALSE, echo=TRUE=
MoreCode
@

And finally,

Chunk3, eval=TRUE, echo=TRUE=
Chunk1
Chunk2
EndOfTheCode
@

That works ... as long as SomeCode, MoreCode and EndOfTheCode are self-
standing pieces of R code, but *not* code fragments. You can *not*
intersperse comments in, say, a function body, or  local() environment
this way : when Sweaving, *R* complains of an incomplete source (makes
noise about an unexpected end of input at the end of Chunk1, IIRC, and
never sees Chunk2).

I hoped that Sweave's alternative syntax could offer a way out : no
such luck.

There seems to be no way to delay R evaluation of a R chunk passed by
Sweave ; at least, the eval=FALSE option of chunk declaration is not
sufficient for that.

Am I missing something in the Sweave nd odfWeve documentations (that I
read till I grew green and moldy) ? Or does this require a fundamental
change in the relevant Sweave drivers ?

Can you suggest alternative ways of doing what I mean to do ? The only
workaround I found is to paste a second copy of my code in a \verbatim
environment (or, in the case of odfWeave, in the text part), and spice
it with \end{verbatim} comments.. \begin{verbatim} chunks. This way, I
lose any guarantee of consistency between commented text and effective
code.

Any other idea ?

Emmanuel Charpentier



To summarize the answers I got so far, there seems to be no satisfactory
solutions to my current problem with either Sweave or odfWeave :
- every (Sw|odfW)eave code chunk has to be parseable in itself.
One cannot break it in unparseable pieces in home to paste it later ;
- the (hypothetical) parse=FALSE option, suggested by Duncan
Murdoch, is, well, hypothetical ;
- the brew package is not integrated in either Sweave or
odfWeave ;
- R-style comments will get you only so far (where is math markup
when you need it ?) ;
- subdivising work in small units is not a practical solution,
when your big piece of software is a collection of small parts and
local variable assignments, embedded in a (perforce large) local
environment to keep things clean...

Therefore, I let this problem to sleep. However, I Cc this answer (with
the original question below) to Max Kuhn and Friedrich Leisch, in the
(faint) hope that this feature, which does not seem to have been missed
by anybody in 8 years, might be considered sufficiently useful to grant
addition someday...



I thought about this a little more, and it's not impossible to solve. 
For example, using this file named test.Rnw:



\documentclass{article}
\usepackage{Sweave}
\SweaveOpts{concordance=TRUE}
\begin{document}

Here is the start:

start,engine=noParse, echo=TRUE=
f - function(x) {
@

Here is the end:

end,engine=noParse, echo=TRUE=
  x + 1
}
@

Here is the whole thing:

=
start
end

f(4)
@

\end{document}
-

The following code does approximately what you want:

-
testRuncode - function(object, chunk, options) {
  if(options$engine == noParse  options$echo) {
  chunkout - object$output
  cat(\\begin{Schunk}\n\\begin{Sinput}\n, file=chunkout)
  cat(chunk[-1], sep=\n, file=chunkout)
  cat(\n\\end{Sinput}\n\\end{Schunk}\n, file=chunkout)
  return(object)
  } else return(utils:::RweaveLatexRuncode(object, chunk, options))
}

testRweave - function() {
  driver - RweaveLatex()
  driver$runcode - testRuncode
  driver
}

Sweave(test.Rnw, driver=testRweave())

--

It could probably use a lot of elaboration, but it's a start.

Duncan Murdoch

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Summary (Re: (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?)

2010-12-12 Thread Gabor Grothendieck
On Sun, Dec 12, 2010 at 6:30 PM, Emmanuel Charpentier
emm.charpent...@free.fr wrote:
 Dear list,

 see comment at end.

 On Sat, 11 Dec 2010 22:58:10 +, Emmanuel Charpentier wrote :

 Dear list,

 Inspired by the original Knuth tools, and for paedaogical reasons, I
 wish to produce a document presenting some source code with interspersed
 comments in the source (see Knuth's books rendering TeX and metafont
 sources to see what I mean).

 I seemed to remember that a code chunk could be defined piecewise, like
 in

 Comments...

 Chunk1, eval=FALSE, echo=TRUE=
 SomeCode
 @

 Some other comments...

 Chunk2, eval=FALSE, echo=TRUE=
 MoreCode
 @

 And finally,

 Chunk3, eval=TRUE, echo=TRUE=
 Chunk1
 Chunk2
 EndOfTheCode
 @

 That works ... as long as SomeCode, MoreCode and EndOfTheCode are self-
 standing pieces of R code, but *not* code fragments. You can *not*
 intersperse comments in, say, a function body, or  local() environment
 this way : when Sweaving, *R* complains of an incomplete source (makes
 noise about an unexpected end of input at the end of Chunk1, IIRC, and
 never sees Chunk2).

 I hoped that Sweave's alternative syntax could offer a way out : no
 such luck.

 There seems to be no way to delay R evaluation of a R chunk passed by
 Sweave ; at least, the eval=FALSE option of chunk declaration is not
 sufficient for that.

 Am I missing something in the Sweave nd odfWeve documentations (that I
 read till I grew green and moldy) ? Or does this require a fundamental
 change in the relevant Sweave drivers ?

 Can you suggest alternative ways of doing what I mean to do ? The only
 workaround I found is to paste a second copy of my code in a \verbatim
 environment (or, in the case of odfWeave, in the text part), and spice
 it with \end{verbatim} comments.. \begin{verbatim} chunks. This way, I
 lose any guarantee of consistency between commented text and effective
 code.

 Any other idea ?

                                       Emmanuel Charpentier


 To summarize the answers I got so far, there seems to be no satisfactory
 solutions to my current problem with either Sweave or odfWeave :
        - every (Sw|odfW)eave code chunk has to be parseable in itself.
 One cannot break it in unparseable pieces in home to paste it later ;
        - the (hypothetical) parse=FALSE option, suggested by Duncan
 Murdoch, is, well, hypothetical ;
        - the brew package is not integrated in either Sweave or
 odfWeave ;

brew doesn't need to be.  Its an alternative to Sweave, not something
intended to be used with Sweave.

-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.