Your example works for me. I'd guess there's a problem with the way you handled Sweave.sty, but I don't really know what it would be. Can't you tell MikTex to use the Sweave file from its original location, using -include_directory?

Duncan Murdoch

On 1/6/2009 10:48 AM, Mr Derik wrote:
Thanks for replying.

I'm working on a windows XP sp3 PC.

I am trying to run the test file provided by R at the moment:

################################################################

% -*- mode: noweb; noweb-default-code-mode: R-mode; -*-
\documentclass[a4paper]{article}

\title{A Test File}
\author{Friedrich Leisch}

\SweaveOpts{echo=FALSE}
\usepackage{a4wide}

\begin{document}

\maketitle

A simple example that will run in any S engine: The integers from 1 to
10 are
<<print=TRUE>>=
1:10
<<results=hide>>=
print(1:20)
@ % the above is just to ensure that 2 code chunks can follow each other

We can also emulate a simple calculator:
<<echo=TRUE,print=TRUE>>=
1 + 1
1 + pi
sin(pi/2)
@

Now we look at Gaussian data:

<<>>=
library(stats)
x <- rnorm(20)
print(x)
print(t1 <- t.test(x))
@
Note that we can easily integrate some numbers into standard text: The
third element of vector \texttt{x} is \Sexpr{x[3]}, the
$p$-value of the test is \Sexpr{format.pval(t1$p.value)}. % $

Now we look at a summary of the famous iris data set, and we want to
see the commands in the code chunks:

\SweaveOpts{echo=true}

% the following code is R-specific, as data(iris) will not run in Splus. % Hence, we mark it as R code. <<engine=R>>=
data(iris)
summary(iris)
@ %def


\begin{figure}[htbp]
  \begin{center}
<<fig=TRUE>>=
library(graphics)
pairs(iris)
@
    \caption{Pairs plot of the iris data.}
  \end{center}
\end{figure}

\begin{figure}[htbp]
  \begin{center}
<<fig=true>>=
boxplot(Sepal.Length~Species, data=iris)
@
    \caption{Boxplot of sepal length grouped by species.}
  \end{center}
\end{figure}


% R is not S-PLUS, hence this chunk will be ignored:
<<engine=S4>>=
function.that.comes.only.with.Splus(x)
@

\end{document}


##################################################

I open in Tinn-R 2.1.1.6. I save it, change the working directory in R to
the one I have the file saved in. Then execute Sweave from R with:

Sweave("C:\\R_folder\\sweave_Test\\Sweave-test-1.rnw")

Which produces the following .tex file:


##################################################

% -*- mode: noweb; noweb-default-code-mode: R-mode; -*-
\documentclass[a4paper]{article}

\title{A Test File}
\author{Friedrich Leisch}


\usepackage{a4wide}

\usepackage{Sweave}
\begin{document}

\maketitle

A simple example that will run in any S engine: The integers from 1 to
10 are
\begin{Schunk}
\begin{Soutput}
 [1]  1  2  3  4  5  6  7  8  9 10
\end{Soutput}
\end{Schunk}

We can also emulate a simple calculator:
\begin{Schunk}
\begin{Sinput}
1 + 1
\end{Sinput}
\begin{Soutput}
[1] 2
\end{Soutput}
\begin{Sinput}
1 + pi
\end{Sinput}
\begin{Soutput}
[1] 4.141593
\end{Soutput}
\begin{Sinput}
sin(pi/2)
\end{Sinput}
\begin{Soutput}
[1] 1
\end{Soutput}
\end{Schunk}

Now we look at Gaussian data:

\begin{Schunk}
\begin{Soutput}
[1] -2.45376344 0.88169202 0.60153415 -0.34419029 -2.13665627 -0.06469998 0.02702410 0.73846812 -0.49656363
[10] -0.77085572 -0.64614048  0.31631767 -0.52449644 -0.15666901  1.00128172
-1.42788545  0.94008626  1.76111249
[19] -1.38254184 -0.34449613
\end{Soutput}
\begin{Soutput}
        One Sample t-test

