I have searched in stackoverflow, and there are similar questions, but the answers did not work in my case.
I want to generate a list of plots, and output them in HTML by knitting Rmarkdown file.
Here is a very simple demo code:
library(ggplot2)
library(plotly)
DF <- data.frame(A = c(1:10), B = c(1:10), C = (1:10))
myplots <- vector('list', ncol(DF))
for (i in 1: dim(DF)[2]) {
p = ggplot(DF, aes(x = DF[, i], y = C)) +
geom_point()
myplots[[i]] = plotly::ggplotly(p)
}
myplots[[1]]
myplots[[2]]
myplots[[3]]
If I index them, they can be plotted in HTML file.
myplots[[1]]
myplots[[2]]
myplots[[3]]
However, if I directly use the list object or loop it, none of the method worked:
# method 1
myplots
# method 2
for (i in seq_along(myplots)) print(myplots[[i]])
#nethod 3
for (i in 1:length(myplots)) print(myplots[[i]])
I don't understand the problem. Thanks a lot for your help.