You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Getting Error on Missing PNG when trying to generate a PDF #40
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.
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.
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:
I tried reducing the out.width, but that didn't solve the PDF creation issue.
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
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 <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
x Building issue-40.html into issue-40.pdf ... failed
Error in force(expr) :
Failed to generate output. Reason: Failed to open http://127.0.0.1:7277/plot.png (HTTP status code: 404)
Run `rlang::last_error()` to see where the error occurred.
but if you first build a self-contained HTML file you can build the PDF with the image included
> 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
Thanks for the excellent detective work on this! I have definitely made progress, but I'm still not there yet.
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.
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!)
But when I try to build the pdf, I get a hand_read_frame error
this error is more or less harmless and usually goes away if you restart your R session and try again.
In this produced PDF, slide 6 is missing its plot, but not slide 5 or 7.
If you're using self_contained = TRUE at this point, the issue is very likely the one pointed out by @jhelvy#40 (comment).
It can be hard to tell visually 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.
To diagnose it, try including this CSS in your slides (you can put this chunk anywhere)
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.
Activity
gadenbuie commentedon Nov 15, 2021
Can you share the source code for that slide?
jhelvy commentedon Nov 15, 2021
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 commentedon Nov 15, 2021
Sure. The presentation references plots I pre-generated in R. The first slide doesn't generate an error:
But the second one does, and has the funny behavior I pointed out above: it is blank when printed.
bshor commentedon Nov 15, 2021
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:
I tried reducing the out.width, but that didn't solve the PDF creation issue.
gadenbuie commentedon Nov 16, 2021
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: trueto the YAML header of your slides under themoon_readeroutput.OR you can use xaringanBuilder to render your slides to HTML first with
build_html(), settingself_contained = TRUEin that calland then calling
build_pdf()on the HTML filebuild_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.Rmdin aslides/directory)Then move into the directory and try to build the PDF file:
but if you first build a self-contained HTML file you can build the PDF with the image included
bshor commentedon Nov 19, 2021
Thanks for the excellent detective work on this! I have definitely made progress, but I'm still not there yet.
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.
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!)
In this produced PDF, slide 6 is missing its plot, but not slide 5 or 7.
gadenbuie commentedon Nov 19, 2021
this error is more or less harmless and usually goes away if you restart your R session and try again.
If you're using
self_contained = TRUEat this point, the issue is very likely the one pointed out by @jhelvy #40 (comment).It can be hard to tell visually 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.
To diagnose it, try including this CSS in your slides (you can put this chunk anywhere)
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.