R Plot_ly 列出在 .Rmd R-Studio 运行模式下绘制多个图,但在针织时不绘制

Jac*_*sox 4 for-loop r rstudio r-markdown plotly

我正在尝试从 data.frames 列表中绘制多个图。我正在使用 Markdown 来呈现数据。在 R-Studio 中,当我单击“>”运行按钮时,我得到了所有的图。

我尝试使用的代码是:

### Plot each list
```{r plotSWBS6IByPhaseAndWave, echo=TRUE, eval=TRUE}
plotList <- list()

for(i in 1:length(seriesFigureSaleDataBS6I_PhaseWave)) {
  plotList[[i]] <- plot_ly(data = seriesFigureSaleDataBS6I_PhaseWave[[i]],
                 x = ~priceDate,
                 y = ~amount,
                 color = ~actionFigurePackageName,
                 colors = "Pastel2",
                 type = "scatter",
                 mode = "markers") %>%
                 layout(title = paste("Phase", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Phase, "& Wave", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Wave))
}
# p <- lapply(seriesFigureSaleDataBS6I_PhaseWave, function(phaseWaveRow) plot_ly(data = phaseWaveRow, x = ~priceDate, y = ~amount, color = ~actionFigureUniqueId, colors = "Pastel2"))

print(class(seriesFigureSaleDataBS6I_PhaseWave))
print(summary(seriesFigureSaleDataBS6I_PhaseWave))

#rm(seriesFigureSaleDataBS6I_PhaseWave)

plotList
```
Run Code Online (Sandbox Code Playgroud)

该列表如下所示:

print(summary(seriesFigureSaleDataBS6I_PhaseWave))
                                            Length Class      Mode
40th.1                                      35     data.frame list
40th.2                                      35     data.frame list
40th.Legacy                                 35     data.frame list
Blue.5                                      35     data.frame list
Blue.6                                      35     data.frame list
Blue.7                                      35     data.frame list
Blue.8                                      35     data.frame list
...
Run Code Online (Sandbox Code Playgroud)

列表中的一些数据

运行模式下的输出如下所示:

运行输出

knit 输出只给了我 R 控制台输出:

## [[1]]
## 
## [[2]]
## 
## [[3]]
## 
## [[4]]
## 
## [[5]]
Run Code Online (Sandbox Code Playgroud)

如果我尝试以下代码,我会丢失 R 控制台输出(这很好)并在 R-Studio“运行”模式下获得绘图,但在 knit 模式下没有绘图输出:

for(i in 1:length(seriesFigureSaleDataBS6I_PhaseWave)) {
  print(plot_ly(data = seriesFigureSaleDataBS6I_PhaseWave[[i]],
                 x = ~priceDate,
                 y = ~amount,
                 color = ~actionFigurePackageName,
                 colors = "Pastel2",
                 type = "scatter",
                 mode = "markers") %>%
                 layout(title = paste("Phase", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Phase, "& Wave", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Wave)))
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*zer 7

使用htmltools::tagList函数:

---
title: "Knit a List of Plotly Graphs"
output: html_document
---

```{r, include = F}
library(dplyr)
library(plotly)
library(htmltools)
```


```{r, echo=TRUE, eval=TRUE}

dat <- list(mtcars, mtcars)
plots <- lapply(dat, function(x) {
  plot_ly(data = x, x = ~hp, y = ~mpg) %>% 
    add_trace(type = "scatter", mode = "markers")
})
tagList(plots)
```
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明