0

I've been trying to debug a .Rnw file I wrote in RStudio for about a week now. The most frustrating part is that it used to work perfectly in its current form but then seemed to inexplicably stop working.

I've tracked down the source of the error to graphics I'm trying to add to the pdf file. It seems that only certain image files break the script. Every graphic that is added to the pdf is created using another R script. Let me know if you need to see that for any reason and I'll make an edit.

Here's the basic format of my .Rnw file:

\documentclass[
  title
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
##Sets up variables in paragraphs and creates data frames.
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
  \includegraphics{Fig1.png}
\end{figure}

\end{document}

When I compile the PDF in RStudio, I get an error Running pdflatex.exe on REPORT.tex...failed

When I look at the log file, I see this:

!pdfTeX error: pdflatex.exe (file C:/Users/myname/Documents/Report/Fig1.png): libpng: 
internal error ==> Fatal error occurred, no output PDF file produced!

Anyone have any ideas what has gone wrong?

I've tried changing the images from .png files to .jpg and .tiff with the same results. I've tried adding a \graphicspath to the preamble and tried adding the full path to the \includegraphics section.

EDIT: Here's how I generated the fig1.png image. This should allow you to recreate an example in RStudio. I had to manually copy this code so typos could exist. Also, it will, of course, look like crap because I just recreated the image format with the example mtcars dataset.

png("C:/Users/UserName/Documents/Report/fig1.png", height = 7, width = 14, units = 'in', res = 350)
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
dev.off()
Neal
  • 199
  • 3
  • 16
  • I think you'll have to put that `Fig1.png` file online somewhere, so others can try it and try to debug it. – user2554330 Mar 28 '17 at 23:59
  • @user2554330 I added an example that you can recreate fig1.png with. – Neal Mar 29 '17 at 13:32
  • I needed to edit your figure code a bit (you have no "=" in "y disp" in the 2nd line), but then it worked fine. I'd suggest you might be trying to load a different file than you think you are. You're on Windows, so the `fig1.png` vs `Fig1.png` difference probably doesn't matter, but I'd fix that. – user2554330 Mar 29 '17 at 14:14
  • I appreciate the edit. Like I said, I expected typos would have crept in. As far as the names. Fig1 is just a placeholder hame. In my actual code, the case matches just fine. – Neal Mar 29 '17 at 14:22
  • Do you see the problem with your sample code? I didn't. – user2554330 Mar 29 '17 at 15:24
  • The sample code doesn't generate the same issue. I haven't been able to create a sample that generates the same issue. – Neal Mar 29 '17 at 17:56
  • Sounds like we're back to my first comment. If others can't reproduce this, they won't be able to diagnose or debug it. – user2554330 Mar 29 '17 at 18:49

0 Answers0