[R] declaring constants in an Sweave / LaTeX document

Duncan Murdoch murdoch at stats.uwo.ca
Sat Nov 22 16:45:13 CET 2008


On 22/11/2008 2:01 AM, Kyle Matoba wrote:
> List,
> 
> I would like to set a variable to hold, say, the size of my plots in a
> Sweave document.  i.e. something like the following in my '.Rnw' file:
> 
> ==============================================================================
> smallPlotSize = 4
> 
> <<fig1, echo=false, results=hide, height=smallPlotSize, width=smallPlotSize,
> fig=true>>=
> dat <- read.table("
> http://www.stanford.edu/~xing/statfinbook/_BookData/Chap05/q_us_gdp.txt",
> skip=2, header=T)
> 
> GDP <- ts(data=dat$VALUE, start=c(1947,1), frequency=4)
> 
> acf(GDP, type="correlation", main= "ACF of US GDP")
> @
> ==============================================================================

I don't think that is possible, but you can do things like

\SweaveOpts{height=3.5, width=7}

anywhere in your document to set the default size for subsequent plots.

You can also set LaTeX macros to change the displayed size, e.g.

\newcommand{\Fullwidth}{\setkeys{Gin}{width=\textwidth}}
\newcommand{\Smallwidth}{\setkeys{Gin}{width=4in}}

Here you're talking to LaTeX, not to R, so those are setting options to 
the \includegraphics{} calls.  Unfortunately, you can't put \SweaveOpts 
in the macro to also set the R width and height, because Sweave handles 
all the \SweaveOpts calls before calling LaTeX.

Duncan Murdoch

> This way I can tweak things if I have to include a bunch of graphics of the
> same size in a file.  I am not great with LaTeX and still very new to using
> Sweave, so I was wondering what the preferred method of doing something like
> this is between LaTeX and Sweave.  I have seen some people doing some tricky
> stuff from within R such that when their code is preprocessed it is compiled
> as LaTeX source, but this seems like an ugly hack for something so simple.
> 
> Please forgive me if this has been addressed elsewhere; I have found
> documentation on Sweave to be rather sparse.
> 
> Best,
> 
> Kyle
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list