R Plot_ly list在.Rmd R-Studio运行模式中绘制多个绘图,但在编织时不绘制

时间:2018-04-08 15:12:14

标签: r for-loop rstudio r-markdown plotly

我正在尝试从data.frames列表中绘制多个图。我正在使用Markdown来呈现数据。在R-Studio中,当我点击">"按下按钮,我得到了所有的情节。

我尝试使用的代码是:

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

列表如下:

## [[1]]
## 
## [[2]]
## 
## [[3]]
## 
## [[4]]
## 
## [[5]]

Some data from the list

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

Run output

编织输出只给我R控制台输出:

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)))
}

如果我尝试以下代码,我将丢失R控制台输出(这很好),并在R-Studio"运行"模式,但在编织模式下没有绘图输出:

Validator::extend('numericarray', function($attribute, $value, $parameters)
{

    foreach($value as $v) {
         if(!is_int($v)) return false;
    }
    return true;
});

1 个答案:

答案 0 :(得分:3)

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