RenderPlot inside RenderUI?

1,436 views
Skip to first unread message

tao hong

unread,
Nov 14, 2014, 9:57:49 AM11/14/14
to shiny-...@googlegroups.com
I am trying to dynamically display multiple plots in a tab (better if this could be done across multiple tabs). After some search, I found this [post][1] is very helpful. But in my case, the number of plots is determined by the uploaded `CSV file. So I think the question is how to call `plotInput()$n_plot`in a for loop? I appreciate any suggestions! 

At this moment, I am able to create multiples `<div>s` from 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 properly call the reactive function in the `for (i in 1:plotInput()$n_plot)` loop. The error message is: 

    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 wrap the for loop inside a `reactive` function and call it as part of `renderUI`, the loop works, but the plot is missing now...

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


Joe Cheng

unread,
Nov 15, 2014, 11:44:58 PM11/15/14
to tao hong, shiny-...@googlegroups.com
Replace "plot_concent<-reactive({" with "observe({", does that fix it?

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/047c2bfb-1e5f-49ee-b866-cf73bf8360af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tao hong

unread,
Nov 17, 2014, 12:44:26 AM11/17/14
to shiny-...@googlegroups.com, hongt...@gmail.com
Thanks! It is working now!
Reply all
Reply to author
Forward
0 new messages