[R] Need Help: User Defined R Functions in Sweave/Latex

Marc Schwartz marc_schwartz at comcast.net
Thu Jun 21 14:55:52 CEST 2007


On Wed, 2007-06-20 at 22:50 -0500, M. Jankowski wrote:
> Dear all,
> 
> I want to start my post by expressing my sincere gratitude for all the
> help this group has given me in Sweave/Latex/R. The tools are
> excellent and so is the community!
> 
> On to the question.
> 
> My Sweave code is intended to make lots of plots and create a *.pdf
> document. Sweave is perfect for this. The only problem is that  I find
> myself using the same R code, within my Sweave input file over an
> over. I know about Latex macros and I can even get R functions,
> essentially used as subroutines, to sort of work. "\Sexpr{}" will not
> work because the R code I want to use over and over is in the R
> environment. I've tried numerous ways to tackle this process and could
> really use some help. If there is some easier way to do this please
> let me know!
> 
> This is the R function:
> basicplot <- function(x, nplots, sectionname){
> # Begin to make figure here
> file = paste("scatter",nplots, ".pdf", sep="")
> pdf(file = file,paper="special", width=6, height = 6)
> plot(x)
> dev.off()
> cat("\\begin{figure}\n")
> cat("\\includegraphics{",file,"}\n", sep="")
> cat("\\caption{", sectionname,"}\n", sep = "")
> cat("\\end{figure}\n")
> #End figure making
> }
> 
> The aim is to generate Latex code which will have some basic
> information as part of the caption. The trouble seems to be that the
> output from the function appears to latex as if it is protected R code
> when I really want to create output that pdflatex will act on.
> Essentially, the resulting *.pdf contains the lines output by the cat
> function in basicplot. Or:
> \begin{figure}
> \includegraphics{scatter1.pdf}
> \caption{myname}
> \end{figure}
> These lines are not in the environment acted by Latex. I tried a
> variant of the function where <<results=tex,echo=FALSE>> and received
> the same result. Below are the files *.Snw -> *.tex -> *.pdf and the
> output I received while compiling. If there is anything else I can
> give to help you help me just let me know. Thanks!
> 
> Matt


Matt,

I don't know if you have reviewed the Sweave manual, but section 3.4
provides insight into reusing named code chunks, as an alternative to
looping or creating LaTeX macros as proposed by Dieter. These can be
"nested" within other named code chunks and/or R code.

If you don't yet have the manual, it is available here from Fritz' site:

http://www.ci.tuwien.ac.at/~leisch/Sweave/Sweave-manual-20060104.pdf

You could consider using a figure chunk, if the sole output is the plot
and not text. That way you don't have to worry about explicitly cat()ing
the figure related LaTeX markup as you are doing above. Thus, the LaTeX
markup for things like captions will be in LaTeX sections of the .Rnw
file and you can use \Sexpr{}'s to include derived scalar values.

The preferred approach may be to an extent dependent upon whether your
final document will simply have several plots on one or more pages in
sequence, or whether you will need to have text between the plots, such
that you really need LaTeX sections between the figure/code chunks.

Back to Dieter's solution for a moment, take note of the Sweave FAQ
(also in the above manual), specifically FAQ A.9, which covers the issue
of figure chunks and multiple plots, proposing a looping approach
consistent with Dieter's.

I hope that this might give you some other insights into alternative
approaches.

HTH,

Marc Schwartz



More information about the R-help mailing list