2017-10-16 106 views
3

我正在使用bookdown包與RMarkdown生成類似於this的基於網絡的書籍,同樣可以選擇下載PDF版本的書籍。RMarkdown:積攢書款

我已經列入我的「書」這本書的HTML版本很好地工作plotly圖。在交互時,當在YAML頭中包含pdf輸出時,按鈕「build book」會引發錯誤。

根據this description我找到了一個常規RMarkdown文件的解決方法,用plotly圖形輸出創建pdf。一個最小的解決方案(外bookdown)看起來是這樣的:

--- 
title: "test" 
output: 
    pdf_document: default 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
library(plotly) 

Sys.setenv("plotly_username" = "username") 
Sys.setenv("plotly_api_key" = "API") 
``` 


```{r cars} 
library(plotly) 
p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines') 
plotly_IMAGE(p, format = "png", out_file = "output.png") 

``` 
![Caption for the picture.](output.png) 

有沒有一種辦法,包括bookdown內這種解決方案,從而使圖形會被自動地在PDF中的HTML輸出和靜態(PNG)互動輸出?

回答

0

基於this blogpost我能拿出一個解決方案。請注意,這僅適用

  • 與 「knitr」 按鈕(見this post的解決方法)
  • 與互聯網連接(1)和Plotly帳戶(2)

(1)查看this link本地出口的靜態圖像(我沒有去上班,因爲我沒有安裝PhantomJS

(2)Plotly擁有目前100地塊用戶配額/每

天每個用戶的網格
--- 
title: "test" 
output: 
    html_document: default 
    pdf_document: default 
    word_document: default 
--- 

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE) 
library(plotly) 
library(pander) 
Sys.setenv("plotly_username" = "YOUR PLOTLY USERNAME") 
Sys.setenv("plotly_api_key" = "API KEY") 

output <- knitr::opts_knit$get("rmarkdown.pandoc.to") # html/latex/docx 

``` 


```{r, results="asis"} 

print(output) 

p <- plot_ly(x = c(1,2,3,4), y = c(2,4,1,3), type = 'scatter', mode = 'lines') 

filename <- "output.png" 

if(output %in% c("latex","docx")){ 
    plotly_IMAGE(p, format = "png", out_file = filename) 
    pandoc.image(filename) 
} else if(output == "html"){ 
    p 
} else(print("No format defined for this output filetype")) 


``` 
+0

使用HTML作爲輸出格式不想要輸出的全'plotly'對象,以便用戶可以與它進行交互? –

+1

我的問題是與生成的書_pdf_版本 – Ratnanil