An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090509/c6a52317/attachment-0001.pl>
Sweave \Sexpr{} advice please
3 messages · Kyle Matoba, Dieter Menne, Tobias Verbeke
Kyle Matoba <kmmatoba <at> ucdavis.edu> writes:
A question in my work I use \Sexpr{} statements scalar values and the xtable
package for all manner of tables. What I'd like to do is to use a vector
inline, rather than a whole separate table. Something like:
% Sweave block:
<<>>=
covmat <- cov(matrix(runif(100),ncol=3))
@
% back to Latex, typing up a report, my homework, etc.
The first column of the covariance matrix is $(\Sexpr{covmat[1,1]},
\Sexpr{covmat[2,1]}, \Sexpr{covmat[3,1]})^T$
To unclutter the text in Sexpr, I normally prepare the results in the Sweave block in advance Dieter covmat <- cov(matrix(runif(30),ncol=3)) ft = paste(round(covmat[,3]),collapse=", ") ft named = c(Val=3,StdDev=4,p=0.4) ft = paste(names(named),named,sep="= ",collapse=", ") ft
Hi Kyle,
First off, my deepest gratitude to the Sweave developers: this tool has
improved my quality greatly.
A question in my work I use \Sexpr{} statements scalar values and the xtable
package for all manner of tables. What I'd like to do is to use a vector
inline, rather than a whole separate table. Something like:
%%%%%%%%%%%%%%%% begin code
% Latex junk
% Sweave block:
<<>>=
covmat <- cov(matrix(runif(100),ncol=3))
@
% back to Latex, typing up a report, my homework, etc.
The first column of the covariance matrix is $(\Sexpr{covmat[1,1]},
\Sexpr{covmat[2,1]}, \Sexpr{covmat[3,1]})^T$
%%%%%%%%%%%%%%%%% end code
but, of course, this is poor way of going about it. Any suggestions?
<<loadPkg, echo=FALSE>>= require(xtable) @ <<>>= covmat <- cov(matrix(runif(99), ncol=3)) # 99 not 100 @ The first row of the\dots is <<firstRow, echo = FALSE, results=tex>>= xtable(covmat[1,,drop=FALSE]) @ This should get you started. Be sure to explore the facilities of the xtable package (you can use captions, labels etc.): ?xtable ?print.xtable The results=tex option to the chunk is important as well, of course. HTH, Tobias