0

I have a plot with millions of datapoints, so I am looking to first make a png then include it. However, I've run into the problem of not being able to include png when I go to compile time.

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htb]
<<fig=TRUE,echo=FALSE>>=
png('test.png')
plot(rnorm(100))
dev.off()
@
\includegraphics{test}
\end{figure}

\end{document}

After calling the above MWE I will then go to my R console just like usual and call:

Sweave("report.Rnw")
texi2pdf("report.tex")

Which works all the time unless I have the above code in my Rnw file. The error message:

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
  Running 'texi2dvi' on 'report.tex' failed.
LaTeX errors:
!pdfTeX error: pdflatex (file ./report-019.pdf): PDF inclusion: requir
ed page does not exist <0>
 ==> Fatal error occurred, no output PDF file produced!
> Sweave("report.Rnw") ; texi2pdf("report.tex")
Writing to file report.tex
user2763361
  • 3,789
  • 11
  • 45
  • 81

4 Answers4

1

First, create a function to output your .png (or .pdf, or whatever) file. I like to create a separate folder for this (images_plot below).

for (i in x) {
  # set a real filename here, instead of 'i'
  pdf(paste('images_plot/', i, '.pdf', sep = ''), width = 10, height = 5)
  plot(x)
  dev.off()
}

Then use Tex to show it:

<<echo = FALSE, results=tex>>=
for (i in x)
{
  cat('\\begin{figure}[h]\n')
  file = paste('images_plot/', i, '.pdf', sep = '')
  cat('\\includegraphics{', file, '}\n', sep = '')
  cat('\\end{figure}\n')
}
@

That's how i do it, hope it helps!

Fernando
  • 7,785
  • 6
  • 49
  • 81
1

It is erroring because your chunk has fig=TRUE but does not produce any Sweave plots there.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Neal Fultz
  • 9,282
  • 1
  • 39
  • 60
0

If you have the option of using knitr instead Sweave, then you can just specify dev="png" in the chunk options. It will even set up the figure environment for you if you specify the fig.cap argument:

\documentclass{article}

\begin{document}

<<test,dev="png",fig.cap="My figure",fig.pos="htb">>=
plot(rnorm(100))
@

\end{document}
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
-1

Sorry to be so late, what I would do is just use this echo option:

\documentclass[11pt]{article}
\usepackage{graphicx, verbatim}

\begin{document}

<<fig=TRUE,echo=FALSE>>=
boxplot(rnorm(100))
@

\end{document}
Fra Contin
  • 53
  • 1
  • 6
  • But the OP had the same parameter. Your answer does not clarify that it works nor does it explain sufficiently to anyone seeking help what the (unproven) change does. – Shawn Mehan Oct 23 '15 at 21:15
  • Yeah does not clarify anything, just wanted to make it work in a neater way. – Fra Contin Oct 24 '15 at 21:30