Brilliant R renderPlots on the fly

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]) }) }) } }) 
+8
r shiny shiny-server
source share
2 answers

If someone is still interested in the answer, try the following:

 library(shiny) runApp(shinyApp( ui = shinyUI( fluidPage( numericInput("number", label = NULL, value = 1, step = 1, min = 1), uiOutput("plots") ) ), server = function(input, output) { ### This is the function to break the whole data into different blocks for each page plotInput <- reactive({ n_plot <- input$number total_data <- lapply(1:n_plot, function(i){rnorm(500)}) 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) }) observe({ lapply(1:plotInput()$n_plot, function(i){ output[[paste("plot", i, sep="") ]] <- renderPlot({ hist(plotInput()$total_data[[i]], main = paste("Histogram Nr", i)) }) }) }) } )) 
+5
source share

Instead of isolate({}) , local({}) .

http://www.inside-r.org/packages/cran/shiny/docs/isolate

"The expression allocated for isolation () is evaluated in the calling environment. This means that if you assign a variable inside the isolate (), its value will be displayed outside the isolate (). If you want to avoid this, you can use local () inside the isolate (). "

-one
source share

All Articles