我有一个可变的地块列表,它不依赖于用户选择的地块数量。相反,他们输入数据,并根据他们的数据结构,我在列表中返回适当数量的图。用户不知道适当数量的绘图 - 我的代码为他们计算出来。因此,列表中的对象数量不是他们选择的输入。
我已经成功地使用 lapply 在 RStudio 中生成了多个绘图,但只有最后一个绘图出现在我的 Shiny App 中。我想我已经在一些传统的闪亮应用程序上看到过这个讨论,但是我在将它翻译成 flexdashboard/RMD 时遇到了麻烦。
我在下面包含了一个简单的代表。iris_list 中的对象数量是可变的。我使用 lapply 将列表中每个对象/数据集的两个图绑定在一起,并使用拼凑来添加共享图例(不包括在 reprex 中)。如何让列表中的所有对象出现在我的应用程序中?我得到的印象是我需要多个渲染图和一个观察语句。
---
title: "my app"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
source_code: embed
theme: flatly
always_allow_html: yes
---
### Plots
```{r chunk}
renderPlot({
iris_list<- list(iris,iris)
p1 <- map(iris_list,~ggplot(data=.x,aes(x=Sepal.Length))+geom_histogram())
p2 <- map(iris_list,~ggplot(data=.x,aes(x=Petal.Length))+geom_histogram())
plots_combined <- lapply(seq(length(iris_list)),function(x) c(p1[x],p2[x]))
lapply(seq(length(plots_combined)), function(x) wrap_plots(plots_combined[[x]])+
plot_annotation(title=paste("plot",x)))
})
```