The following Rmarkdown renders the plotly 3D graph in HTML, but not in PDF.
Testing plotly
```{r}
library(plotly)
p <- plot_ly(data=iris, x=~Sepal.Length, y=~Sepal.Width, z=~Petal.Length,
color=~Species, symbols=c(0,1), type="scatter3d", mode="markers")
p
```
A snapshot of the graph appears as follows:
According to the plotly help page:
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.
Is there a way to render the plotly graph in a PDF?
Note: The error from rmarkdown::render()
is:
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.
What I do so that rendering to PDF's work but you can still have the interactive plots in other knit types and in rmarkdown files in r studio is:
this goes in the setup block (or really, anywhere early in the file):
then this is used to render any plotly plot based on what kind of document you are creating:
in the example
base_plot
is the name of the plot.Same issue with R markdown compile error:. You need to choose what format you want to KNIT to, tried to look at mine.
I have created a little workaround, which saves the plotly images locally as png-file and imports it back to the RMD file. You need the package
webshot
, which you can load via:Further more, you need to install phantomjs via
Then (when phantomjs is in your PATH), you can create your RMD file:
This works for me .. perhaps it's a workaround for you, too!
As @hrbrmstr commented,
export()
previously didn't support WebGL at all, but more recent versions support exporting to png via RSelenium (seehelp(export, package = "plotly")
). If you need pdf export, you'll have to pay for a cloud account -- https://plot.ly/products/cloud/