我正在尝试动态构建一个报告,需要运行循环,并在每次迭代中打印一些消息、表格和图表。除了图表之外,我可以让所有东西都正常工作。
example.rmd
```{r echo=FALSE, results='asis', fig.keep='all', message = FALSE, warning = FALSE}
library(knitr)
library(plotly)
for(i in 1:4){
foo <- iris[sample(nrow(iris), 20), ]
cat("\n")
cat("# Iteration", i, "\n")
# Print the first few lines
print(kable(head(foo)))
cat("\n")
# Plot Sepal.Width vs Petal.Length using ggplotly()
plt <- ggplot(foo, aes(x = Sepal.Width, y = Petal.Length))+geom_point()
# plot(plt) # <- this works
# plot(ggplotly(plt)) # <- this doesn't work
# ggplotly(plt) # <- this doesn't work
cat("\n")
}
```
我该如何让Plotly的图表在我的报告中呈现?