Closed bshor closed 2 years ago
Can you share the source code for that slide?
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.
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.
knitr::include_graphics("../../Plots/Legislatures_2021/States/legislatures_party_density.png")
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
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 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!)
> 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_…
In this produced PDF, slide 6 is missing its plot, but not slide 5 or 7.
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 https://github.com/jhelvy/xaringanBuilder/issues/40#issuecomment-969367425.
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)
```{css echo=FALSE}
img { outline: 2px solid red; }
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)
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.
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.