I am trying to dynamically display multiple graphs in a tab (it is better if this can be done through several tabs). After some searching, I found the post to be very helpful. But in my case, the number of graphs is determined by the downloaded CSV file. So I think the question is how to call plotInput()$n_plot in a for loop? I appreciate any suggestions!
At this point, I can create a multiplicity of <div>s by calling renderUI .
<div id="plot1" class="shiny-plot-output" style="width: 800px ; height: 800px"></div> <div id="plot2" class="shiny-plot-output" style="width: 800px ; height: 800px"></div>
But I could not correctly call the reactive function in the for (i in 1:plotInput()$n_plot) . Error message:
Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context.
Server.R
shinyServer(function(input, output) { ### This is the function to break the whole data into different blocks for each page plotInput <- reactive({ get_the_data() return (list("n_plot"=n_plot, "total_data"=total_data)) }) ##### Create divs###### output$plots <- renderUI({ plot_output_list <- lapply(1:plotInput()$n_plot, function(i) { plotname <- paste("plot", i, sep="") plotOutput(plotname, height = 280, width = 250) }) do.call(tagList, plot_output_list) }) # Call renderPlot for each one. ####This is the place caused the error## for (i in 1:plotInput()$n_plot) { local({ my_i <- i plotname <- paste("plot", my_i, sep="") output[[plotname]] <- renderPlot({ hist(plotInput()$total_data[i]) }) }) } })
Update
If I end the for loop inside the reactive function and call it part of renderUI , the loop works, but there is no graph, I think this is because renderUI only creates HTML tags, it will not assign images to the created tags.
output$plots <- renderUI({ plot_output_list <- lapply(1:plotInput()$n_plot, function(i) { plotname <- paste("plot", i, sep="") plotOutput(plotname, height = 280, width = 250) }) do.call(tagList, plot_output_list) plot_concent() }) # Call renderPlot for each one. ####This is the place caused the error## plot_concent<-reactive({ for (i in 1:plotInput()$n_plot) { local({ my_i <- i plotname <- paste("plot", my_i, sep="") output[[plotname]] <- renderPlot({ hist(plotInput()$total_data[i]) }) }) } })