【问题标题】:Double for loop to check dynamic number of plots in shiny app双 for 循环检查闪亮应用程序中的动态图数
【发布时间】:2022-01-26 08:08:11
【问题描述】:

我发现自己面临一个问题:

我已经有了像这样的闪亮应用程序(服务器部分):

observe({
      data <- my_function() %>%
       filter(col1 == "X1")

h <- sort(unique(data$year))

nb <- length(h)
lst <- as.list(h)

output$plot_name <- renderUI({
  plot_output <- lapply(1:nb,
                                 function(n1) {
                                   plotname <- paste0("plots", n1)
                                   plotOutput(plotname)
                                 })
  do.call(tagList, plot_output)
})

my_data <- list()

i <- 0
for (i in 1:length(lst)) {
  
  local({
    my_i <- i
    plotname <- paste0("plots", my_i)
    my_data[[my_i]] <- data %>% 
      filter(year == lst[[my_i]])
    
    output[[plotname]] <- renderPlot({
      
      g <- ggplot(..........)
      
      print(g)
      
    })
    
  })
}

})

这让我可以为给定的 X 每年输出图表。

但是我想删除过滤器并为每个 X 的每一年提供一个图表,因为我知道所有 X 的年数不同(X1:2000,X2:2000 和 2001,X3:2001等等......)。

因为我想动态确定图表的数量。

如果你有任何线索,那将非常有用。

提前致谢。

【问题讨论】:

  • 能否提供my_function()的内容让代码可复现?
  • 感谢@jpdugo17,您的评论。为了简化 my_function 返回一个像这样的数据表: Col1 = X1, X1, X2, X1, X3, X2, X2, X3; Col2 = 1、2、1、0、1、1、2、2;年 = 2000、2001、2000、2000、2000、2000、2002、2001; pts = 24, 36, 48, 24, 72, 24, 48, 24 。我的 ggplot 是 x = pts, y = col2 的简单图,图标题显示年份和相应的 X。

标签: for-loop shiny shinydashboard shiny-server shinyapps


【解决方案1】:

我们可以在一个数据框中创建所有的图,然后计算它们有多少,以便创建相应的输出。

library(tidyverse)
library(shiny)


df <-
  read_table("col1 col2 year pts
X1 1 2000 24 
X1 2 2001 36 
X2 1 2000 48
X1 0 2000 24
X3 1 2000 72
X2 1 2000 24
X2 2 2002 48
X3 2 2001 24")



ui <- fluidPage(
  uiOutput("plot_name")
)

server <- function(input, output, session) {
  my_function <- reactive({
    df
  })
  
  observeEvent(my_function, {
    data <- my_function() 
    

# Create the plots --------------------------------------------------------

    
    plots_df <- 
      data %>% 
      group_by(col1, year) %>%
      summarise(plot = list(
        ggplot(cur_data_all() ,aes(x = pts, y = col2)) +
          geom_point() +
          ggtitle(paste('col:', col1 ,'year:', year))
      ))
    

# plots UI ----------------------------------------------------------------

    
    nb <- length(plots_df$plot)
    plotname <- paste0("plots", 1:nb) # save the names for renderPlot functions

    output$plot_name <- renderUI({
      plot_output <- lapply(
        plotname,
        function(n1) {
          plotOutput(n1)
        }
      )
      do.call(tagList, plot_output)
    })


# renderPlot funcitons ----------------------------------------------------


    walk2(plotname, plots_df$plot, ~ {
      output[[.x]] <<- renderPlot({
        .y
      })
    })
   })
}

shinyApp(ui, server)

【讨论】:

  • 感谢 @jpdugo17 的回答和您的时间,我想避免使用第二个 selectInput 因为这部分已经在我的大脚本中。
  • @ss10 你想同时显示所有的图吗?好像在sliderInput中选择了所有选项?
  • 是@jpdugo17,因为我有第一个selectizeInput,如果我输入例如Xena,它是X1,X2 ....,如果我输入Yin,它是Y1,Y2 ....出现。因此,当我键入 Xena 时,我想年复一年地同时显示所有图表,而当我自动键入 Yin 时,则显示相同的东西。 Xena 和 Yin 在 df 的另一列中。
  • @ss10 我编辑了答案。它现在应该绘制所有内容。
  • 非常感谢@jpdugo17,这很好地回答了我的问题。我必须承认我对 walk2 不太熟悉,但多亏了你,我学到了一些新东西。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
相关资源
最近更新 更多