使用 flexdashboard、lapply 和 patchwork 的动态图数

Dynamic number of plots with flexdashboard, lapply and patchwork

我有一个可变的地块列表,它 取决于用户 select 的地块数量。相反,他们输入数据,并根据他们的数据结构,我 return 列表中适当数量的地块。用户不知道适当数量的地块 - 我的代码为他们计算出来。因此,列表中的对象数量不是它们 select.

的输入

我已成功使用 lapply 在 RStudio 中生成多个绘图,但只有最后一个绘图出现在我的 Shiny App 中。我想我已经在一些传统的闪亮应用程序上看到过这个讨论,但我无法将它翻译成 flexdashboard/RMD。

我在下面包含了一个简单的代表。 iris_list 中的对象数量是可变的。我使用 lapply 将列表中每个 object/dataset 的两个图绑定在一起,并拼凑添加一个共享图例(不包含在 reprex 中)。如何让列表中的所有对象出现在我的应用程序中?我得到的印象是我需要多个 renderPlots 和一个 Observe 语句。

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

})
```

可以在这里找到答案:。之前看错了,但是答案一直就在我眼前

诀窍是把renderPlot改成reactive,放到一个对象中,然后加上renderUIobserve语句。这在 flexdashboard 中非常有效。情节的 widthheight 像往常一样在 renderPlot 语句之后,例如

renderPlot({
            
        }, width=900,height = 325)