I am new to R, Sweave, and LaTeX. I am running StatET with Eclipse 3.7 on a Windows 7 x64 OS. StatET also includes the Sweave add-on, which I use to try and compile a PDF document.
I also installed MiKTeX 2.9, because, previously, R could not execute texi2dvi (presumably, because there was no TeX runner). Basically, no .tex file could be compiled into PDF file. After I installed MiKTeX, I was successful at compiling a .pdf file from .tex file.
However, the moment I introduce pdf() function into the .Rnw file, I get a "fatal" error.
When trying to compile this simple .Rnw file:
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{pdftest.Rnw}
\author{Egor}
\maketitle
<<echo=F, results=hide>>=
pdf(file='figure1.pdf')
plot(1:10,1:10)
dev.off()
@
\end{document}
I get the following error message:
Sweave(file = "C:/Users/Egor/Desktop/R/Head-tracking/pdftest.Rnw") Writing to file pdftest.tex Processing code chunks with options ... 1 : term hide
You can now run (pdf)latex on 'pdftest.tex'
require(tools)
texi2dvi(file = "C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex", pdf = TRUE)
Error: running 'texi2dvi' on 'C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex' failed
LaTeX errors: C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex:13: ==> Fatal error occurred, no output PDF file produced!
In addition: Warning message: running command '"C:\PROGRA~1\MIKTEX~1.9\miktex\bin\x64\texi2dvi.exe" --quiet --pdf "C:/Users/Egor/Desktop/R/Head-tracking/Data/pdftest.tex" -I "C:/Program Files/R/R-2.13.1/share/texmf/tex/latex" -I "C:/Program Files/R/R-2.13.1/share/texmf/bibtex/bst"' had status 1
I'm following the method described by Ista Zahn in his guide Learning to Sweave in APA Style: http://www.tug.org/pracjourn/2008-1/zahn/zahn.pdf.
The method I'm going to present here is specifically chosen because it makes it easier to produce figures that are sized appropriately, with appropriately sized axis labels and legends (something that can be tricky with other methods). Specifically, I recommend creating your graphic and saving it to a .pdf file, and then inserting the image into a figure environment.
Unfortunately, I cannot include the image file of the code, because I am a new user. It is on page 14 of Ista's guide, but here's an ugly version of it:
<<echo=f a l s e , r e s u l t s=hide>>=
pdf ( f i l e=" spl o t 1 2 . pdf " , width=6, he i ght=5)
qplo t ( educat ion , income , shape=type , s i z e=women , c o l our=p r e s t i g e ,
xlab="Education " , ylab="Income ")
dev . of f ( )
@
nbeginf f i g u r e g
ncentering
n i n c l u d e g r a p h i c s [ width=6in , he i ght=5in ] f spl o t 1 2 . pdf g
ncaptionfA s c a t t e r p l o t d i s p l a y i n g the r e l a t i o n s h i p s
among Income , Education , Pr e s t i g e , Type o f occupat ion ,
and pe r c entage o f Women in the f i e l d . g
n label f f i g : f i g 1 2 g
nendf f i g u r e g
I did try to just plot without an intermediate .pdf file, but using only height and width options of \includegraphics gave me distorted figures. Sorry, it is not clear from the first code I provided that I am later planning on recalling this figure with LaTeX code, because I just wanted to concentrate on getting a pdf() function to work inside the <<>>=...@ thing. It clearly doesn't for me, whereas it does for Ista.
I decided to create the .pdf image files in a separate .R file, and then compile the comprehensive .pdf file with Sweave, but that's just an intermediate solution.
Thanks! --Egor