Skip to content

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

Closed

Description

@bshor

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

Activity

  1. gadenbuie commented on Nov 15, 2021

    @gadenbuie
    Collaborator

    Can you share the source code for that slide?

  2. jhelvy commented on Nov 15, 2021

    @jhelvy
    Owner

    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.

  3. bshor commented on Nov 15, 2021

    @bshor
    Author

    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
    
    ```{r echo=FALSE, out.width="85%", fig.align='center'}
    knitr::include_graphics("../../Plots/Legislatures_2021/States/legislatures_party_density.png")
    ```
    
  4. bshor commented on Nov 15, 2021

    @bshor
    Author

    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.

  5. gadenbuie commented on Nov 16, 2021

    @gadenbuie
    Collaborator

    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()
    ```
    
    ![](../plot.png)
    

    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
    

    image

  6. bshor commented on Nov 19, 2021

    @bshor
    Author

    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.

    image

  7. gadenbuie commented on Nov 19, 2021

    @gadenbuie
    Collaborator

    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)

    ```{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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions