Hello,
There's a plotly chart which can be produced in HTML output but not in PDF output. The following error shows up:
Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
Execution halted
If I set "always_allow_html: yes", the plot also can't be produced in the PDF-file.
There's an entry to this subject on https://plot.ly/r/knitr/#embedding-plotly-graphs-in-rmd-files:
If you are using rmarkdown with HTML output, printing a plotly object in a code chunk will result in an interactive HTML graph. When using rmarkdown with non-HTML output, printing a plotly object will result in a png screenshot of the graph.
Does anybody know why there's no png screenshot in PDF-output?
Thanks!
A reproducible example would help me diagnose. See here for another approach via export().
@cpsievert, here is an example of an .Rmd file that you can Knit successfully. But if you try to Knit to Word or PDF, for example, you'll get the issue of the topicstarter. So what should one do to get the png instead of html?
````
title: "Test report"
output: html_document
params:
{r}
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price, color = ~carat,
size = ~carat, text = ~paste("Clarity: ", clarity))
````
Use export() to write to disk (png, pdf, etc), then knitr::include_graphics() or similar to embed the static image. It's a bit more work, but more robust than knitr's default behavior (which uses webshot)
As far as I understood, export() itself requires webshot. What do you mean by knitr's default behavior (which uses webshot)?
export() itself requires webshot.
In some cases, yes, but not always (it can't render WebGL, for instance)
I just found an extremely simple answer - I had to update the plotly package and everything started working just fine out of the box.
Same problem, with plotly 4.7.0. Updating not working.
Same problem with plotly 4.7.1, no figures when rendering to pdf or word from rstudio.
It used to work before without loading any additional package.
All I had to do was to load the webshot library on the Rmd file. No need to add any other commands, it worked as soon as I added the library. Now I can knit to html and doc without modifying the document.
@cpsievert I am not sure why this issue is closed. The issue still persists with the latest versions of R and plotly and having to export/orca a png version of the image and then re-include this is an unpleasant hack which really shouldn't be expected for a simple knit to pdf.
Similarly having to add always_allow_html: yes is not ideal, albeit only a smaller headache.
It is an annoying problem.
Absolutely!! Please re-open this issue @cpsievert
I got a work-around. If you first bild your plot layers in ggplot2 and then pass it to ggplotly the plot is displayed. At least in the html.
@philippgrafendorfe my problem is, that I have a lot of plotly objects already and I want to create an export button in shiny, but I am not able to install external packages easily on the server, so currently I do not have a possibility as the "export" function does not work and "orca" requires external package.
There is also no possibility to convert a plotly object to a ggplot object.
Another work around which I just found it to WebShot, which will take a screenshot and then include that. I am not sure if this should be required, but it is a work around for now.
install.packages('webshot')
webshot::install_phantomjs()
cf the following:
Under the hood I understand this is doing similar things to the orca solution, but it seems to do it automatically, which is a bit nicer in terms of cleaner code, albeit if you want to fit a lot onto the window such that it would normally have scroll bars, then these seem to be included, but it seems a small price to pay.
Most helpful comment
@cpsievert I am not sure why this issue is closed. The issue still persists with the latest versions of R and plotly and having to export/orca a png version of the image and then re-include this is an unpleasant hack which really shouldn't be expected for a simple knit to pdf.
Similarly having to add
always_allow_html: yesis not ideal, albeit only a smaller headache.