How would it be possible to save a graph from plotly into the pdf.
fit = qpgraph(f2blocks, graph, return_fstats = TRUE)
fit$score
fit$edges %>% plotly_graph
fit$f4 %>% slice_max(abs(z))
Thanks!
CodePudding user response:
I don't see an obvious place where a plot object was assigned a name. Once you have a name, let's say it's named p
then this is now tested code:
library(plotly)
library(reticulate)
py_install("plotly")
p <- plot_ly(x = 1:10, type="histogram")
# without the type parameter the help page example gives warning
reticulate::py_run_string("import sys") # not loaded by default?
reticulate::py_run_string("import plotly") # Also needed?
save_image(p, "./pic.png")
save_image(p, "./pic.pdf") # checked; both were created
file.show(tmp) # to see if it succeeded
CodePudding user response:
I have tried pdf()...dev.off()
, pandoc_convert()
, and webshot()
, but I failed to produce a pdf file from a plotly
object.
Then, I check the class of the plotly object. It turns out that the object inherits htmlwidget
class.
I then used saveWidget()
from {htmlwidget}
to save the plotly object as a html file, and then I used chrome_print
from {pagedown}
to convert the html file to a pdf file.
Here I use the example in the documentation
library(admixtools)
library(htmlwidgets)
library(pagedown)
fit = qpgraph(example_f2_blocks, example_graph)
yourgraph = fit$edges %>% plotly_graph
saveWidget(yourgraph, file = "your_graph.html")
chrome_print("your_graph.html", output = "your_graph.pdf")
The result: