Skip to content

Sweave and \Sexpr{}

5 messages · Eric R., Felipe Carrillo, Dieter Menne

#
Hi: 
I am trying to create a dynamic latex table using \Sexpr{} but it's not evaluating it. I also tried the example below without Sweave and also fails. I have also copied the Sweave.sty to my working directory but nothing seems to work. Do I need to have certain package in order to run \Sexpr{}?

\documentclass[a4paper]{article}
\usepackage{C:/R/R-2.8.1/share/texmf/Sweave}
\begin{document}

<<echo=false,results=hide>>=
library(lattice)
library(xtable)
data(cats, package="MASS")
@ 

\section*{The Cats Data}

Consider the \texttt{cats} regression example from Venables \& Ripley
(1997). The data frame contains measurements of heart and body weight
of \SExpr{nrow(cats)} cats (\Sexpr{sum(cats$Sex=="F")} female,
\Sexpr{sum(cats$Sex=="M")} male).

A linear regression model of heart weight by sex and gender can be
fitted in R using the command
<<>>=
lm1 = lm(Hwt~Bwt*Sex, data=cats)
lm1
@ 
Tests for significance of the coefficients are shown in
Table~\ref{tab:coef}, a scatter plot including the regression lines is
shown in Figure~\ref{fig:cats}.

\SweaveOpts{echo=false}

<<results=tex>>=
xtable(lm1, caption="Linear regression model for cats data.", label="tab:coef")
@ 

\begin{figure}
  \centering
<<fig=TRUE,width=12,height=6>>=

print(xyplot(Hwt~Bwt|Sex, data=cats, type=c("p", "r")))
@  
  \caption{The cats data from package MASS.}
  \label{fig:cats}
\end{figure}
\begin{center}
\end{center}

\end{document}


Felipe D. Carrillo  
Supervisory Fishery Biologist  
Department of the Interior  
US Fish & Wildlife Service  
California, USA
#
Felipe Carrillo <mazatlanmexico <at> yahoo.com> writes:
... rest removed

Your example works well in my setting when I replace the usepackage by

\usepackage{Sweave}

which is on my file name database path of latex. Make sure you run "update file
name database".

Maybe better start with a tiny example, such as

\documentclass[a4paper]{article}
\usepackage{Sweave}
\begin{document}
\Sexpr{"Hello"}
\end{document}

to locate the problem more precisely.

Dieter
#
Felipe:

See Leisch's FAQ A18.  

http://www.statistik.lmu.de/~leisch/Sweave/FAQ.html#x1-20000A.18

You likely have R2HTML loaded (likely because you are using Tinn-R) and that
causes some difficulties for Sweave that are easily solved as described in
the FAQ.
Felipe Carrillo wrote:

  
    
#
Thaks Eric and Dieter:
It is working now although I still have to specify the path to Sweave.sty \usepackage{C:/R/R-2.8.1/share/texmf/Sweave}
and it doesn't evaluate \Sexpr{} when I simply use \usepackage{Sweave}. I placed Sweave.sty in the same folder where my rnw and tex file reside.
--- On Thu, 2/5/09, Eric R. <ericr at mcs.st-and.ac.uk> wrote:

            
#
Felipe Carrillo <mazatlanmexico <at> yahoo.com> writes:
\usepackage{C:/R/R-2.8.1/share/texmf/Sweave}
placed Sweave.sty in the same
If it works, keep it. If you, like me, do not like explicit paths, either 
copy Sweave.sty to the local tex directory and update your database; in 
Miktex, this is from the settings gui.

Or add the Sweave path in share to you local tex directory (from the
same dialog, next tab).

Dieter