jhelvy / renderthis

An R package for building xaringan slides into multiple outputs, including html, pdf, png, gif, pptx, and mp4.
https://jhelvy.github.io/renderthis
Other
173 stars 12 forks source link

Getting Error on Missing PNG when trying to generate a PDF #40

Closed bshor closed 2 years ago

bshor commented 2 years ago

Hello,

I'm running the latest versions of R and Rstudio on Arch-derived Garuda Linux. I'm also running the latest version of Google Chrome.

I want to output PDF from my Xaringen slides (I can see the HTML just fine).

But I get the following error on a specific slide.

> xaringanBuilder::build_pdf("Presentations/USC2021/statepolarization_2021.Rmd")
✓ Building Presentations/USC2021/statepolarization_2021.Rmd into Presentations/USC2021/statepolarization_2021.html ... done
x Building Presentations/USC2021/statepolarization_2021.html into Presentations/USC2021/statepolarization_2021.pdf ... failed
Error in force(expr) : 
  Failed to generate output. Reason: Failed to open http://127.0.0.1:7799/Plots/Legislatures_2021/States/legislatures_party_density.png (HTTP status code: 404)

It seems like it is trying to open a file on a web server? Why? I have the plot in my file system.

I can see the presentation perfectly in the Moon Reader and when I open the html presentation in Chrome.

Interestingly, when I try to print the presentation from Chrome itself, it is missing several plots, including the one that xaringanBuilder complains about. Here's the screenshot of what I mean. Notice that the PDF preview is missing that plot, when the underlying HTML display in Chrome looks fine.

image

gadenbuie commented 2 years ago

Can you share the source code for that slide?

jhelvy commented 2 years ago

I've had some issues in the past with images that go beyond the page margins, not sure if that is the source of the issue here though as it doesn't typically throw an error.

bshor commented 2 years ago

Can you share the source code for that slide?

Sure. The presentation references plots I pre-generated in R. The first slide doesn't generate an error:

# Change in Ideal Points Distributions

```{r echo=FALSE, out.width="50%", fig.align='center'}
knitr::include_graphics("../../Plots/Legislatures_2021/States/legislatures_year_party_density.png")

But the second one does, and has the funny behavior I pointed out above: it is blank when printed.

1996 and 2020

knitr::include_graphics("../../Plots/Legislatures_2021/States/legislatures_party_density.png")
bshor commented 2 years ago

I've had some issues in the past with images that go beyond the page margins, not sure if that is the source of the issue here though as it doesn't typically throw an error.

Interesting. The slide that gives me a problem is expanded to 85% instead of 50%. But it doesn't go beyond the page margins in the Moon Reader previewer:

image

I tried reducing the out.width, but that didn't solve the PDF creation issue.

gadenbuie commented 2 years ago

The problem is that the plot you've created isn't in the same directory as your slides. This is one of the ways that xaringan is somewhat different from other R Markdown outputs — the default target of xaringan slides are the web, where you'd need to copy all the files onto a web server, so everything needs to be contained in the same folder.

There is a work around, however. You can add self_contained: true to the YAML header of your slides under the moon_reader output.

OR you can use xaringanBuilder to render your slides to HTML first with build_html(), setting self_contained = TRUE in that call

library(xaringanBuilder)
build_html("Presentations/USC2021/statepolarization_2021.Rmd", self_contained = TRUE)

and then calling build_pdf() on the HTML file

build_pdf("Presentations/USC2021/statepolarization_2021.html")

Here's a small reprex that demonstrates the issue. Put the following in slides/issue-40.Rmd (i.e. issue-40.Rmd in a slides/ directory)

---
output:
  xaringan::moon_reader:
    seal: false
---

This slide makes a plot **in the parent directory** and then includes the image.

```{r plot, results="hide"}
png("../plot.png", width = 8, height = 4, units = "in", res = 150)
hist(faithful$waiting)
dev.off()


Then move into the directory and try to build the PDF file:

build_pdf("issue-40.Rmd") [WARNING] This document format requires a nonempty element. Defaulting to 'issue-40.knit' as the title. To specify a title, use 'title' in metadata or --metadata title="...". ✓ Building issue-40.Rmd into issue-40.html ... done x Building issue-40.html into issue-40.pdf ... failed Error in force(expr) : Failed to generate output. Reason: Failed to open <a href="http://127.0.0.1:7277/plot.png">http://127.0.0.1:7277/plot.png</a> (HTTP status code: 404) Run <code>rlang::last_error()</code> to see where the error occurred.</p> <pre><code></code></pre> </blockquote> <p>but if you first build a self-contained HTML file you can build the PDF with the image included</p> <pre><code>> build_html("issue-40.Rmd", self_contained = TRUE) [WARNING] This document format requires a nonempty <title> element. Defaulting to 'issue-40.knit' as the title. To specify a title, use 'title' in metadata or --metadata title="...". ✓ Building issue-40.Rmd into issue-40.html ... done > build_pdf("issue-40.html") ✓ Building issue-40.html into issue-40.pdf ... done</code></pre> <p><img src="https://user-images.githubusercontent.com/5420529/142018769-e5cac447-98bc-4d12-a71d-7146f7b35b38.png" alt="image" /></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/bshor"><img src="https://avatars.githubusercontent.com/u/468070?v=4" />bshor</a> commented <strong> 2 years ago</strong> </div> <div class="markdown-body"> <p>Thanks for the excellent detective work on this! I have definitely made progress, but I'm still not there yet.</p> <p>When I build the html file with the self_contained=TRUE argument, I get a correctly built HTML file with no errors. It displays perfectly in Chrome if I click on it.</p> <p>But when I try to build the pdf, I get a hand_read_frame error, and the PDF that is built is missing a bunch of my plots (but not all of them!)</p> <pre><code>> xaringanBuilder::build_pdf("Presentations/USC2021/statepolarization_2021.html") [2021-11-19 13:39:41] [error] handle_read_frame error: asio.system:104 (Connection reset by peer)rization_… </code></pre> <p>In this produced PDF, slide 6 is missing its plot, but not slide 5 or 7.</p> <p><img src="https://user-images.githubusercontent.com/468070/142675364-a0984a2e-5a24-471b-a1ec-a4dc144e9e40.png" alt="image" /></p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/gadenbuie"><img src="https://avatars.githubusercontent.com/u/5420529?v=4" />gadenbuie</a> commented <strong> 2 years ago</strong> </div> <div class="markdown-body"> <blockquote> <p>But when I try to build the pdf, I get a hand_read_frame error</p> </blockquote> <p>this error is more or less harmless and usually goes away if you restart your R session and try again.</p> <blockquote> <p>In this produced PDF, slide 6 is missing its plot, but not slide 5 or 7.</p> </blockquote> <p>If you're using <code>self_contained = TRUE</code> at this point, the issue is very likely the one pointed out by @jhelvy <a href="https://github.com/jhelvy/xaringanBuilder/issues/40#issuecomment-969367425">https://github.com/jhelvy/xaringanBuilder/issues/40#issuecomment-969367425</a>.</p> <p>It can be hard to tell <em>visually</em> if the plot extends past the margins of the slide because the actual image may include empty space that you can't see. It's a weird thing with Chrome that if any part of the image is clipped the whole image may be missing from the printed version.</p> <p>To diagnose it, try including this CSS in your slides (you can put this chunk anywhere)</p> <pre><code class="language-markdown">```{css echo=FALSE} img { outline: 2px solid red; }</code></pre> <pre><code> This will add a red outline around your plots and might help you see if the plot is extending past the slide margins. Here's an example where it might look like the plot fits in the slide, but the outline shows that it doesn't. ![image](https://user-images.githubusercontent.com/5420529/142676484-375d77bf-4bf8-4a8e-aaf0-fc5823b6d38e.png)</code></pre> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>