alishinski / lavaanPlot

R package for plotting lavaan path models with DiagrammeR
http://alexlishinski.com/lavaanPlot/
38 stars 4 forks source link

Print plots in PDFs #12

Closed jorgesinval closed 2 years ago

jorgesinval commented 5 years ago

Is it possible to knit the diagrams on a PDF?

alishinski commented 5 years ago

This seems to be significantly trickier than it ought to be, but I'm looking into it.

jorgesinval commented 5 years ago

Excellent!

On Tue, Jan 8, 2019 at 4:33 PM Alex Lishinski notifications@github.com wrote:

This seems to be significantly trickier than it ought to be, but I'm looking into it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/alishinski/lavaanPlot/issues/12#issuecomment-452363743, or mute the thread https://github.com/notifications/unsubscribe-auth/AH5QNx-kdVzfUMr-ZvViF9V8TgbDKa0Lks5vBMhdgaJpZM4WOoAp .

alishinski commented 5 years ago

The way that I've dealt with this in the past is simply to export the plot as an image and then include that in the rmarkdown document that I'm going to render to a pdf, but I'd like to avoid that step if possible. It kind of seems like it might be an inherent diagrammeR issue though: https://github.com/rich-iannone/DiagrammeR/issues/133

Deleetdk commented 5 years ago

I am just in general unable to save figures except for manually click Export in RStudio. The documentation does not seem to have anything on this. How does one save figures programmatically?

mattansb commented 4 years ago

I've come across the same problem. Has anyone been able to programmatically save the plots?

mattansb commented 4 years ago

Seems like this is solved by adding always_allow_html: yes in the yaml header!

dongyiyi commented 3 years ago

Yes, I met the same problem. How to save the plot to a pdf file?

tafakkur commented 3 years ago

One possible workaround.

tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, "lavaan_model.html")
htmltools::save_html(lavaan_model,htmlFile)

# webshot supports other formats as well (png, bmp etc.)
# This will create lavaan_model.pdf in the working directory

webshot::webshot(htmlFile,"lavaan_model.pdf") 
jorgesinval commented 3 years ago

Yes, and you can also use pagedown::chrome_print(). But if you only want to directly plot a diagram it is not straightforward.

tafakkur commented 3 years ago

That's true. Probably in the meantime, we could have a function that does all this work in the background. But we'll still only get screenshots and not diagrams.

alishinski commented 3 years ago

I wrote 2 new functions and put them in a new branch called export_functions:

save_png will save your plot as a .png programmatically embed_plot_pdf you can use in an .Rmarkdown document that you knit into a pdf to embed your plot

Give those a try and let me know what you think

alishinski commented 3 years ago

And whether they work, of course

jorgesinval commented 3 years ago

It is working. Both the svg function and the pdf one:

image

jorgesinval commented 3 years ago

So... we always need to create a separate pdf (with only the plot) to then be added to the Rmarkdown one, right?

alishinski commented 3 years ago

Yeah the embed_plot_pdf function converts and saves the plot as a pdf (So you should use a .pdf file name) in your working directory and then uses knitr::include_graphics to put the plot into the knitted (knitr-ed?) .pdf. I haven't figured out how to do it without saving, but please let me know if you have any suggestions.

shangeconnew commented 1 year ago

I wonder where the 'export_functions' is now, you know, the link does not work. @alishinski