data: x t = -0.929, df = 19, p-value = 0.3645 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: -0.7288798 0.2807356 sample estimates: mean of x -0.2240721 \end{Soutput}
\end{Schunk}
Note that we can easily integrate some numbers into standard text: The
third element of vector \texttt{x} is \Sexpr{x[3]}, the
$p$-value of the test is \Sexpr{format.pval(t1$p.value)}. % $

Now we look at a summary of the famous iris data set, and we want to
see the commands in the code chunks:



% the following code is R-specific, as data(iris) will not run in Splus. % Hence, we mark it as R code. \begin{Schunk}
\begin{Sinput}
data(iris)
summary(iris)
\end{Sinput}
\begin{Soutput}
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50 Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50 Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 \end{Soutput}
\end{Schunk}


\begin{figure}[htbp]
  \begin{center}
\begin{Schunk}
\begin{Sinput}
library(graphics)
pairs(iris)
\end{Sinput}
\end{Schunk}
\includegraphics{Sweave-test-1-006}
    \caption{Pairs plot of the iris data.}
  \end{center}
\end{figure}

\begin{figure}[htbp]
  \begin{center}
\begin{Schunk}
\begin{Sinput}
boxplot(Sepal.Length ~ Species, data = iris)
\end{Sinput}
\end{Schunk}
\includegraphics{Sweave-test-1-007}
    \caption{Boxplot of sepal length grouped by species.}
  \end{center}
\end{figure}


% R is not S-PLUS, hence this chunk will be ignored:

\end{document}

######################################################

Which does indeed have \usepackage{Sweave} in the preamble. I am trying to
run Latex on this file with a MikTex 2.7 distribution via Texnic Center 1.0.
Initially I couldn't get the file to run at all as latex couldn't find
Sweave.sty, but I took that out of the Sweave package file for R and put it
in my MikTex folder and ran the file name database indexer. Since doing so
latex will run and turn out PDF and PS files that contain the Schunks and
figures just fine, however I do get errors like:

! Undefined control sequence.
l.68 $p$-value of the test is \Sexpr
                                    {format.pval(t1$p.value)}. % $

And if you look at this line in the PDF you see:

format.pval(t1p.value)

the $ having been taken to be the beginning of maths mode.

I ran that segment of code the R by hand, it should return a number like:

0.26149

So for some reason latex can handle most of the Sweave control sequences
such as \begin{Schunk} but doesn't recognise \Sexpr.

I've uploaded the PDF I get, if that is of any help.

Cheers

Matt

-----Original Message-----
From: Duncan Murdoch [mailto:murd...@stats.uwo.ca] Sent: Tuesday, January 06, 2009 2:18 PM
To: Mr Derik
Cc: r-help@r-project.org
Subject: Re: [R] Sweave in LATEX

On 1/6/2009 6:44 AM, Mr Derik wrote:
Hello

I have been setting up my computer to run Sweave. I have got the whole
thing
working on example files, except that my MikTex returns an "Undefined
Control Sequence" error for \Sexpr and my output file contains verbatim
code
sequences at the apropriate point in the text rather than the R output.
The
rest of the output file is fine with tables, R code sequences and figures
in
the right place and correctly formatted. I have searched everywhere for
advice on what to do about this, any ideas would be gratefully received.

You need to give more details. Which version of R are you running? How are you running Sweave? Are you including \usepackage{Sweave} in your Sweave document? (This is not always necessary, but is usually a good idea).

Duncan Murdoch
http://www.nabble.com/file/p21312956/Sweave-test-1.pdf Sweave-test-1.pdf
Duncan Murdoch-2 wrote:

On 1/6/2009 6:44 AM, Mr Derik wrote:
Hello

I have been setting up my computer to run Sweave. I have got the whole
thing
working on example files, except that my MikTex returns an "Undefined
Control Sequence" error for \Sexpr and my output file contains verbatim
code
sequences at the apropriate point in the text rather than the R output.
The
rest of the output file is fine with tables, R code sequences and figures
in
the right place and correctly formatted. I have searched everywhere for
advice on what to do about this, any ideas would be gratefully received.

You need to give more details. Which version of R are you running? How are you running Sweave? Are you including \usepackage{Sweave} in your Sweave document? (This is not always necessary, but is usually a good idea).

Duncan Murdoch

______________________________________________
R-help@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.




______________________________________________
R-help@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.

Reply via email to