【问题标题】:Embedding a list of plotly figures in an Rmarkdown html report [duplicate]在 Rmarkdown html 报告中嵌入一系列图表 [重复]
【发布时间】:2017-06-25 21:14:33
【问题描述】:

我错过了这是一个非常简单的东西,所以希望应该是一个简单的修复。

我正在一个Rmarkdown 代码块中生成plotly 热图列表,然后想在下一步循环中将它们嵌入到报告中。

我正在尝试:

---
title: "test"
output: html_document
---

```{r setup, include=FALSE,echo=FALSE}
knitr::opts_chunk$set(echo=FALSE,out.width='1000px',dpi=200,fig.keep="all")
options(width = 1000)
options(knitr.table.format = "html")

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(knitr))
```


```{r heatmaps, include=FALSE,echo=FALSE}
set.seed(1)
heatmaps.list <- vector(mode="list",3)
for (i in 1:3) heatmaps.list[[i]] <- plot_ly(z = matrix(rnorm(10000),100,100), type = "heatmap")
```

```{r plots, include=FALSE,echo=FALSE}
for (i in 1:3){
  heatmaps.list[[i]]
  cat("\n")
}
```

不幸的是,尽管热图是在 heatmaps Rmarkdown 代码块中生成的,但它并未嵌入热图。

如果我用base 试试这个情节似乎可行:

```{r heatmaps, include=FALSE,echo=FALSE}
set.seed(1)
hist.list <- vector(mode="list",3)
for (i in 1:3){
  hist.list[[i]] <- hist(rnorm(10000),plot=F)
}
```

```{r plot, warning=FALSE,message=FALSE,echo=FALSE}
for (i in 1:3){
  plot(hist.list[[i]])
  cat("\n")  
}
```

【问题讨论】:

    标签: r knitr r-markdown plotly heatmap


    【解决方案1】:

    即使设置更简单,我也会遇到同样的错误

    ---
    output: html_document
    ---
    
    ```{r setup}
    suppressPackageStartupMessages(library(plotly))
    
    # this works
    plot_ly(z = matrix(rnorm(100), 10, 10), type = "heatmap")
    
    # this does not
    set.seed(1)
    for (i in 1:3)
      plot_ly(z = matrix(rnorm(100), 10, 10), type = "heatmap")
    ```
    

    您要保存绘图 ab 对象的事实不是问题,而是循环。 here 的答案是使用

    htmltools::tagList(as_widget(plot1), as_widget(plot2))
    

    相反。对于我的最小示例,这将是。

    ---
    output: html_document
    ---
    
    ```{r setup}
    suppressPackageStartupMessages(library(plotly))
    
    # now it works
    set.seed(1)
    l <- htmltools::tagList()
    for (i in 1:3) 
      l[[i]] = as_widget(plot_ly(z = matrix(rnorm(100), 10, 10), type = "heatmap") )
    l
    ```
    

    很遗憾,我没有代表来标记重复项。

    【讨论】:

      猜你喜欢
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2017-09-26
      • 2017-01-14
      • 2020-04-14
      • 1970-01-01
      • 2015-12-12
      相关资源
      最近更新 更多