Plotly as png in knitr/rmarkdown

后端 未结 4 1271
一生所求
一生所求 2020-12-13 11:12

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.Lengt         


        
4条回答
  •  孤街浪徒
    2020-12-13 11:39

    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:

    install.packages("webshot")
    

    Further more, you need to install phantomjs via

    webshot::install_phantomjs()
    

    Then (when phantomjs is in your PATH), you can create your RMD file:

    ---
    title: "Untitled"
    output: pdf_document
    ---
    
    ```{r}
    library(plotly)
    p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
    
    tmpFile <- tempfile(fileext = ".png")
    export(p, file = tmpFile)
    ```
    ![Caption for the picture.](`r tmpFile`)
    

    This works for me .. perhaps it's a workaround for you, too!

提交回复
热议问题