plot all figures of a loop simultaneously on one page (all plots of a loop)

I have a render plotly function in which I get some plots of a loop, how can I plot all of them at the same time?
please see a part of my code:
please notice this code is inside of a render plotly

plot_list_final = list()
            for (i in 1: length(s)) {
            Final_fig_box <- subplot(plot_list1[[i]], plot_list2[[i]])%>%
              layout(title = "Inferred Celltype Activity")
            plot_list_final[[i]] = Final_fig_box 
            return(plot_list_final[[i]]) 
            }

I would like to have all plots on a page at the same time, but the code shows only the last plot (last i).
Thanks in advance

Seems you have two issues.
Not disposing of all but the last plot that you collate
Then generating a ui capable of displaying all the kept plots.
Ill help you directly with the first part, but the second i recommend you read up on dynamic UI and make a reprex if you want further help with.

plot_list_final = list()
            for (i in 1: length(s)) {
            Final_fig_box <- subplot(plot_list1[[i]], plot_list2[[i]])%>%
              layout(title = "Inferred Celltype Activity")
            plot_list_final[[i]] = Final_fig_box 
    }
            return(plot_list_final) 
            }

Yes, I have realized that I should have separate plot functions in UI section to show all figures together, but my question is: for example if I have 5 plots (i is 5 in the above code), how can I write to add 5 plot functions in UI?
I don't know the syntax!
Is it possible to automatically add UI elements based on the server part's output?

Is the number of plots fixed or variable?
You could use "insertUI" to insert some elements, e.g. the plots that you could label "plot1", "plot2", "plot3" etc, however I think this isn't very elegant.

The easiest strategy would be to merge all plots into one figure and just show this!?
Did you consider using facet_wrap?
Also the cowplot-library allows to arrange multiple plots into one, here the plot_grid function might be useful when the plots are already stored in a list.

library(shiny)
library(plotly)
library(tidyverse) # includes purrr::map*
library(rlang) # for list2()

ui <- fluidPage(
  actionButton("btn","add a plot"),
  uiOutput("myui")
)

server <- function(input, output, session) {
  
  my_plots <- reactiveVal(NULL)
  
  observeEvent(input$btn,{
    existing <- my_plots()
    new_plot_name <- paste0("plot",input$btn)
    new_plot <- plot_ly(economics,x=~date,
                        y=as.formula(paste0("~",sample(c("pce" ,   "pop" ,"psavert", "uempmed" ,"unemploy"),
                                                       1))),
                        type="scatter",mode="markers") %>% layout(title=new_plot_name)
    new_named_plot <- list2(!!sym(new_plot_name) := new_plot)
    new_plot_list <- append(existing,new_named_plot)
    my_plots(new_plot_list)
    output[[new_plot_name]] <- renderPlotly({
      new_plot
    })
  })
  
  
  output$myui <- renderUI({
    mp <- req(my_plots())
    
    imap(mp,
         ~{plotlyOutput(.y)})
  })
  
  
  
}

shinyApp(ui, server)
1 Like

Resources for dynamic UI are available both here:
Shiny - Build a dynamic UI that reacts to user input (rstudio.com)
and here : Chapter 10 Dynamic UI | Mastering Shiny (mastering-shiny.org)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.