我是R-Markdown
的新手,我在HTML报告中渲染plotly
对象时遇到问题。问题是for loop
。当我在for loop
中创建图形时,它们在最终报告中不可见。我找到了单循环的解决方案:LINK双循环中的情况如何?我不能将解决方案从单循环转换为例如2个嵌套循环。总结:
1.单FOR LOOP
- 打印不可见:
plots <- list()
for(i in 1:3) {
l[[i]] <- plot_ly(x = rnorm(10))
}
l
- 图可见:
l <- htmltools::tagList()
for (i in 1:3) {
l[[i]] <- plot_ly(x = rnorm(10))
}
l
1.双人FOR LOOP
- 打印不可见:
for(j in 1:3){
# some text, tables depends of j
# transform dataset based on `j`
l <- htmltools::tagList()
for (i in 1:3) {
l[[i]] <- plot_ly(x = rnorm(10))
}
l # <-plot on the end of each j loop, not on the end of whole loop like was in single for loop
}
- 图可见:??
有什么办法解决这个问题吗?我将非常感谢任何帮助!
EDIT:代码示例
#' ---
#' title: "Sample Document"
#' output:
#' html_document:
#' toc: true
#' theme: united
#' ---
#' R Markdown report of graphs generated with a double (nested) for loop
df <- data.frame(
j = numeric(),
desc = character()
)
for(j in c(1:3)){
cat(paste0("# below we present visualizations for iteration no.: ", j, "\n"))
#' Random table
#+ echo=FALSE, results = 'asis'
df <- rbind(df, data.frame(j = j, desc = paste0("iteration no.: ", j)))
print(knitr::kable(df, row.names = F))
l <- htmltools::tagList()
for(i in (1:3)){
l[[i]] <- plot_ly(x = j*rnorm(i*10))
print(l[[i]])
}
}
1条答案
按热度按时间sy5wg1nm1#
基本上,你可以用同样的方法来实现这一点,把外部循环 Package 在第二个
tagList
中:Using
lapply
Using a
for
looptitle: "Untitled"
output:
html_document:
toc: true
theme: united
date: "2023-04-12"