[R] recommended way to group function calls in Sweave

Liviu Andronic landronimirc at gmail.com
Thu Apr 26 20:43:09 CEST 2012


Dear Rainer


On Wed, Apr 25, 2012 at 5:34 PM, Rainer Schuermann
<rainer.schuermann at gmx.net> wrote:
> <<chunk_name_1,eval=FALSE,echo=FALSE>>=
>
I like the 'eval=FALSE' trick.


> SweaveInput( "setup.Rnw" )
>
> and from here, I can suse the named chunks almost like function calls, as you
> you describe below. The advantage (for me) is that I have only one place where
> I maintain the functions code, and only one line in the "real document",
> rather than a lot of code, possibly distributed over the document..
>
This makes sense for LaTeX, but I actually use LyX and things are a
bit more straightforward there. I can put the initialization chunks
within a 'TeX inset' that can be collapsed; this way it contains all
the necessary code, but takes up only one line in the editor.

I attach an example .lyx file. I renamed it to .lyx.txt and cross
finger that r-help will accept the attachment; make sure to rename it
back when trying to open with LyX.

Regards
Liviu
-------------- next part --------------
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass article
\use_default_options true
\begin_modules
sweave
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100

\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Standard
\begin_inset ERT
status collapsed

\begin_layout Plain Layout

<<echo=FALSE, results=hide>>=
\end_layout

\begin_layout Plain Layout

library(xtable)
\end_layout

\begin_layout Plain Layout

d <- iris
\end_layout

\begin_layout Plain Layout

ind <- 1
\end_layout

\begin_layout Plain Layout

@
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

<<chunk_name_1,eval=FALSE,echo=FALSE>>=
\end_layout

\begin_layout Plain Layout

x <- cor(d[ , ind])
\end_layout

\begin_layout Plain Layout

@
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

<<chunk_name_2,eval=FALSE,echo=FALSE>>=
\end_layout

\begin_layout Plain Layout

# produce a nice table from some data
\end_layout

\begin_layout Plain Layout

xtable(x)
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Here's a sample use of Sweave macros.
 
\end_layout

\begin_layout Standard
\begin_inset ERT
status open

\begin_layout Plain Layout

<<echo=F, results=tex>>=
\end_layout

\begin_layout Plain Layout

d <- mtcars
\end_layout

\begin_layout Plain Layout

ind <- 1:3
\end_layout

\begin_layout Plain Layout

<<chunk_name_1>>
\end_layout

\begin_layout Plain Layout

<<chunk_name_2>>
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\end_body
\end_document


More information about the R-help mailing list