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

标签 r for-loop 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
```

该列表如下所示:
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
...

Some data from the list

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

Run output

knit 输出只给了我 R 控制台输出:
## [[1]]
## 
## [[2]]
## 
## [[3]]
## 
## [[4]]
## 
## [[5]]

如果我尝试以下代码,我会丢失 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)))
}

最佳答案

使用 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)
```

enter image description here

关于R Plot_ly 列出在 .Rmd R-Studio 运行模式下绘制多个图,但在针织时不绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49719359/

相关文章:

java - 如何循环每个 hh :00 whithin a time period in Java?

Rstudio Git bash 每次 pop

r - 设置工作目录: "Error in setwd: cannot change working directory" under "C:/Documents and Settings/..."

excel - 如果第一个 IF 为 false,如何移动到数组中的下一个条目?

Python 嵌套循环问题

r - 如何更改 RStudio(或 R)中的默认目录?

latex - 如何在四开 pdf 书籍的标题页上使用不同的页边距?

R:交互效果中的特殊对比

r - 在 ggplot 中设置上限以包含大于最大值的标签

r - 更改 RPres 中的数学公式字体大小