3

I have a function in my global.R file that creates a plot:

CreatePlot<-function(mergedFiles,File1, File2) 
{
  main<- mergedFiles
  csv1<- main[,1:4]
  csv2<- cbind( main[,1],main[,5:7] )
  strikes<- factor(main$Strike,levels=c(main$Strike),ordered=TRUE)
  stacked<- stacked <- data.frame(time=strikes, value =c(c(csv1$Vol), c(csv2$Vol)) ,     variable = rep(c(File1,File2), each=NROW(csv1[,1])))  

  g<- ggplot(stacked, aes( x = time,  y=value, colour=variable, group= variable)      )       +   geom_line()  +  xlab("Strike") +geom_point(shape = 7,size = 1) +
    ylab("Volatiltiy") +ggtitle("Plot of Volatilities") + theme(axis.text.x =       element_text(angle = 90, hjust = 1))
  return(g)  
}

I want to create a function to print multiple plots in a loop. Let's say I want 3 plots I am doing something like this in my server.R:

  output$MyList <- renderUI({ 
        main<- mergedFiles() # this is the data for my plots
        g<-CreatePlot(main, input$File1, input$File2) #this calls the create plot function
        for (i in 1:3){
          renderPlot({
              print(g) #for testing purposes I am just trying to print the same plot 3     times
          })
        }

})

and my UI.R in the mainPanel:

uiOutput("MyList")

When I run this nothing happens. The screen is just blank with no errors. Any ides how to print multiple plots like this using a loop?

Thank yoU!

user3022875
  • 8,598
  • 26
  • 103
  • 167
  • You could always generate each plot and then use `grid.arrange` or use multiple named output `div`s, generate the plots to reactive values and then output those values in each named, distinct `div`. – hrbrmstr Apr 02 '14 at 22:47

1 Answers1

3

You can't render a plot with renderUI. renderUI evaluates only "expression that returns a Shiny tag object, HTML, or a list of such objects."

You must create a plotOutput for each plot you want to display. So you can create severals plotOuputs dynamically with renderUI and then create the graphs with a loop.

Have a look to this example : https://gist.github.com/wch/5436415/

Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
  • What is the purpose of the max_plots? I see that it is set to 5 and the for loop loops 5 times but how does it only produce 2 plots if it loops 5 times? In my app I need to set max_plots dynamically based on the lenght of a vector that is returned from a function. Do you know how I'd do that? – user3022875 Apr 03 '14 at 14:23
  • can you see here http://stackoverflow.com/questions/22840892/r-multiple-plots-with-max-plots-dynamically-set – user3022875 Apr 03 '14 at 14:34
  • 5 plots are created in the loop you are right, but plots are displayed only if the respective `plotOutput` exists, and to create them he loops from 1:input$n and not 1:max_plots. This is the outputs that are created dynamically not the plots, yes. – Julien Navarre Apr 03 '14 at 14:45
  • Can you tell me how max_plots can be reactive instead of hardcoded to 5? I have some inputs: input$Desk and that goes into a reactive function on my shinyServer.R file. I want max_plots to be set to the result of that function. Any idea how? – user3022875 Apr 03 '14 at 14:55
  • I guess what I am asking is: right now that For loop is not in a reactive call? Can I put it in a reactive function somehow so that I can call the function f(input$desk) that returns a number x that max_plots is set to i.e. max_plots <- x – user3022875 Apr 03 '14 at 14:59